您的位置:首页 > 运维架构 > Apache

比较Spring、apache和Property的beanUtils效率的案例

2015-10-30 13:52 501 查看
平时使用的bean有许多,我们来比较一下;

1.首先建立FromBean和ToBean两个bean类,

package com.testbeanutils;

/**

* @author zhangjiamei

* @createtime 2015年10月28日

* @version 0.1

* @instruction 用于测试的bean类

*/

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;

}

}

2.定义出接口方法执行的接口,

package com.testbeanutils;

public interface IMethodCallBack {

String getMethodName();

ToBean callMethod(FromBean frombean) throws Exception;

}

3.写测试类;

package com.testbeanutils;

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

}

}

}

4.开始测试.

package com.main;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.PropertyUtils;

import com.testbeanutils.BenchmarkTest;

import com.testbeanutils.FromBean;

import com.testbeanutils.IMethodCallBack;

import com.testbeanutils.ToBean;

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;

}

};

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

BenchmarkTest bt = new BenchmarkTest(10);

bt.benchmark(beanutilCB, fb);

bt.benchmark(propertyCB, fb);

bt.benchmark(springCB, fb);

// 测试一万次性能测试

/*BenchmarkTest bt10000 = new BenchmarkTest(10000);

bt10000.benchmark(beanutilCB, fb);

bt10000.benchmark(propertyCB, fb);

bt10000.benchmark(springCB, fb);*/

// 担心因为顺序问题影响测试结果

/*BenchmarkTest bt1000R = new BenchmarkTest(10000);

bt1000R.benchmark(springCB, fb);

bt1000R.benchmark(propertyCB, fb);

bt1000R.benchmark(beanutilCB, fb);*/

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: