java beanutils 性能_Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)...

本文通过BenchmarkTest类对比了Apache BeanUtils、PropertyUtils、Spring BeanUtils和Cglib BeanCopier在Bean复制性能上的差异。测试结果显示,Cglib的BeanCopier性能最佳,尤其是在大量复制时接近零损耗。Spring BeanUtils在数量增加时表现出较好的性能,而PropertyUtils性能稳定,BeanUtils性能较差。
摘要由CSDN通过智能技术生成

一个用于测试的BenchmarkTest类,为了减少重复代码,写了一个策略模式

48304ba5e6f9fe08f3fa1abda7d326ab.png

public classBenchmarkTest {

private intcount;

public BenchmarkTest(intcount) {

this.count =count;

System.out.println("性能测试" + this.count + "==================");

}

public voidbenchmark(IMethodCallBack m, FromBean frombean) {

try{

long begin = newjava.util.Date().getTime();

ToBean tobean = null;

System.out.println(m.getMethodName() + "开始进行测试");

for (int i = 0; i < count; i++) {

tobean =m.callMethod(frombean);

}

long end = newjava.util.Date().getTime();

System.out.println(m.getMethodName() + "耗时" + (end -begin));

System.out.println(tobean.getAddress());

System.out.println(tobean.getAge());

System.out.println(tobean.getIdno());

System.out.println(tobean.getMoney());

System.out.println(tobean.getName());

System.out.println(" ");

} catch(Exception e) {

e.printStackTrace();

}

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

策略中使用的接口声明

48304ba5e6f9fe08f3fa1abda7d326ab.png

public interfaceIMethodCallBack {

String getMethodName();

ToBean callMethod(FromBean frombean) throwsException;

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

使用的测试类

48304ba5e6f9fe08f3fa1abda7d326ab.png

public classTestMain {

/*** @paramargs

*/

public static voidmain(String[] args) {

FromBean fb = newFromBean();

fb.setAddress("北京市朝阳区大屯路");

fb.setAge(20);

fb.setMoney(30000.111);

fb.setIdno("110330219879208733");

fb.setName("测试");

IMethodCallBack beanutilCB = newIMethodCallBack() {

@Override

publicString getMethodName() {

return "BeanUtil.copyProperties";

}

@Override

public ToBean callMethod(FromBean frombean) throwsException {

ToBean toBean = newToBean();

BeanUtils.copyProperties(toBean, frombean);

returntoBean;

}

};

IMethodCallBack propertyCB = newIMethodCallBack() {

@Override

publicString getMethodName() {

return "PropertyUtils.copyProperties";

}

@Override

public ToBean callMethod(FromBean frombean) throwsException {

ToBean toBean = newToBean();

PropertyUtils.copyProperties(toBean, frombean);

returntoBean;

}

};

IMethodCallBack springCB = newIMethodCallBack() {

@Override

publicString getMethodName() {

return "org.springframework.beans.BeanUtils.copyProperties";

}

@Override

public ToBean callMethod(FromBean frombean) throwsException {

ToBean toBean = newToBean();

org.springframework.beans.BeanUtils.copyProperties(frombean,

toBean);

returntoBean;

}

};

IMethodCallBack cglibCB = newIMethodCallBack() {

BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class,

false);

@Override

publicString getMethodName() {

return "BeanCopier.create";

}

@Override

public ToBean callMethod(FromBean frombean) throwsException {

ToBean toBean = newToBean();

bc.copy(frombean, toBean, null);

returntoBean;

}

};

// 数量较少的时候,测试性能

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);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

进行了三次测试,最后的结果如下:

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

性能测试就到这里,数据也展示如上,后续会继续编写剩余两篇文章,这一片关注性能,后面的一篇是就每种方式的使用上的差异进行详解,最后一篇想进行探讨是什么早就了这四种方式的性能差异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值