-
Notifications
You must be signed in to change notification settings - Fork 44
/
PerformaceTest.java
178 lines (144 loc) · 6.41 KB
/
PerformaceTest.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package net.neoremind.dynamicproxy;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Test;
import com.google.common.collect.Lists;
import net.neoremind.dynamicproxy.impl.ASMCreator;
import net.neoremind.dynamicproxy.impl.ByteBuddyCreator;
import net.neoremind.dynamicproxy.impl.CglibCreator;
import net.neoremind.dynamicproxy.impl.JavassistCreator;
import net.neoremind.dynamicproxy.impl.JdkProxyCreator;
import net.neoremind.dynamicproxy.sample.Echo;
/**
* 性能测试JVM参数设置
* <pre>
* -Xms512m -Xmx512m -XX:PermSize=128M -XX:MaxPermSize=128M -verbose:gc
* </pre>
*
* @author zhangxu
*/
public class PerformaceTest {
private static final Class<?>[] ECHO_ONLY = new Class[] {Echo.class};
private ProxyCreator jdkProxyCreator = new JdkProxyCreator();
private ProxyCreator javassistCreator = new JavassistCreator();
private ProxyCreator cglibCreator = new CglibCreator();
private ProxyCreator asmCreator = new ASMCreator();
private ProxyCreator byteBuddyCreator = new ByteBuddyCreator();
@Test
public void testCreatePerformance() {
testCreate(10000);
}
@Test
public void testInvokePerformance() {
testInvoke(1000 * 1000 * 1);
}
public void testCreate(int invokeNum) {
List<Profiler> profilerList = Lists.newArrayList();
profilerList.add(innerTestCreate(byteBuddyCreator, invokeNum));
hang(2);
profilerList.add(innerTestCreate(asmCreator, invokeNum));
hang(2);
profilerList.add(innerTestCreate(cglibCreator, invokeNum));
hang(2);
profilerList.add(innerTestCreate(javassistCreator, invokeNum));
hang(2);
profilerList.add(innerTestCreate(jdkProxyCreator, invokeNum));
System.out.println("-------------------------------------");
System.out.println(String.format("| Create proxy number:%8d |", invokeNum));
System.out.println("-------------------------------------");
System.out.println("| ProxyCreator | time cost |");
System.out.println("-------------------------------------");
for (Profiler profiler : profilerList) {
System.out.println(profiler);
}
System.out.println("-------------------------------------");
}
public void testInvoke(int invokeNum) {
List<Profiler> profilerList = Lists.newArrayList();
Echo jdkProxyCreatorInvokerProxy = jdkProxyCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
Echo javassistCreatorInvokerProxy = javassistCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
Echo cglibCreatorInvokerProxy = cglibCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
Echo asmCreatorInvokerProxy = asmCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
Echo byteBuddyCreatorInvokerProxy = byteBuddyCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
// warmup
innerTestInvoke(byteBuddyCreator, byteBuddyCreatorInvokerProxy, 1);
innerTestInvoke(asmCreator, asmCreatorInvokerProxy, 1);
innerTestInvoke(cglibCreator, cglibCreatorInvokerProxy, 1);
innerTestInvoke(javassistCreator, javassistCreatorInvokerProxy, 1);
innerTestInvoke(jdkProxyCreator, jdkProxyCreatorInvokerProxy, 1);
hang(2);
profilerList.add(innerTestInvoke(byteBuddyCreator, byteBuddyCreatorInvokerProxy, invokeNum));
hang(2);
profilerList.add(innerTestInvoke(asmCreator, asmCreatorInvokerProxy, invokeNum));
hang(2);
profilerList.add(innerTestInvoke(cglibCreator, cglibCreatorInvokerProxy, invokeNum));
hang(2);
profilerList.add(innerTestInvoke(javassistCreator, javassistCreatorInvokerProxy, invokeNum));
hang(2);
profilerList.add(innerTestInvoke(jdkProxyCreator, jdkProxyCreatorInvokerProxy, invokeNum));
System.out.println("-------------------------------------");
System.out.println(String.format("| Invoke number:%12d |", invokeNum));
System.out.println("-------------------------------------");
System.out.println("| ProxyCreator | time cost |");
System.out.println("-------------------------------------");
for (Profiler profiler : profilerList) {
System.out.println(profiler);
}
System.out.println("-------------------------------------");
}
public Profiler innerTestCreate(ProxyCreator proxyCreator, int invokeNum) {
long start = System.currentTimeMillis();
for (int i = 0; i < invokeNum; i++) {
proxyCreator.createInvokerProxy(new InvokerTester(), ECHO_ONLY);
}
return Profiler.apply(System.currentTimeMillis(), start)
.setProxyCreatorName(proxyCreator.getClass().getSimpleName());
}
public Profiler innerTestInvoke(ProxyCreator proxyCreator, Echo echo, int invokeNum) {
long start = System.currentTimeMillis();
for (int i = 0; i < invokeNum; i++) {
echo.echoBack("abc");
}
return Profiler.apply(System.currentTimeMillis(), start)
.setProxyCreatorName(proxyCreator.getClass().getSimpleName());
}
private static class Profiler {
private String proxyCreatorName;
private long timeCost;
public Profiler(long timeCost) {
this.timeCost = timeCost;
}
public static Profiler apply(long end, long start) {
return new Profiler(end - start);
}
public Profiler setProxyCreatorName(String proxyCreatorName) {
this.proxyCreatorName = proxyCreatorName;
return this;
}
public String getProxyCreatorName() {
return proxyCreatorName;
}
public long getTimeCost() {
return timeCost;
}
@Override
public String toString() {
return String.format("| %16s | %5dms |", proxyCreatorName, timeCost);
}
}
private void hang(int sec) {
try {
//System.gc();
Thread.sleep(sec * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected static class InvokerTester implements ObjectInvoker {
private static final long serialVersionUID = -8586595308078627409L;
@Override
public Object invoke(Object proxy, Method method, Object... args) throws Throwable {
return args[0];
}
}
}