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

来源:互联网 发布:io是什么域名 编辑:程序博客网 时间:2024/04/27 18:12
 比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的 BeanCopier。做法是在Eclipse新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:
       一个FromBean和一个ToBean,两个的代码基本上一样,除了类名称不一样,所以只是贴出来了一份。
       
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
publicclass FromBean {
    privateString name;
    privateint age;
    privateString address;
    privateString idno;
    privatedouble money;
  
    publicdouble getMoney() {
        returnmoney;
    }
  
    publicvoid setMoney(doublemoney) {
        this.money = money;
    }
  
    publicString getName() {
        returnname;
    }
  
    publicvoid setName(String name) {
        this.name = name;
    }
  
    publicint getAge() {
        returnage;
    }
  
    publicvoid setAge(intage) {
        this.age = age;
    }
  
    publicString getAddress() {
        returnaddress;
    }
  
    publicvoid setAddress(String address) {
        this.address = address;
    }
  
    publicString getIdno() {
        returnidno;
    }
  
    publicvoid setIdno(String idno) {
        this.idno = idno;
    }
  
}

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

     

 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
publicclass BenchmarkTest {
    privateint count;
 
    publicBenchmarkTest(intcount) {
        this.count = count;
        System.out.println("性能测试"this.count + "==================");
    }
 
    publicvoid benchmark(IMethodCallBack m, FromBean frombean) {
        try{
            longbegin = newjava.util.Date().getTime();
            ToBean tobean = null;
            System.out.println(m.getMethodName() + "开始进行测试");
            for(inti = 0; i < count; i++) {
 
                tobean = m.callMethod(frombean);
 
            }
            longend = 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();
        }
    }
}
 

 策略中使用的接口声明

 

?
1
2
3
4
5
6
7
publicinterface IMethodCallBack {
 
    String getMethodName();
 
    ToBean callMethod(FromBean frombean)  throwsException;
 
}

使用的测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
publicclass TestMain {
 
    /**
     * @param args
     */
    publicstatic void main(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
            publicToBean callMethod(FromBean frombean) throwsException {
 
                ToBean toBean = newToBean();
                BeanUtils.copyProperties(toBean, frombean);
                returntoBean;
            }
        };
 
        IMethodCallBack propertyCB = newIMethodCallBack() {
 
            @Override
            publicString getMethodName() {
                return"PropertyUtils.copyProperties";
            }
 
            @Override
            publicToBean 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
            publicToBean 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
            publicToBean callMethod(FromBean frombean) throwsException {
                ToBean toBean = newToBean();
                bc.copy(frombean, toBean, null);
                returntoBean;
            }
        };
 
        // 数量较少的时候,测试性能
        BenchmarkTest bt = newBenchmarkTest(10);
        bt.benchmark(beanutilCB, fb);
        bt.benchmark(propertyCB, fb);
        bt.benchmark(springCB, fb);
        bt.benchmark(cglibCB, fb);
 
        // 测试一万次性能测试
        BenchmarkTest bt10000 = newBenchmarkTest(10000);
        bt10000.benchmark(beanutilCB, fb);
        bt10000.benchmark(propertyCB, fb);
        bt10000.benchmark(springCB, fb);
        bt10000.benchmark(cglibCB, fb);
 
        // 担心因为顺序问题影响测试结果
        BenchmarkTest bt1000R = newBenchmarkTest(10000);
        bt1000R.benchmark(cglibCB, fb);
        bt1000R.benchmark(springCB, fb);
        bt1000R.benchmark(propertyCB, fb);
        bt1000R.benchmark(beanutilCB, fb);
 
    }
 
}

 

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

10次测验第一次第二次第三次平均值每次平均值BeanUtil.copyProperties54575053.666675.366666667PropertyUtils.copyProperties44440.4org.springframework.beans.BeanUtils.copyProperties121011111.1BeanCopier.create00000

 

10000次测验第一次第二次第三次平均值每次平均值BeanUtil.copyProperties241222226229.66670.022966667PropertyUtils.copyProperties92909291.333330.009133333org.springframework.beans.BeanUtils.copyProperties29303230.333330.003033333BeanCopier.create11110.1

 

10000次反转测验第一次第二次第三次平均值每次平均值BeanUtil.copyProperties178174178176.66670.017666667PropertyUtils.copyProperties918789890.0089org.springframework.beans.BeanUtils.copyProperties212121210.0021BeanCopier.create0110.6666676.66667E-05

 

      不过需要注意的是,Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整 体的性能还是很好,不过奇怪的是10000次反而比10次少,而且后面的反转1万次反而耗时最少,进行多次测试效果也是如此。    从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损 耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。PropertyUtils的性能 相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。

 10次10000次10000次反转BeanCopier.create412810

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

0 0
原创粉丝点击