ContiPerf是一个轻量级的测试工具,基于JUnit 4 开发,可用于接口级的性能测试,可以指定在线程数量和执行次数,通过限制最大时间和平均执行时间来进行效率测试。常用的参数如下:@PerfTest(invocations = 100,threads = 10)
invocations() : 执行次数与线程无关
duration(): 间隔时间
threads():线程数
添加依赖包
<!-- 性能测试 -->
<dependency>
<groupId>org.databene</groupId>
<artifactId>contiperf</artifactId>
<version>2.3.4</version>
<scope>test</scope>
</dependency>
测试类使用
private @Autowired UserService userService;
//引入 ContiPerf 进行性能测试
@Rule
public ContiPerfRule rule = new ContiPerfRule();
//10个线程 执行100次
@Test
@PerfTest(invocations = 100,threads = 10)
public void test(){
Long id = (long) (Math.random()*100);
userService.selectId(id);
}
在junit执行完毕,会在target/contiperf-report中有相关的执行结果,可以使用浏览器打开查看