ContiPerf 是一个轻量级的测试工具,基于JUnit 4 开发,可用于性能测试等。可以指定在线程数量和执行次数,通过限制最大时间和平均执行时间来进行性能测试。
1. PerfTest参数说明
/* 单元测试类中,注入以下属性激活Contiperf */
@Rule
public ContiPerfRule i = new ContiPerfRule();
package cn.stylefeng.guns.base;
import cn.stylefeng.guns.GunsApplication;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
/**
* 基础测试类
*
* @author stylefeng
* @Date 2017/5/21 16:10
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GunsApplication.class)
@WebAppConfiguration
//@Transactional //打开的话测试之后数据可自动回滚
public class BaseJunit {
@Autowired
WebApplicationContext webApplicationContext;
protected MockMvc mockMvc;
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Before
public void initDatabase() {
}
}
参数 | 说明 |
@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 |
3 名词解释:TP90
指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第90%的那个值作为TP90 值;配置此监控指标对应的报警阀值后,需要保证在这个时间段内该方法所有调用的消耗时间至少有90%的值要小于此阀值,否则系统将会报警
4. 报告查看
contiperf-report目录,里面有个index.html文件就是测试结果
5 指标
指标 | 说明 |
Execution time | 执行总时间 |
Total invocations | 总请求数 |
Throughput | 每秒效率 TPS |
Min. latency | 最短响应时间 |
Average latency | 平均响应时间 |
Median | TP50响应时间 |
90% | TP90响应时间 |
Max latency | 最长响应时间 |