Last active
August 29, 2015 14:21
-
-
Save valdo404/a8d82ee2b6a86d840783 to your computer and use it in GitHub Desktop.
proxy jmh
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 bench; | |
import java.lang.reflect.InvocationHandler; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.TimeUnit; | |
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.openjdk.jmh.annotations.*; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
@SuppressWarnings("unchecked") | |
@State(Scope.Benchmark) | |
public class ProxifierPerformance { | |
final Enhancer enhancer = createEnhancer(); | |
Callable<Integer> cglib = createCglibProxy(); | |
Callable<Integer> jdk = createJdkProxy(); | |
Callable<Integer> javassist = createJavassistProxy(); | |
private Enhancer createEnhancer() { | |
Enhancer enhancer = new Enhancer(); | |
enhancer.setInterfaces(new Class[] { Callable.class }); | |
enhancer.setCallback(new CglibMethodInterceptor(new Counter())); | |
return enhancer; | |
} | |
public static void main(String[] args) throws Exception { | |
Options opt = new OptionsBuilder() | |
.include(ProxifierPerformance.class.getSimpleName()) | |
.warmupIterations(5) | |
.measurementIterations(10) | |
.forks(1) | |
.build(); | |
new Runner(opt).run(); | |
//new ProxifierPerformance().jdkCall(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void jdkCreateAndCall() throws Exception { | |
createJdkProxy().call(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void jdkCall() throws Exception { | |
jdk.call(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void javassistCreateAndCall() throws Exception { | |
createJavassistProxy().call(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void javassistCall() throws Exception { | |
javassist.call(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void cglibCreateAndCall() throws Exception { | |
createCglibProxy(); | |
} | |
@Benchmark | |
@BenchmarkMode({Mode.AverageTime}) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
public void cglibCall() throws Exception { | |
cglib.call(); | |
} | |
private Callable<Integer> createJdkProxy() { | |
return (Callable<Integer>) Proxy.newProxyInstance(Callable.class.getClassLoader(), | |
new Class[] { Callable.class }, new JdkInvocationHandler(new Counter())); | |
} | |
private Callable<Integer> createJavassistProxy() { | |
ProxyFactory proxyFactory = new ProxyFactory(); | |
proxyFactory.setInterfaces(new Class[] { Callable.class }); | |
Class<?> proxyClass = proxyFactory.createClass(); | |
Callable<Integer> proxy = null; | |
try { | |
proxy = (Callable<Integer>) proxyClass.newInstance(); | |
((ProxyObject) proxy).setHandler(new JavassistMethodInterceptor(new Counter())); | |
} catch (InstantiationException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return proxy; | |
} | |
private Callable<Integer> createCglibProxy() { | |
return (Callable<Integer>) enhancer.create(); | |
} | |
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