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:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
|
package org.performance.test;
import java.util.ArrayList;
import org.performance.test.entity.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PerformanceTest {
public static void main(String[] args) throws ReflectiveOperationException {
testMap1();
testMap2();
}
public static void testMap1() throws InstantiationException, IllegalAccessException{
System.out.println("Type 1 test");
System.out.println("Map<Class, Class<? extends Agent>");
System.out.println("-----------");
gc();
long mem = Runtime.getRuntime().freeMemory();
System.out.println("Memory before map creation: " + mem);
Map<Class, Class<? extends Agent>> var1 = constructType1();
gc();
long mem2 = Runtime.getRuntime().freeMemory();
System.out.println("Memory after map creation: " + mem2);
System.out.println("Memory delta: " + (mem - mem2));
System.out.println("Entry size: " + ((mem - mem2) / var1.size()));
List<Long> timespan = new ArrayList<>();
for (int i = 0; i < 1000; i++){
long start = System.nanoTime();
Class<? extends Agent> type = var1.get(Boolean.class);
Agent agent = type.newInstance();
agent.doAction(false);
long end = System.nanoTime();
timespan.add(end - start);
}
double avg = timespan.stream().mapToLong(v -> v).average().getAsDouble();
System.out.println("\nAvg Performance: " + avg);
}
public static void testMap2(){
System.out.println("\nType 2 test");
System.out.println("Map<Class, Agent>");
System.out.println("-----------");
gc();
long mem = Runtime.getRuntime().freeMemory();
System.out.println("Memory before map creation: " + mem);
Map<Class, Agent> var2 = constructType2();
gc();
long mem2 = Runtime.getRuntime().freeMemory();
System.out.println("Memory after map creation: " + mem2);
System.out.println("Memory delta: " + (mem - mem2));
System.out.println("Entry size: " + ((mem - mem2) / var2.size()));
List<Long> timespan = new ArrayList<>();
for (int i = 0; i < 1000; i++){
long start = System.nanoTime();
Agent agent = var2.get(Boolean.class);
agent.doAction(false);
long end = System.nanoTime();
timespan.add(end - start);
}
double avg = timespan.stream().mapToLong(v -> v).average().getAsDouble();
System.out.println("\nAvg Performance: " + avg);
}
public static Map<Class, Class<? extends Agent>> constructType1(){
Map<Class, Class<? extends Agent>> map = new HashMap<>();
map.put(Boolean.class, BooleanAgent.class);
map.put(Double.class, DoubleAgent.class);
map.put(Float.class, FloatAgent.class);
map.put(Integer.class, IntegerAgent.class);
map.put(Object.class, ObjectAgent.class);
map.put(Short.class, ShortAgent.class);
map.put(String.class, StringAgent.class);
return map;
}
public static Map<Class, Agent> constructType2(){
Map<Class, Agent> map = new HashMap<>();
map.put(Boolean.class, new BooleanAgent());
map.put(Double.class, new DoubleAgent());
map.put(Float.class, new FloatAgent());
map.put(Integer.class, new IntegerAgent());
map.put(Object.class, new ObjectAgent());
map.put(Short.class, new ShortAgent());
map.put(String.class, new StringAgent());
return map;
}
public static void gc(){
Runtime rt = Runtime.getRuntime();
for (int i = 0; i < 500; i++){
rt.gc();
}
}
}
|