aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/galacticgreg/auxiliary/ProfilingStorage.java
blob: 94ffb8da88ab1a00e772fa692fa974eee7cb1e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package galacticgreg.auxiliary;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import galacticgreg.api.ModDimensionDef;

/**
 * A simple FIFO-storage for Long-values Will keep 50 values for each dimension in memory Doesn't need to be changed
 * when adding new planets/mods
 */
public class ProfilingStorage {

    private final Map<String, List<Long>> mProfilingMap;

    public ProfilingStorage() {
        mProfilingMap = new HashMap<>();
    }

    /**
     * Add a new time to the list of pDimension. Will be ignored it tTotalTime == 0
     *
     * @param pDimension
     * @param pTotalTime
     */
    public void AddTimeToList(ModDimensionDef pDimension, long pTotalTime) {
        try {
            if (pTotalTime == 0) return;

            if (!mProfilingMap.containsKey(pDimension.getDimIdentifier()))
                mProfilingMap.put(pDimension.getDimIdentifier(), new LinkedList<>());

            LinkedList<Long> ll = (LinkedList<Long>) mProfilingMap.get(pDimension.getDimIdentifier());

            ll.addLast(pTotalTime);

            while (ll.size() > 50) ll.removeFirst();
        } catch (Exception e) {
            // Just do nothing. profiling is for debug purposes only anyways...
        }
    }

    /**
     * Return the average time required to execute the oregen in Dimension pDimension
     *
     * @param pDimension The DimensionType in question
     * @return
     */
    public long GetAverageTime(ModDimensionDef pDimension) {
        try {
            if (!mProfilingMap.containsKey(pDimension.getDimIdentifier())) return -1;

            int tTotalVal = 0;
            long tAverage = 0;
            long tReturnVal = 0;

            LinkedList<Long> ll = (LinkedList<Long>) mProfilingMap.get(pDimension.getDimIdentifier());

            if (ll != null) {
                for (Long aLong : ll) {
                    tAverage += aLong;
                    tTotalVal++;
                }

                tReturnVal = (long) ((float) (tAverage / tTotalVal));
            }
            return tReturnVal;
        } catch (Exception e) {
            return -1;
        }
    }
}