[转]ContiPerf介绍

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

ContiPerf是一个轻量级的测试工具,基于JUnit 4 开发,可用于效率测试等。
1、ContiPerf介绍
可以指定在线程数量和执行次数,通过限制最大时间和平均执行时间来进行效率测试,一个简单的例子如下:

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public class ContiPerfTest {  
  2.     @Rule  
  3.     public ContiPerfRule i = new ContiPerfRule();  
  4.   
  5.     @Test  
  6.     @PerfTest(invocations = 1000, threads = 40)  
  7.     @Required(max = 1200, average = 250, totalTime = 60000)  
  8.     public void test1() throws Exception {  
  9.         Thread.sleep(200);  
  10.     }  
  11. }  

使用@Rule注释激活ContiPerf,通过@Test指定测试方法,@PerfTest指定调用次数和线程数量,@Required指定性能要求(每次执行的最长时间,平均时间,总时间等)。
也可以通过对类指定@PerfTest和@Required,表示类中方法的默认设置,如下:

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. @PerfTest(invocations = 1000, threads = 40)  
  2. @Required(max = 1200, average = 250, totalTime = 60000)  
  3. public class ContiPerfTest {  
  4.     @Rule  
  5.     public ContiPerfRule i = new ContiPerfRule();  
  6.   
  7.     @Test  
  8.     public void test1() throws Exception {  
  9.         Thread.sleep(200);  
  10.     }  
  11. }  

2、在maven中使用ContiPerf
配置方式如下:

[html]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>4.7</version>  
  6.         <scope>test</scope>  
  7.     </dependency>   
  8.     <dependency>  
  9.         <groupId>org.databene</groupId>  
  10.         <artifactId>contiperf</artifactId>  
  11.         <version>2.1.0</version>  
  12.         <scope>test</scope>  
  13.     </dependency>  
  14. </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。
4、测试结果展示
测试的结果可以展示在浏览器中,如下:

 生成的报名文件在maven项目的target文件夹下面。target\contiperf-report\index.html

 

猜你喜欢

转载自breezylee.iteye.com/blog/2326011
今日推荐