天天看點

cglib相關性能測試對比正題:最後

背景: 

繼上一篇文章 cglib源碼學習交流 

很多同學提出,因中文文檔缺乏,導緻對文章中的介紹看的不是很明白,更多的隻是想了解具體的使用即可。是以趁勢寫了這篇博文,主要是将cglib中的幾個工具類和常用的reflect ,beanutils做一個對比,順便也介紹一下cglib的相關用法,一舉兩得,望大家多多支援。

 說明: 該copybean基本包含了java的所有原型對象,基本對象,和常用的bigdecimal,biginteger,總共17個屬性。

   定義一個testcallback接口。

 定義測試的模闆方法 

   private static final decimalformat integerformat = new decimalformat("#,###");

 說明:

為了測試更加精确,避免因為在一次的循環中進行處理,jvm記憶體,gc,class裝載對測試的影響,有一個warmup的過程,先執行少量的測試方法,這裡是執行10次

避免jvm記憶體gc對測試id影響,這裡有restorejvm強制進行一次jvm gc

   private static void restorejvm() {

測試環境說明:

作業系統 linux ccbu-156-49 2.6.18-131.el5.customxen #1 smp tue sep 15 15:46:11 cst 2009 x86_64 x86_64 x86_64 gnu/linux

虛拟8cpu , 5g記憶體

jdk  1.6.0_18

jvm 參數 

beancopier  (cglib)

propertyutils (apache-common)

beanutils (apache-common)

1.  beancopier  (cglib)

2. propertyutils (apache-common) 

3. beanutils (apache-common)

測試結果: 

測試次數:testcount = 1000 * 1000 = 100萬次

beancopier total cost=36,626,000ns , each cost=36ns

propertyutils total cost=18,173,767,000ns , each cost=18173ns

beanutils total cost=31,236,079,000ns , each cost=31236ns

從這個結果可以看出, beancopier是propertyutils的504倍, propertyutils是beanutils的1.71倍, beancopier是propertyutils的861.84倍,差了近3個數量級。

bulkbean (cglib)

beanmap (cglib)

fastclass/fastmethod  (cglib)

未處理的jdk reflect (jdk)

處理的jdk reflect (jdk)

1. bulkbean 

2. beanmap

 3. fastclass/fastmethod

4.  未處理的jdk reflect

5. 處理過的jdk reflect

測試結果:  

測試次數:testcount = 1000 * 1000 * 100 = 1億次

bulkbean total cost=2,125,759,000ns , each cost=21ns

beanmap total cost=2,730,912,000ns , each cost=27ns

fastclass total cost=2,576,470,000ns , each cost=25ns

未處理過的reflect total cost=2,882,755,000ns , each cost=28ns

處理過的reflect total cost=2,792,828,000ns , each cost=27ns

測試結果,性能相差不多,差距不大,這也可以說明jdk對reflect調用的優化已經做的很棒了。

測試資料僅拱參考,最後測試代碼可見附件。測試方法如存在問題,歡迎拍磚