测试环境:
JDK1.6.29
CPU:I7 2.80 双核四线程
单位为纳秒
测试对象
1)spring包中的 BeanUtil 采用反射实现
2)cglib包中的 Beancopier 采用动态字节码实现
1.对象包含所有的基本类型
public class TestBeanSource1 {
private String str;
private byte b;
private short s;
private int i;
private long l;
private double d;
private float f;
private char c;
private boolean bb;
private List<String> ll;
private Map<String, String> map;
场景一:copy次数 :1
spring.beanUtil : costs : 57
cglib : costs : 0
场景二:copy次数 :10000
spring.beanUtil : total1:327
cglib : total2:55
场景三:copy次数 :100000
spring.beanUtil : total1:839
cglib : total2:392
场景四:copy次数 :1000000
spring.beanUtil : total1:5609
cglib : total2:3436
场景五:copy次数 :10000000
spring.beanUtil : total1:53597 avg: 0
cglib : total2:32668 avg: 0
从数据上我们可以看出,在小数量级上,cglib的性能优势明显,但是随着数据量增大,两者的差距基本在2:1。
取一个业务线上的实际对象做测试:
对象属性:
public class TestBeanSource2 {
private String userPW;
private String userEmail;
private String userNickName;
private Integer userCity;
private Integer userSource;
private Integer userPower;
private String userFace;
private Integer userVote;
private Integer userVoteGood;
private Integer userLogin;
private Integer userDailyLogin;
private Integer userHits;
private String userIP;
private Boolean isRefusePromo;
private Integer regInviteNo;
private Boolean isRefuseCard;
private Integer emailOption;
private Integer groupTotal;
private Integer userStatus;
private String userTags;
private String permaLink;
private String rawURL;
private Integer freeBookCity;
private Integer manaScore;
private Integer dcash;
private Integer userBigFace;
private Integer groupUserScore;
private Integer oldGroupUserScore;
private Integer manaOfReview;
private Integer manaOfShop;
private Integer manaOfPic;
private Integer manaOfOther;
private String mobileNO;
private Integer mobileNOStatus;
private Integer manaOfUserBase;
private Integer userEmailVerifyStatus;
场景一:copy次数 :1
spring.beanUtil : costs : 100160495
cglib : costs : 169295
场景二:copy次数 :10000
spring.beanUtil : 447733683 avg: 44
cglib : 56684493 avg: 5
场景三:copy次数 :100000
spring.beanUtil : 1278141962 avg: 127
cglib : 392142487 avg: 39
场景四:copy次数 :1000000
spring.beanUtil : 9722299911 avg: 972
cglib : 3292135548 avg: 329
场景五:copy次数 :10000000
spring.beanUtil : total1:91461239886 avg: 9146
cglib : total2:32762515523 avg: 3276
当我们拿实际的业务DO来做测试时候,规律同上,随着数量级的增大,两者之间的差距在降低,基本维持在3:1左右。
对比第一个测试,我们可以看出,随着业务对象的复杂度增高,cglib的性能相对反射之间的差距会越来越大。
故推荐使用cglib的beanCopier
cglib的简单使用:
public class CglibCopier extends BeanCopyTemplate {
private static final BeanCopier beanCopier2 = BeanCopier.create(TestBeanSource1.class,TestBeanTarget1.class, false);
@Override
public void handle(TestBeanSource2 source, TestBeanTarget2 target) {
beanCopier2.copy(source, target, null);
}
}
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐