您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Bean復(fù)制的幾種框架有哪些區(qū)別”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Bean復(fù)制的幾種框架有哪些區(qū)別”吧!
正文
作為一個(gè)員工,最重要的不是編寫(xiě)代碼,而是閱讀代碼,本博主在閱讀代碼的時(shí)候,發(fā)現(xiàn)幾種實(shí)現(xiàn)兩個(gè)不同類(lèi)型的Bean的差異,本著研究的精神,仔細(xì)對(duì)比了Bean復(fù)制的性能差異。
比較的框架分別是Apache的BeanUtils,PropertyUtils,Spring的,BeanUtils,Cglib的BeanCopier。
做法是在idea新建了一個(gè)Project,專(zhuān)門(mén)用于專(zhuān)門(mén)測(cè)試幾種代碼的性能。具體的代碼如下:一個(gè)FromBean和一個(gè)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; } }
一個(gè)用于測(cè)試的BenchmarkTest類(lèi)
public class BenchmarkTest { private int count; public BenchmarkTest(int count) { this.count = count; System.out.println("性能測(cè)試" + 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() + "開(kāi)始進(jìn)行測(cè)試"); for (int i = 0; i < count; i++) { tobean = m.callMethod(frombean); } long end = new java.util.Date().getTime(); System.out.println(m.getMethodName() + "耗時(shí)" + (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(); } } }
對(duì)接口的聲明
public interface IMethodCallBack { String getMethodName(); ToBean callMethod(FromBean frombean) throws Exception; }
使用的測(cè)試類(lèi)
public class TestMain { /** * @param args */ public static void main(String[] args) { FromBean fb = new FromBean(); fb.setAddress("北京市朝陽(yáng)區(qū)大屯路"); fb.setAge(20); fb.setMoney(30000.111); fb.setIdno("110330219879208733"); fb.setName("測(cè)試"); 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; } }; // 數(shù)量較少的時(shí)候,測(cè)試性能 BenchmarkTest bt = new BenchmarkTest(10); bt.benchmark(beanutilCB, fb); bt.benchmark(propertyCB, fb); bt.benchmark(springCB, fb); bt.benchmark(cglibCB, fb); // 測(cè)試一萬(wàn)次性能測(cè)試 BenchmarkTest bt10000 = new BenchmarkTest(10000); bt10000.benchmark(beanutilCB, fb); bt10000.benchmark(propertyCB, fb); bt10000.benchmark(springCB, fb); bt10000.benchmark(cglibCB, fb); // 擔(dān)心因?yàn)轫樞騿?wèn)題影響測(cè)試結(jié)果 BenchmarkTest bt1000R = new BenchmarkTest(10000); bt1000R.benchmark(cglibCB, fb); bt1000R.benchmark(springCB, fb); bt1000R.benchmark(propertyCB, fb); bt1000R.benchmark(beanutilCB, fb); } }
測(cè)試的結(jié)果如下
不過(guò)需要注意的是,Cglib在測(cè)試的時(shí)候,先進(jìn)行了實(shí)例的緩存,這個(gè)也是他性能較好的原因之一。如果把緩存去掉的話,性能就會(huì)出現(xiàn)了一些的差異,但是整體的性能還是很好, 從整體的表現(xiàn)來(lái)看,Cglib的BeanCopier的性能是最好的無(wú)論是數(shù)量較大的1萬(wàn)次的測(cè)試,還是數(shù)量較少10次,幾乎都是趨近與零損耗,Spring是在次數(shù)增多的情況下,性能較好,在數(shù)據(jù)較少的時(shí)候,性能比PropertyUtils的性能差一些。PropertyUtils的性能相對(duì)穩(wěn)定,表現(xiàn)是呈現(xiàn)線性增長(zhǎng)的趨勢(shì)。而Apache的BeanUtil的性能最差,無(wú)論是單次Copy還是大數(shù)量的多次Copy性能都不是很好。
到此,相信大家對(duì)“Bean復(fù)制的幾種框架有哪些區(qū)別”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。