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

原文:http://www.open-open.com/lib/view/open1404998048200.html


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

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

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

比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的 BeanCopier。做法是在Eclipse新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:
       一个FromBean和一个ToBean,两个的代码基本上一样,除了类名称不一样,所以只是贴出来了一份。
  1. public class FromBean {
  2. private String name;
  3. private int age;
  4. private String address;
  5. private String idno;
  6. private double money;
  7. public double getMoney() {
  8. return money;
  9. }
  10. public void setMoney(double money) {
  11. this.money = money;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public void setAge(int age) {
  23. this.age = age;
  24. }
  25. public String getAddress() {
  26. return address;
  27. }
  28. public void setAddress(String address) {
  29. this.address = address;
  30. }
  31. public String getIdno() {
  32. return idno;
  33. }
  34. public void setIdno(String idno) {
  35. this.idno = idno;
  36. }
  37. }
一个用于测试的BenchmarkTest类,为了减少重复代码,写了一个策略模式

  1. public class BenchmarkTest {
  2. private int count;
  3. public BenchmarkTest(int count) {
  4. this.count = count;
  5. System.out.println("性能测试" + this.count + "==================");
  6. }
  7. public void benchmark(IMethodCallBack m, FromBean frombean) {
  8. try {
  9. long begin = new java.util.Date().getTime();
  10. ToBean tobean = null;
  11. System.out.println(m.getMethodName() + "开始进行测试");
  12. for (int i = 0; i < count; i++) {
  13. tobean = m.callMethod(frombean);
  14. }
  15. long end = new java.util.Date().getTime();
  16. System.out.println(m.getMethodName() + "耗时" + (end - begin));
  17. System.out.println(tobean.getAddress());
  18. System.out.println(tobean.getAge());
  19. System.out.println(tobean.getIdno());
  20. System.out.println(tobean.getMoney());
  21. System.out.println(tobean.getName());
  22. System.out.println(" ");
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
策略中使用的接口声明
  1. public interface IMethodCallBack {
  2. String getMethodName();
  3. ToBean callMethod(FromBean frombean) throws Exception;
  4. }
使用的测试类

  1. public class TestMain {
  2. /**
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. FromBean fb = new FromBean();
  7. fb.setAddress("北京市朝阳区大屯路");
  8. fb.setAge(20);
  9. fb.setMoney(30000.111);
  10. fb.setIdno("110330219879208733");
  11. fb.setName("测试");
  12. IMethodCallBack beanutilCB = new IMethodCallBack() {
  13. @Override
  14. public String getMethodName() {
  15. return "BeanUtil.copyProperties";
  16. }
  17. @Override
  18. public ToBean callMethod(FromBean frombean) throws Exception {
  19. ToBean toBean = new ToBean();
  20. BeanUtils.copyProperties(toBean, frombean);
  21. return toBean;
  22. }
  23. };
  24. IMethodCallBack propertyCB = new IMethodCallBack() {
  25. @Override
  26. public String getMethodName() {
  27. return "PropertyUtils.copyProperties";
  28. }
  29. @Override
  30. public ToBean callMethod(FromBean frombean) throws Exception {
  31. ToBean toBean = new ToBean();
  32. PropertyUtils.copyProperties(toBean, frombean);
  33. return toBean;
  34. }
  35. };
  36. IMethodCallBack springCB = new IMethodCallBack() {
  37. @Override
  38. public String getMethodName() {
  39. return "org.springframework.beans.BeanUtils.copyProperties";
  40. }
  41. @Override
  42. public ToBean callMethod(FromBean frombean) throws Exception {
  43. ToBean toBean = new ToBean();
  44. org.springframework.beans.BeanUtils.copyProperties(frombean,
  45. toBean);
  46. return toBean;
  47. }
  48. };
  49. IMethodCallBack cglibCB = new IMethodCallBack() {
  50. BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class,
  51. false);
  52. @Override
  53. public String getMethodName() {
  54. return "BeanCopier.create";
  55. }
  56. @Override
  57. public ToBean callMethod(FromBean frombean) throws Exception {
  58. ToBean toBean = new ToBean();
  59. bc.copy(frombean, toBean, null);
  60. return toBean;
  61. }
  62. };
  63. // 数量较少的时候,测试性能
  64. BenchmarkTest bt = new BenchmarkTest(10);
  65. bt.benchmark(beanutilCB, fb);
  66. bt.benchmark(propertyCB, fb);
  67. bt.benchmark(springCB, fb);
  68. bt.benchmark(cglibCB, fb);
  69. // 测试一万次性能测试
  70. BenchmarkTest bt10000 = new BenchmarkTest(10000);
  71. bt10000.benchmark(beanutilCB, fb);
  72. bt10000.benchmark(propertyCB, fb);
  73. bt10000.benchmark(springCB, fb);
  74. bt10000.benchmark(cglibCB, fb);
  75. // 担心因为顺序问题影响测试结果
  76. BenchmarkTest bt1000R = new BenchmarkTest(10000);
  77. bt1000R.benchmark(cglibCB, fb);
  78. bt1000R.benchmark(springCB, fb);
  79. bt1000R.benchmark(propertyCB, fb);
  80. bt1000R.benchmark(beanutilCB, fb);
  81. }
  82. }







posted @ 2019-07-10 13:16  No8g攻城狮  阅读(22)  评论(0编辑  收藏  举报