Spring中JDK动态代理和CGLIB动态代理的性能比较

有空自己也做个实验,急于得到结论就先留个参考;
转自[url]http://budairenqin.iteye.com/blog/1500366[/url]
我们知道,service由于要做事务的包装,需要创建代理对象,spring中使用JDK动态代理或者CGLIB动态代理来创建代理对象,据说JDK动态代理创建对象的时间快于CGLIB,但是性能比CGLIB差(接下来我会测试这个观点),所以我得出以下结论:
1.spring在bean的scope为prototype的情况下,因为是延迟实例化bean,所以最好使用JDK的API创建代理对象;
2.反之对于singleton对象,spring默认是容器启动时就初始化bean,最好使用CGLIB来创建对象

把service配置成singleton我觉得性能方面显然要更好些,如果非要将request穿透到service层,是不是可以考虑用ThreadLocal?

在测试之前,我们先确定spring是以什么方式使用JDK动态代理和CGLIB的,如图:
JDK动态代理:
点击查看原始大小图片


CGLIB动态代理:
点击查看原始大小图片

接下来对测试下JDK动态代理和CGLIB动态代理的性能(CGLIB测试代码也和spring一样使用MethodInterceptor)

先贴上测试代码
Java代码 收藏代码
public interface CountService {
int count();
}

Java代码 收藏代码
public class CountServiceImpl implements CountService {
private int count = 0;

public int count() {
return ++count;
}
}

Java代码 收藏代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.text.DecimalFormat;

import net.sf.cglib.core.DefaultGeneratorStrategy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

@SuppressWarnings("unused")
public class DynamicProxyPerformanceTest {

public static void main(String[] args) throws Exception {
CountService delegate = new CountServiceImpl();

long time = System.currentTimeMillis();
CountService jdkProxy = createJdkDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("Create JDK Proxy: " + time + " ms");

time = System.currentTimeMillis();
CountService cglibProxy = createCglibDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("Create CGLIB Proxy: " + time + " ms");

for (int i = 0; i < 3; i++) {
test(jdkProxy, "Run JDK Proxy: ");
test(cglibProxy, "Run CGLIB Proxy: ");
System.out.println("-------------------");
}
}

private static void test(CountService service, String label) throws Exception {
service.count(); // warm up
int count = 10000000;
long time = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
service.count();
}
time = System.currentTimeMillis() - time;
System.out.println(label + time + " ms, " + new DecimalFormat().format(count * 1000 / time) + " t/s");
}

private static CountService createJdkDynamicProxy(final CountService delegate) {
CountService jdkProxy = (CountService) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
new Class[] { CountService.class }, new JdkHandler(delegate));

// 反汇编字节码用,测试的时候注释掉这段代码,不然影响测试结果
// 下面一行代码参照java.lang.reflect.Proxy
// byte[] proxyClassFile =
// sun.misc.ProxyGenerator.generateProxyClass(
// jdkProxy.getClass().getName(), jdkProxy.getClass().getInterfaces());
// try {
// FileOutputStream fos =
// new FileOutputStream(new File(jdkProxy.getClass().getName() + ".class"));
// fos.write(proxyClassFile, 0, proxyClassFile.length);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
return jdkProxy;
}

private static class JdkHandler implements InvocationHandler {

final Object delegate;

JdkHandler(Object delegate) {
this.delegate = delegate;
}

public Object invoke(Object object, Method method, Object[] objects) throws Throwable {
return method.invoke(delegate, objects);
}
}

private static CountService createCglibDynamicProxy(final CountService delegate) throws Exception {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(CountServiceImpl.class);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return proxy.invokeSuper(obj, args);
}
});
CountServiceImpl cglibProxy = (CountServiceImpl) enhancer.create();
// 反汇编字节码用,测试的时候注释掉这段代码,不然影响测试结果
// 下面一行代码参照net.sf.cglib.core.AbstractClassGenerator类中byte[] b = strategy.generate(this);
// byte[] proxyClassFile = new DefaultGeneratorStrategy().generate(enhancer);
// try {
// FileOutputStream fos =
// new FileOutputStream(new File(cglibProxy.getClass().getName() + ".class"));
// fos.write(proxyClassFile, 0, proxyClassFile.length);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
return cglibProxy;
}
}


数据为执行三次,每次调用一千万次代理方法的结果
测试环境1:
JDK:fastdebug1.6
CGLIB:和spring2.0.6 使用同样的cglib-nodep-2.1_3.jar
CPU:P8400 2.53GHz 2.53GHz
测试结果1:
Java代码 收藏代码
Create JDK Proxy: 13 ms
Create CGLIB Proxy: 201 ms
Run JDK Proxy: 1571 ms, 897,559 t/s
Run CGLIB Proxy: 824 ms, 1,711,244 t/s
-------------------
Run JDK Proxy: 1519 ms, 928,285 t/s
Run CGLIB Proxy: 576 ms, 2,448,030 t/s
-------------------
Run JDK Proxy: 1546 ms, 912,073 t/s
Run CGLIB Proxy: 590 ms, 2,389,941 t/s
-------------------

CGLIB创建代理对象速度大概比JDK Proxy慢15倍,执行速度是JDK Proxy的2倍左右

测试环境2:
JDK:fastdebug1.7
CGLIB:和spring2.0.6 使用同样的cglib-nodep-2.1_3.jar
CPU:P8400 2.53GHz 2.53GHz
测试结果2:
Java代码 收藏代码
Create JDK Proxy: 14 ms
Create CGLIB Proxy: 204 ms
Run JDK Proxy: 1608 ms, 876,906 t/s
Run CGLIB Proxy: 529 ms, 2,665,530 t/s
-------------------
Run JDK Proxy: 1591 ms, 886,276 t/s
Run CGLIB Proxy: 405 ms, 3,481,642 t/s
-------------------
Run JDK Proxy: 1624 ms, 868,266 t/s
Run CGLIB Proxy: 405 ms, 3,481,642 t/s
-------------------

CGLIB创建代理对象速度大概比JDK Proxy慢15倍,执行速度是JDK Proxy的4倍左右

测试环境3:
JDK:jdk1.6.0_21
CGLIB:和spring2.0.6 使用同样的cglib-nodep-2.1_3.jar
CPU:P8400 2.53GHz 2.53GHz
测试结果3:
Java代码 收藏代码
Create JDK Proxy: 8 ms
Create CGLIB Proxy: 99 ms
Run JDK Proxy: 911 ms, 1,547,821 t/s
Run CGLIB Proxy: 435 ms, 3,241,529 t/s
-------------------
Run JDK Proxy: 870 ms, 1,620,764 t/s
Run CGLIB Proxy: 399 ms, 3,533,998 t/s
-------------------
Run JDK Proxy: 894 ms, 1,577,254 t/s
Run CGLIB Proxy: 404 ms, 3,490,260 t/s
-------------------

CGLIB创建代理对象速度大概比JDK Proxy慢10倍以上,执行速度是JDK Proxy的2倍左右

测试环境4:
JDK:jdk1.7.0_02
CGLIB:和spring2.0.6 使用同样的cglib-nodep-2.1_3.jar
CPU:P8400 2.53GHz 2.53GHz
测试结果4:
Java代码 收藏代码
Create JDK Proxy: 43 ms
Create CGLIB Proxy: 129 ms
Run JDK Proxy: 940 ms, 1,500,069 t/s
Run CGLIB Proxy: 299 ms, 4,715,937 t/s
-------------------
Run JDK Proxy: 921 ms, 1,531,015 t/s
Run CGLIB Proxy: 269 ms, 5,241,878 t/s
-------------------
Run JDK Proxy: 932 ms, 1,512,945 t/s
Run CGLIB Proxy: 265 ms, 5,321,001 t/s
-------------------

CGLIB创建代理对象速度大概比JDK Proxy慢3倍,执行速度是JDK Proxy的3倍以上

字节码比较:
把测试代码中被注释的部分打开,生成class文件后执行javap -c 类名
JDK动态代理生成的字节码:
Java代码 收藏代码
public final int count() throws ;
Code:
0: aload_0
1: getfield #16 // Field java/lang/reflect/Proxy.h:Ljava/lang/reflect/InvocationHandler;
4: aload_0
5: getstatic #50 // Field m3:Ljava/lang/reflect/Method;
8: aconst_null
9: invokeinterface #28, 4 // InterfaceMethod java/lang/reflect/InvocationHandler.invoke:(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;
14: checkcast #52 // class java/lang/Integer
17: invokevirtual #55 // Method java/lang/Integer.intValue:()I
20: ireturn
21: athrow
22: astore_1
23: new #42 // class java/lang/reflect/UndeclaredThrowableException
26: dup
27: aload_1
28: invokespecial #45 // Method java/lang/reflect/UndeclaredThrowableException."<init>":(Ljava/lang/Throwable;)V
31: athrow
Exception table:
from to target type
0 21 21 Class java/lang/Error
0 21 21 Class java/lang/RuntimeException
0 21 22 Class java/lang/Throwable


CGLIB生成的字节码:
Java代码 收藏代码
public final int count();
Code:
0: aload_0
1: getfield #37 // Field CGLIB$CALLBACK_0:Lnet/sf/cglib/proxy/MethodInterceptor;
4: dup
5: ifnonnull 17
8: pop
9: aload_0
10: invokestatic #41 // Method CGLIB$BIND_CALLBACKS:(Ljava/lang/Object;)V
13: aload_0
14: getfield #37 // Field CGLIB$CALLBACK_0:Lnet/sf/cglib/proxy/MethodInterceptor;
17: dup
18: ifnull 52
21: aload_0
22: getstatic #43 // Field CGLIB$count$0$Method:Ljava/lang/reflect/Method;
25: getstatic #45 // Field CGLIB$emptyArgs:[Ljava/lang/Object;
28: getstatic #47 // Field CGLIB$count$0$Proxy:Lnet/sf/cglib/proxy/MethodProxy;
31: invokeinterface #53, 5 // InterfaceMethod net/sf/cglib/proxy/MethodInterceptor.intercept:(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;Lnet/sf/cglib/proxy/MethodProxy;)Ljava/lang/Object;
36: dup
37: ifnonnull 45
40: pop
41: iconst_0
42: goto 51
45: checkcast #55 // class java/lang/Number
48: invokevirtual #58 // Method java/lang/Number.intValue:()I
51: ireturn
52: aload_0
53: invokespecial #35 // Method CountServiceImpl.count:()I
56: ireturn
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值