随笔 - 100  文章 - 50  trackbacks - 0
<2017年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

我收藏的一些文章!

搜索

  •  

最新评论

阅读排行榜

评论排行榜

作为一个新员工,一个首要的工作就是阅读别人的代码,阅读代码的诸多好处就不说了,我就直奔主题,通过预读代码,发现了几种实现两个不同类型的Bean之间实现值复制的几种方式,上网查询后发现性能上会有差异,于是就萌生自己深入了解几种实现的想法。第一步就是先本着实事求是的原则去探求一下大家总结出来的性能差异是否正确。

 
       比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的BeanCopier。做法是在Eclipse新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:
       一个FromBean和一个ToBean,两个的代码基本上一样,除了类名称不一样,所以只是贴出来了一份。
       
public class FromBean {
    private String name;
    private int age;
    private String address;
    private String idno;
    private double money;
 
    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;
            }
        };
        //另外一种BeanCopier缓存实现,效率更高
        IMethodCallBack cglibCB2 = new IMethodCallBack() {
             private ConcurrentHashMap<String,BeanCopier> cache=new ConcurrentHashMap<String, BeanCopier>();

            @Override
            public String getMethodName() {
                return "BeanCopier.create";
            }

            @Override
            public ToBean callMethod(FromBean frombean) throws Exception {
                ToBean toBean = new ToBean();
               return copyBeanProperties(sourceObj, target, false);
            }

           public  <T> T copyBeanProperties(Object sourceObj, T target, boolean useConverter) {
                if(sourceObj==null||target==null) return null;
                String key=sourceObj.getClass().getSimpleName()+target.getClass().getSimpleName();
                BeanCopier copier = cache.get(key);
                if(copier==null){
                     copier=createBeanCopier(sourceObj.getClass(), target.getClass(), useConverter, key);
                }
                copier.copy(sourceObj, target, null);
                return target;
            }

            @SuppressWarnings({"rawtypes" })
            private  BeanCopier createBeanCopier(Class sourceClass,Class targetClass,boolean useConverter,String cacheKey){
                BeanCopier copier = BeanCopier.create(sourceClass,targetClass, useConverter);
                cache.putIfAbsent(cacheKey, copier);
                return copier;
                }  
        };

        // 数量较少的时候,测试性能
        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.copyProperties54575053.666675.366666667
PropertyUtils.copyProperties44440.4
org.springframework.beans.BeanUtils.copyProperties121011111.1
BeanCopier.create00000

 

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

 

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


http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html

posted on 2017-07-25 21:53 fly 阅读(238) 评论(0)  编辑  收藏 所属分类: java学习

只有注册用户登录后才能发表评论。


网站导航: