Cglib 与 JDK动态代理的运行性能对比_第1页
Cglib 与 JDK动态代理的运行性能对比_第2页
Cglib 与 JDK动态代理的运行性能对比_第3页
Cglib 与 JDK动态代理的运行性能对比_第4页
Cglib 与 JDK动态代理的运行性能对比_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文格式为Word版,下载可任意编辑——Cglib与JDK动态代理的运行性能对比Cglib与JDK动态代理的运行性能比较

都说Cglib创立的动态代理的运行性能比JDK动态代理能高出大约10倍,今日抱着怀疑精神验证了一下,发现状况有所不同,遂贴出试验结果,以供参考和探讨。代码很简单,首先,定义一个Test接口,和一个实现TestImpl。Test接口仅定义一个方法test,对传入的int参数加1后返回。代码如下:

packagemy.test;

publicinterfaceTest{

publicinttest(inti);}

packagemy.test;

publicclassTestImplimplementsTest{publicinttest(inti){returni+1;}}

然后,定义了三种代理的实现:装饰者模式实现的代理(decorator),JDK动态代理(dynamicproxy)和Cglib动态代理(cglibproxy)。代码如下:

packagemy.test;

publicclassDecoratorTestimplementsTest{privateTesttarget;

publicDecoratorTest(Testtarget){this.target=target;}

publicinttest(inti){returntarget.test(i);}}

packagemy.test;

importjava.lang.reflect.InvocationHandler;importjava.lang.reflect.Method;importjava.lang.reflect.Proxy;

publicclassDynamicProxyTestimplementsInvocationHandler{privateTesttarget;

privateDynamicProxyTest(Testtarget){this.target=target;}

publicstaticTestnewProxyInstance(Testtarget){return(Test)Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(),newClass[]{Test.class},newDynamicProxyTest(target));

}

publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)

throwsThrowable{

returnmethod.invoke(target,args);}}

packagemy.test;

importjava.lang.reflect.Method;

xy.Enhancer;

xy.MethodInterceptor;xy.MethodProxy;

publicclassCglibProxyTestimplementsMethodInterceptor{

privateCglibProxyTest(){}

publicstaticTestnewProxyInstance(ClasstargetInstanceClazz){Enhancerenhancer=newEnhancer();enhancer.setSuperclass(targetInstanceClazz);enhancer.setCallback(newCglibProxyTest());return(Test)enhancer.create();}

publicObjectintercept(Objectobj,Methodmethod,Object[]args,MethodProxyproxy)throwsThrowable{returnproxy.invokeSuper(obj,args);}}

以TestImpl的调用耗时作为基准,对比通过其它三种代理进行调用的耗时。测试代码如下:

packagemy.test;

importjava.util.LinkedHashMap;importjava.util.Map;

publicclassProxyPerfTester{

publicstaticvoidmain(String[]args){//创立测试对象;

TestnativeTest=newTestImpl();

Testdecorator=newDecoratorTest(nativeTest);

TestdynamicProxy=DynamicProxyTest.newProxyInstance(nativeTest);TestcglibProxy=CglibProxyTest.newProxyInstance(TestImpl.class);

//预热一下;

intpreRunCount=10000;

runWithoutMonitor(nativeTest,preRunCount);runWithoutMonitor(decorator,preRunCount);runWithoutMonitor(cglibProxy,preRunCount);runWithoutMonitor(dynamicProxy,preRunCount);

//执行测试;

Maptests=newLinkedHashMap();tests.put(\\nativeTest);tests.put(\decorator);

tests.put(\\dynamicProxy);tests.put(\\cglibProxy);intrepeatCount=3;

intrunCount=1000000;

runTest(repeatCount,runCount,tests);runCount=50000000;

runTest(repeatCount,runCount,tests);}

privatestaticvoidrunTest(intrepeatCount,intrunCount,Maptests){

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论