-
-
Save kirkch/3214283 to your computer and use it in GitHub Desktop.
cglib vs javassist performance tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mosaic.esa; | |
import javassist.util.proxy.MethodHandler; | |
import javassist.util.proxy.ProxyFactory; | |
import javassist.util.proxy.ProxyObject; | |
import net.sf.cglib.proxy.Enhancer; | |
import net.sf.cglib.proxy.MethodInterceptor; | |
import net.sf.cglib.proxy.MethodProxy; | |
import org.junit.Test; | |
import java.lang.reflect.InvocationHandler; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
import java.util.concurrent.Callable; | |
@SuppressWarnings("unchecked") | |
public class ProxifierPerformance { | |
static final int ITERATIONS = 10000; | |
@Test | |
public void jdkCreateAndCall() throws Exception { | |
System.out.println("jdkCreateAndCall"); | |
doJdkCreateAndCall(); | |
doJdkCreateAndCall(); | |
doJdkCreateAndCall(); | |
doJdkCreateAndCall(); | |
} | |
private void doJdkCreateAndCall() throws Exception { | |
long startNanos = System.nanoTime(); | |
long v = 0; | |
for (int i = 0; i < ITERATIONS; i++) { | |
v += createJdkProxy().call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( (durationNanos/1000000.0) + " " + v); | |
} | |
@Test | |
public void noProxyreateAndCall() throws Exception { | |
System.out.println("noProxyCreateAndCall"); | |
doNoProxyCreateAndCall(); | |
doNoProxyCreateAndCall(); | |
doNoProxyCreateAndCall(); | |
doNoProxyCreateAndCall(); | |
} | |
private void doNoProxyCreateAndCall() throws Exception { | |
long startNanos = System.nanoTime(); | |
long v = 0; | |
for (int i = 0; i < ITERATIONS; i++) { | |
v += new Counter().call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( (durationNanos/1000000.0) + " " + v ); | |
} | |
@Test | |
public void noProxyCall() throws Exception { | |
System.out.println("noProxyCall"); | |
doNoProxyCall(); | |
doNoProxyCall(); | |
doNoProxyCall(); | |
doNoProxyCall(); | |
} | |
private void doNoProxyCall() throws Exception { | |
long v = 0; | |
Counter counter = new Counter(); | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
v += counter.call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( (durationNanos/1000000.0) + " " + v ); | |
} | |
@Test | |
public void jdkCall() throws Exception { | |
System.out.println("jdkCall"); | |
doJdkCall(); | |
doJdkCall(); | |
doJdkCall(); | |
doJdkCall(); | |
} | |
private void doJdkCall() throws Exception { | |
Callable<Integer> proxy = createJdkProxy(); | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
proxy.call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( durationNanos/1000000.0); | |
} | |
@Test | |
public void javassistCreateAndCall() throws Exception { | |
System.out.println("javaassistCreateAndCall"); | |
doJavaAssistCreateAndCall(); | |
doJavaAssistCreateAndCall(); | |
doJavaAssistCreateAndCall(); | |
doJavaAssistCreateAndCall(); | |
} | |
private void doJavaAssistCreateAndCall() throws Exception { | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
createJavassistProxy().call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( durationNanos/1000000.0); | |
} | |
@Test | |
public void javassistCall() throws Exception { | |
System.out.println("javassistCall"); | |
doJavaAssistCall(); | |
doJavaAssistCall(); | |
doJavaAssistCall(); | |
doJavaAssistCall(); | |
} | |
private void doJavaAssistCall() throws Exception { | |
Callable<Integer> proxy = createJavassistProxy(); | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
proxy.call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( durationNanos/1000000.0); | |
} | |
@Test | |
public void cglibCreateAndCall() throws Exception { | |
System.out.println("cglibCreateAndCall"); | |
doCglibCreateAndCall(); | |
doCglibCreateAndCall(); | |
doCglibCreateAndCall(); | |
doCglibCreateAndCall(); | |
} | |
private void doCglibCreateAndCall() throws Exception { | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
createCglibProxy().call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( durationNanos/1000000.0); | |
} | |
@Test | |
public void cglibCall() throws Exception { | |
System.out.println("cglibCall"); | |
doCglibCall(); | |
doCglibCall(); | |
doCglibCall(); | |
doCglibCall(); | |
} | |
private void doCglibCall() throws Exception { | |
Callable<Integer> proxy = createCglibProxy(); | |
int v = 0; | |
long startNanos = System.nanoTime(); | |
for (int i = 0; i < ITERATIONS; i++) { | |
v += proxy.call(); | |
} | |
long endNanos = System.nanoTime(); | |
long durationNanos = endNanos-startNanos; | |
System.out.println( (durationNanos/1000000.0) + " " + v); | |
} | |
private Callable<Integer> createJdkProxy() { | |
return (Callable<Integer>) Proxy.newProxyInstance(Callable.class.getClassLoader(), | |
new Class[] { Callable.class }, new JdkInvocationHandler(new Counter())); | |
} | |
private Callable<Integer> createJavassistProxy() | |
throws InstantiationException, IllegalAccessException { | |
ProxyFactory proxyFactory = new ProxyFactory(); | |
proxyFactory.setInterfaces(new Class[] { Callable.class }); | |
Class<?> proxyClass = proxyFactory.createClass(); | |
Callable<Integer> proxy = (Callable<Integer>) proxyClass.newInstance(); | |
((ProxyObject) proxy).setHandler(new JavassistMethodInterceptor(new Counter())); | |
return proxy; | |
} | |
private Callable<Integer> createCglibProxy() { | |
Enhancer enhancer = new Enhancer(); | |
enhancer.setInterfaces(new Class[] { Callable.class }); | |
enhancer.setCallback(new CglibMethodInterceptor(new Counter())); | |
Callable<Integer> proxy = (Callable<Integer>) enhancer.create(); | |
return proxy; | |
} | |
private static class JdkInvocationHandler | |
implements InvocationHandler { | |
final Object delegate; | |
public JdkInvocationHandler(Object delegate) { | |
this.delegate = delegate; | |
} | |
public Object invoke(Object object, Method method, Object[] args) | |
throws Throwable { | |
return method.invoke(delegate, args); | |
} | |
} | |
private static class CglibMethodInterceptor | |
implements MethodInterceptor { | |
final Object delegate; | |
public CglibMethodInterceptor(Object delegate) { | |
this.delegate = delegate; | |
} | |
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) | |
throws Throwable { | |
return methodProxy.invoke(delegate, args); | |
} | |
} | |
private static class JavassistMethodInterceptor | |
implements MethodHandler { | |
final Object delegate; | |
public JavassistMethodInterceptor(Object delegate) { | |
this.delegate = delegate; | |
} | |
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) | |
throws Throwable { | |
return thisMethod.invoke(delegate, args); | |
} | |
} | |
private static class Counter | |
implements Callable<Integer> { | |
int count = 0; | |
public Integer call() | |
throws Exception { | |
return count++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results of running on a 2GHz laptop, CGLib 2.2.2, JavaAssist 3.12.1.GA
CGLib is quite slow to create proxies, however they are fast once created.
jdkCreateAndCall
171.981
44.36
20.777
11.891
jdkCall
1.413
1.433
1.424
1.399
noProxyCreateAndCall
0.856
0.798
0.802
0.86
noProxyCall
1.332
1.039
0.578
0.568
javaassistCreateAndCall
504.636
142.001
96.048
91.742
javassistCall
0.723
0.718
0.72
0.713
cglibCreateAndCall
337.819
116.297
47.848
53.756
cglibCall
0.722
0.728
0.718
0.711