Bean複製的幾種框架性能比較

做爲一個新員工,一個首要的工做就是閱讀別人的代碼,閱讀代碼的諸多好處就不說了,我就直奔主題,經過預讀代碼,發現了幾種實現兩個不一樣類型的Bean之 間實現值複製的幾種方式,上網查詢後發現性能上會有差別,因而就萌生本身深刻了解幾種實現的想法。第一步就是先本着實事求是的原則去探求一下你們總結出來 的性能差別是否正確。java

 

       比較的是四種複製的方式,分別爲Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的 BeanCopier。作法是在Eclipse新建了一個Project,專門用於專門測試幾種代碼的性能。具體的代碼以下:spring

       一個FromBean和一個ToBean,兩個的代碼基本上同樣,除了類名稱不同,因此只是貼出來了一份。緩存

       

public class FromBean {ide

    private String name;性能

    private int age;測試

    private String address;this

    private String idno;spa

    private double money;code

 

    public double getMoney() {接口

        return money;

    }

 

    public void setMoney(double money) {

        this.money = money;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    public String getAddress() {

        return address;

    }

 

    public void setAddress(String address) {

        this.address = address;

    }

 

    public String getIdno() {

        return idno;

    }

 

    public void setIdno(String idno) {

        this.idno = idno;

    }

 

}

  一個用於測試的BenchmarkTest類,爲了減小重複代碼,寫了一個策略模式

     

複製代碼

複製代碼

public class BenchmarkTest {
    private int count;

    public BenchmarkTest(int count) {
        this.count = count;
        System.out.println("性能測試" + this.count + "==================");
    }

    public void benchmark(IMethodCallBack m, FromBean frombean) {
        try {
            long begin = new java.util.Date().getTime();
            ToBean tobean = null;
            System.out.println(m.getMethodName() + "開始進行測試");
            for (int i = 0; i < count; i++) {

                tobean = m.callMethod(frombean);

            }
            long end = new java.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();
        }
    }
}

複製代碼

複製代碼

 策略中使用的接口聲明

 

複製代碼

複製代碼

public interface IMethodCallBack {

    String getMethodName();

    ToBean callMethod(FromBean frombean)  throws Exception;

}

複製代碼

複製代碼

 

使用的測試類

複製代碼

複製代碼

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

      性能測試就到這裏,數據也展現如上,後續會繼續編寫剩餘兩篇文章,這一片關注性能,後面的一篇是就每種方式的使用上的差別進行詳解,最後一篇想進行探討是什麼早就了這四種方式的性能差別。

相關文章
相關標籤/搜索