測試多線程併發的簡單方法

輕量級的性能測試工具

       給大家介紹一個輕量級的性能測試工具,該工具可以在開發階段做單元測試的時候也做接口的性能測試,極大的方便了開發人員發開發階段就可以對接口進行初步性能評估,有利於在開發階段優化我們的接口代碼。

1、ContiPerf介紹
可以指定在線程數量和執行次數,通過限制最大時間和平均執行時間來進行效率測試,一個簡單的例子如下:

1、ContiPerf介紹

可以指定在線程數量和執行次數,通過限制最大時間和平均執行時間來進行效率測試,一個簡單的例子如下:

public class ContiPerfTest {  

    @Rule  

    public ContiPerfRule i = new ContiPerfRule();  

  

    @Test  

    @PerfTest(invocations = 1000, threads = 40)  

    @Required(max = 1200, average = 250, totalTime = 60000)  

    public void test1() throws Exception {  

        Thread.sleep(200);  

    }  

 

使用@Rule註釋激活ContiPerf,通過@Test指定測試方法,@PerfTest指定調用次數和線程數量,@Required指定性能要求(每次執行的最長時間,平均時間,總時間等)。

也可以通過對類指定@PerfTest和@Required,表示類中方法的默認設置,如下:

@PerfTest(invocations = 1000, threads = 40)  

@Required(max = 1200, average = 250, totalTime = 60000)  

public class ContiPerfTest {  

    @Rule  

    public ContiPerfRule i = new ContiPerfRule();  

  

    @Test  

    public void test1() throws Exception {  

        Thread.sleep(200);  

    }  

}  


2、在maven中使用ContiPerf

配置方式如下:

<dependencies>  

    <dependency>  

        <groupId>junit</groupId>  

        <artifactId>junit</artifactId>  

        <version>4.7</version>  

        <scope>test</scope>  

    </dependency>   

    <dependency>  

        <groupId>org.databene</groupId>  

        <artifactId>contiperf</artifactId>  

        <version>2.1.0</version>  

        <scope>test</scope>  

    </dependency>  

</dependencies>  


3、主要參數介紹

1)PerfTest參數

@PerfTest(invocations = 300):執行300次,和線程數量無關,默認值爲1,表示執行1次;

@PerfTest(threads=30):併發執行30個線程,默認值爲1個線程;

@PerfTest(duration = 20000):重複地執行測試至少執行20s。

2)Required參數

@Required(throughput = 20):要求每秒至少執行20個測試;

@Required(average = 50):要求平均執行時間不超過50ms;

@Required(median = 45):要求所有執行的50%不超過45ms; 

@Required(max = 2000):要求沒有測試超過2s;

@Required(totalTime = 5000):要求總的執行時間不超過5s;

@Required(percentile90 = 3000):要求90%的測試不超過3s;

@Required(percentile95 = 5000):要求95%的測試不超過5s; 

@Required(percentile99 = 10000):要求99%的測試不超過10s; 

@Required(percentiles = "66:200,96:500"):要求66%的測試不超過200ms,96%的測試不超過500ms。


來源:

http://databene.org/contiperf/ 

http://blog.csdn.net/tomato__/article/details/22060449 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章