forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndyDemo.java
More file actions
125 lines (107 loc) · 4.03 KB
/
IndyDemo.java
File metadata and controls
125 lines (107 loc) · 4.03 KB
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
122
123
124
125
import com.github.mustachejava.codegen.CodegenObjectHandler;
import com.github.mustachejava.codegen.CodegenReflectionWrapper;
import com.github.mustachejava.indy.IndyWrapper;
import com.github.mustachejava.reflect.ReflectionObjectHandler;
import com.github.mustachejava.util.Wrapper;
import org.junit.Test;
import java.lang.reflect.Method;
public class IndyDemo {
public static final int TIMES = 100000000;
public static void main(String[] args) throws Throwable {
IndyDemo indyDemo = new IndyDemo();
for (int i = 0; i < 10; i++) {
timeReflectionOH(indyDemo);
timeCodegenReflectionOH(indyDemo);
timeIndyOH(indyDemo);
timeIndyOHNoGuard(indyDemo);
timeReflection(indyDemo);
timeReflectionCached(indyDemo);
timeDirect(indyDemo);
System.out.println("-----------------");
}
}
public static void timeReflectionOH(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < TIMES; i++) {
REFLECTED.call(scopes);
}
System.out.println("reflection OH: " + (System.currentTimeMillis() - start));
}
public static void timeCodegenReflectionOH(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < TIMES; i++) {
CODEGEN_REFLECTED.call(scopes);
}
System.out.println("codegen reflection OH: " + (System.currentTimeMillis() - start));
}
@Test
public void timeIndyOH() throws Throwable {
for (int i = 0; i < 10; i++) {
timeIndyOH(new IndyDemo());
}
}
@Test
public void timeReflectionOH() throws Throwable {
for (int i = 0; i < 10; i++) {
timeReflectionOH(new IndyDemo());
}
}
public static void timeIndyOH(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < TIMES; i++) {
INDY.call(scopes);
}
System.out.println("indy OH: " + (System.currentTimeMillis() - start));
}
public static void timeIndyOHNoGuard(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < TIMES; i++) {
INDY_NOGUARD.call(scopes);
}
System.out.println("indy OH no guard: " + (System.currentTimeMillis() - start));
}
public static void timeReflection(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < 10000000; i++) {
IndyDemo.class.getDeclaredMethod("someMethod").invoke(scopes[0]);
}
System.out.println("reflection: " + 10*(System.currentTimeMillis() - start));
}
public static void timeReflectionCached(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
Method someMethod = IndyDemo.class.getDeclaredMethod("someMethod");
for (int i = 0; i < TIMES; i++) {
someMethod.invoke(scopes[0]);
}
System.out.println("reflection cached: " + (System.currentTimeMillis() - start));
}
public static void timeDirect(IndyDemo indyDemo) throws Throwable {
long start = System.currentTimeMillis();
Object[] scopes = {indyDemo};
for (int i = 0; i < TIMES; i++) {
((IndyDemo)scopes[0]).someMethod();
}
System.out.println("direct: " + (System.currentTimeMillis() - start));
}
private static Wrapper REFLECTED;
private static Wrapper INDY;
private static IndyWrapper INDY_NOGUARD;
private static Wrapper CODEGEN_REFLECTED;
static {
IndyDemo indyDemo = new IndyDemo();
REFLECTED = new ReflectionObjectHandler().find("someMethod", new Object[] { indyDemo });
CODEGEN_REFLECTED = new CodegenObjectHandler().find("someMethod", new Object[] { indyDemo });
INDY = IndyWrapper.create((CodegenReflectionWrapper) CODEGEN_REFLECTED);
INDY_NOGUARD = IndyWrapper.create((CodegenReflectionWrapper) CODEGEN_REFLECTED, false);
}
private int length = 0;
public int someMethod() {
return length++;
}
}