aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/bloodasp/galacticgreg/auxiliary/ProfilingStorage.java
blob: 192fb07262358a7e4d115834f1cd533c83a032b0 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
package bloodasp.galacticgreg.auxiliary;

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

import bloodasp.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 Map<String, List<Long>> mProfilingMap;
	
	public ProfilingStorage()
	{
		mProfilingMap = new HashMap<String, List<Long>>();
	}
	
	/**
	 * Add a new time to the list of pDimension. Will be ignored it tTotalTime == 0
	 * @param pDimension
	 * @param tTotalTime
	 */
	public void AddTimeToList(ModDimensionDef pDimension, long pTotalTime)
	{
		try
		{
			if (pTotalTime == 0)
				return;
			
			if(!mProfilingMap.containsKey(pDimension.getDimIdentifier()))
				mProfilingMap.put(pDimension.getDimIdentifier(), new LinkedList<Long>());
	
			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 ll = (LinkedList) mProfilingMap.get(pDimension.getDimIdentifier());
			
			if(ll != null)
			{
				Iterator<Long> qItr = ll.iterator();
				while(qItr.hasNext())
				{
					tAverage += qItr.next();
					tTotalVal++;
				}
				
				tReturnVal = (long)((float)(tAverage / tTotalVal));
			}
			return tReturnVal;
		}
		catch (Exception e)
		{
			return -1;
		}
	}
}