性能测量工具类——TimeMeasureUtil TimeMeasureProxy
TimeMeasureUtil:做單次時(shí)間測(cè)量。
1、為了能確保 startTime 和 endTime 都正確設(shè)置,因而采用實(shí)例對(duì)象來(lái)實(shí)現(xiàn)。每次測(cè)量是都能判斷對(duì)象是否處在正確狀態(tài)。
2、該類(lèi)為工具類(lèi),即使測(cè)試時(shí)產(chǎn)生許多對(duì)象實(shí)例也對(duì)軟件無(wú)任何影響。
public class TimeMeasureUtil {private long startTime = 0;private long endTime = 0;private TimeMeasureUtil(){}public static TimeMeasureUtil newInstance(){return new TimeMeasureUtil();}public void start(){startTime = System.currentTimeMillis();}public void end(){endTime = System.currentTimeMillis();}public long measure(){// Invalid: Before yif( startTime == 0 ){throw new IllegalStateException( "Must call metod start() first, when call method measure()." );}if( endTime == 0 ){throw new IllegalStateException( "Must call metod end() first, when call method measure()." );}if( startTime > endTime ){throw new IllegalStateException( "Must call metod start() first, when call method end()." );}return endTime - startTime;}}使用示例:
@Testpublic void testMeasure_ok() {TimeMeasureUtil timeUtil = TimeMeasureUtil.newInstance();timeUtil.start();// 下面這段代碼用實(shí)際待測(cè)方法代替try {Thread.sleep( 1000 );} catch (InterruptedException e) {e.printStackTrace();}timeUtil.end();assertEquals( 1000, timeUtil.measure() );}
TimeMeasureProxy:
1、默認(rèn)運(yùn)行20次得平均
2、給定運(yùn)行次數(shù)的平均
缺陷:
1、由于是用反射實(shí)現(xiàn),參數(shù)是通過(guò) Object[ ] 來(lái)傳遞。有原始類(lèi)型(如:byte,int,char...)參數(shù)的方法不能測(cè)試!只能用 TimeMeasureUtil 來(lái)實(shí)現(xiàn)測(cè)試。?
2、如果第一次執(zhí)行方法后改變了某屬性,而該屬性改變后又會(huì)影響下一次方法的執(zhí)行(可能會(huì)有不同的時(shí)間),這樣的方法不能測(cè)試。可能的改進(jìn)方式:先“克隆” count 個(gè)該對(duì)象,然后用這樣相同屬性的對(duì)象來(lái)執(zhí)行方法(每個(gè)對(duì)象執(zhí)行一次方法)。因?yàn)檫@樣要求,被測(cè)試類(lèi)提供“克隆”方法,“因測(cè)試而影響邏輯”不可取,所以沒(méi)有實(shí)現(xiàn)。
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;public class TimeMeasureProxy {// default measure countprivate int count = 20;private TimeMeasureProxy(){}private TimeMeasureProxy( int count ){this.count = count;}public static TimeMeasureProxy newInstance(){return new TimeMeasureProxy();}public static TimeMeasureProxy newInstance( int count ){return new TimeMeasureProxy( count );}public long avgTime( Object owner, String methodName, Object[] args ){// valid parameters.if( owner == null ){throw new IllegalStateException("owner can't be null.");}if( methodName == null ){throw new IllegalStateException("methodName can't be null.");}Class<?> ownerClass = owner.getClass();Class<?>[] argsClass = null;if( args != null ){argsClass = new Class[ args.length ];for( int i=0 ; i<args.length ; i++ ){argsClass[i] = args[i].getClass();}}Method method;try {method = ownerClass.getMethod( methodName, argsClass );} catch (SecurityException e) {throw new RuntimeException( e );} catch (NoSuchMethodException e) {throw new RuntimeException( e );}return totalTime( owner, method, args)/count;}long totalTime( Object owner, Method method, Object[] args ){long totalTime = 0;try {for( int i=0; i<count ; i++ ){TimeMeasureUtil timeUtil = TimeMeasureUtil.newInstance();timeUtil.start();method.invoke( owner, args );timeUtil.end();totalTime += timeUtil.measure();}} catch (IllegalArgumentException e) {throw new RuntimeException( e );} catch (IllegalAccessException e) {throw new RuntimeException( e );} catch (InvocationTargetException e) {throw new RuntimeException( e );}return totalTime;}}使用示例:
1、待測(cè)試的類(lèi)
class ForMeasureAvgTime{public void justForTest(){try {Thread.sleep( 335 );} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void justForTestWithParams( Integer age, String name ){try {Thread.sleep( 558 );} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }2、通過(guò)TimeMeasureProxy 來(lái)測(cè)試
@Test//測(cè)量無(wú)參數(shù)方法&默認(rèn)次數(shù)(20)public void testAvgTime_defaultCount() {ForMeasureAvgTime forMeasure = new ForMeasureAvgTime();TimeMeasureProxy proxy = TimeMeasureProxy.newInstance();long avgTime = proxy.avgTime( forMeasure, "justForTest", null );System.out.println( "avgTime=" + avgTime );assertTrue( (avgTime - 335) < 5 || (335 - avgTime) < 5);}@Test//測(cè)量有參數(shù)方法&給定次數(shù)(10)public void testAvgTime_giveCount() {ForMeasureAvgTime forMeasure = new ForMeasureAvgTime();TimeMeasureProxy proxy = TimeMeasureProxy.newInstance( 10 );long avgTime = proxy.avgTime( forMeasure, "justForTestWithParams", new Object[]{24, "GongQiang"} );System.out.println( "avgTime=" + avgTime );assertTrue( (avgTime - 558) < 5 || (558 - avgTime) < 5);}總結(jié)
以上是生活随笔為你收集整理的性能测量工具类——TimeMeasureUtil TimeMeasureProxy的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Replace Parameter wi
- 下一篇: Java并发编程实战~Balking模式