Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier
2016-03-10 16:05
169 查看
使用的测试类
public class TestMain { /** * @param args */ public static void main(String[] args) { FromBean fb = new FromBean(); fb.setAddress("北京市朝阳区大屯路"); fb.setAge(20); fb.setMoney(30000.111); fb.setIdno("110330219879208733"); fb.setName("测试"); IMethodCallBack beanutilCB = new IMethodCallBack() { @Override public String getMethodName() { return "BeanUtil.copyProperties"; } @Override public ToBean callMethod(FromBean frombean) throws Exception { ToBean toBean = new ToBean(); BeanUtils.copyProperties(toBean, frombean); return toBean; } }; IMethodCallBack propertyCB = new IMethodCallBack() { @Override public String getMethodName() { return "PropertyUtils.copyProperties"; } @Override public ToBean callMethod(FromBean frombean) throws Exception { ToBean toBean = new ToBean(); PropertyUtils.copyProperties(toBean, frombean); return toBean; } }; IMethodCallBack springCB = new IMethodCallBack() { @Override public String getMethodName() { return "org.springframework.beans.BeanUtils.copyProperties"; } @Override public ToBean callMethod(FromBean frombean) throws Exception { ToBean toBean = new ToBean(); org.springframework.beans.BeanUtils.copyProperties(frombean, toBean); return toBean; } }; IMethodCallBack cglibCB = new IMethodCallBack() { BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class, false); @Override public String getMethodName() { return "BeanCopier.create"; } @Override public ToBean callMethod(FromBean frombean) throws Exception { ToBean toBean = new ToBean(); bc.copy(frombean, toBean, null); return toBean; } }; // 数量较少的时候,测试性能 BenchmarkTest bt = new BenchmarkTest(10); bt.benchmark(beanutilCB, fb); bt.benchmark(propertyCB, fb); bt.benchmark(springCB, fb); bt.benchmark(cglibCB, fb); // 测试一万次性能测试 BenchmarkTest bt10000 = new BenchmarkTest(10000); bt10000.benchmark(beanutilCB, fb); bt10000.benchmark(propertyCB, fb); bt10000.benchmark(springCB, fb); bt10000.benchmark(cglibCB, fb); // 担心因为顺序问题影响测试结果 BenchmarkTest bt1000R = new BenchmarkTest(10000); bt1000R.benchmark(cglibCB, fb); bt1000R.benchmark(springCB, fb); bt1000R.benchmark(propertyCB, fb); bt1000R.benchmark(beanutilCB, fb); } }
进行了三次测试,最后的结果如下:
10次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 54 | 57 | 50 | 53.66667 | 5.366666667 |
PropertyUtils.copyProperties | 4 | 4 | 4 | 4 | 0.4 |
org.springframework.beans.BeanUtils.copyProperties | 12 | 10 | 11 | 11 | 1.1 |
BeanCopier.create | 0 | 0 | 0 | 0 | 0 |
10000次测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 241 | 222 | 226 | 229.6667 | 0.022966667 |
PropertyUtils.copyProperties | 92 | 90 | 92 | 91.33333 | 0.009133333 |
org.springframework.beans.BeanUtils.copyProperties | 29 | 30 | 32 | 30.33333 | 0.003033333 |
BeanCopier.create | 1 | 1 | 1 | 1 | 0.1 |
10000次反转测验 | 第一次 | 第二次 | 第三次 | 平均值 | 每次平均值 |
BeanUtil.copyProperties | 178 | 174 | 178 | 176.6667 | 0.017666667 |
PropertyUtils.copyProperties | 91 | 87 | 89 | 89 | 0.0089 |
org.springframework.beans.BeanUtils.copyProperties | 21 | 21 | 21 | 21 | 0.0021 |
BeanCopier.create | 0 | 1 | 1 | 0.666667 | 6.66667E-05 |
不过需要注意的是,Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整体的性能还是很好,不过奇怪的是10000次反而比10次少,而且后面的反转1万次反而耗时最少,进行多次测试效果也是如此。 从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。
10次 | 10000次 | 10000次反转 | |
BeanCopier.create | 41 | 28 | 10 |
http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html
test.7z (1.5 KB)
下载次数: 0
相关文章推荐
- SEVERE: ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
- Apache Thrift - 可伸缩的跨语言服务开发框架
- Apache Spark探秘:Spark Shuffle实现
- 【Apache】apache简单配置URL重写规则
- Apache Commons Exec
- Apache common pool2 对象池
- Apache Tez:一个运行在YARN之上支持DAG作业的计算框架
- Apache Camel框架入门示例
- Apache Shiro 学习
- Apache Mahout 简介
- 运行问题,如何修改APACHE的监听端口和密码
- apache commons工具集简介
- Apache Kylin在云海的实践
- Apache HBase Write Path
- ubuntu 实现apache2+python+cgi
- 源码安装apache2.4遇到的问题
- Apache_日志分类及作用
- apache配置一个域名读取多个路径代码(包括主干和分支)
- Apache Mahout 简介 通过可伸缩、商业友好的机器学习来构建智能应用程序
- win7(32/64)+apache2.4+php5.5+mysql5.6 环境搭建配置