這裡給出java測試代碼
1.Stopwatch類的實現
//filename:StopWatch.java<br />package my.test;<br />public class StopWatch<br />{<br />private long startTime = 0;<br /> private long stopTime = 0; </p><p> public void start() {<br /> this.startTime = System.currentTimeMillis(); </p><p> } </p><p> public void stop() {<br /> this.stopTime = System.currentTimeMillis(); </p><p> } </p><p> //elaspsed time in milliseconds<br /> public long getElapsedTime() {<br /> long elapsed; </p><p> elapsed = (stopTime - startTime); </p><p> return elapsed;<br /> } </p><p>}<br />
2.測試工作類的實現
//filename:WorkClass.java<br />package my.test;<br />public class WorkClass<br />{<br />public double AddTest()<br />{<br />int count=10000000,i=0,j=0,count2=10;<br /> double sum=0.0;<br /> for(i=0;i<count2;i++)<br /> for(j=0;j<count;j++)<br /> sum+=j;<br /> return sum;<br />}</p><p>public double MulTest()<br />{<br />int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum*=i;<br /> return sum;<br />}</p><p>public double DivTest()<br />{<br />int count=10000*10000,i=0;<br /> double sum=0.0;<br /> for(i=1;i<count;i++)sum/=i;<br /> return sum;<br />}</p><p>private long fib(long n)<br />{<br />if (n < 2)<br />return (1);<br />else<br />return (fib(n - 2) + fib(n - 1));<br />}</p><p>public double FibTest()<br />{<br />return (double)fib(30);<br />}<br />}<br />
3.自動化的測試類
//filename:AutoTestClass.java<br />package my.test;<br />import java.lang.reflect.Method;<br />public class AutoTestClass<br />{<br />public void AutoTest(String itemname)<br />{<br /> int test_count=10;<br /> double test_time=0.0;<br /> int run_count=0;<br /> Double ret;<br /> StopWatch sw=new StopWatch();<br /> WorkClass wc=new WorkClass();</p><p> try<br /> {<br /> Class myclass = Class.forName("my.test.WorkClass");<br /> Method mytest_fun = myclass.getMethod(itemname);<br /> for(run_count=0;run_count<test_count;run_count++)<br /> {<br /> ret=0.0;<br /> sw.start();<br /> ret=(Double)mytest_fun.invoke(wc,null);</p><p> if(ret>0)sw.stop();<br /> else sw.stop();</p><p> test_time+=sw.getElapsedTime();<br /> }<br /> }catch (Throwable e)<br /> {<br /> System.err.println(e);<br /> }</p><p> String outstr="work cost average time(10)="+test_time/test_count;<br /> System.out.println(outstr);<br />}<br />}<br />
4.最後是主程式類
//filename:MainClass.java<br />package my.test;<br />public class MainClass {<br />/**<br /> * @param args<br /> */<br />public static void main(String[] args)<br />{<br />AutoTestClass atc=new AutoTestClass();<br />atc.AutoTest("AddTest");<br />atc.AutoTest("FibTest");<br />atc.AutoTest("MulTest");<br />atc.AutoTest("DivTest");</p><p>}<br />}<br />