最近在完成红包的业务之后,刚好今天闲了一点,想做一下性能测试,由于Jmeter使用起来比较复杂,需要搭环境,在网上找到了PerfTest.
要求:PerfTest必须使用Junit框架,按描述是要求在Junit4.7以上版本才可以。
(如果有兴趣,本人还有一篇介绍jmeter与jvisualVM使用的博客,欢迎前去围观)
这个工具使用流程如下:
1、maven 引入
<dependency>
<groupId>org.databene</groupId>
<artifactId>contiperf</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
2、在Junit内新增回滚注解,保证数据不会被提交(造数据比较麻烦)
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
@ContextConfiguration(locations = { "classpath:spring/applicationContext-test.xml" })
3、单元测试类内加上以下代码:
@Rule
public ContiPerfRule i = new ContiPerfRule();
按照文档说明,加上这个之后,就可以使用该性能测试的工具了。
在具体类上,加上以下注解:
@Test
@PerfTest(threads = 30,invocations = 3000)
public void testgetHongBaoAmout(){
…………
}
在测完之后,在本地的target目录下面,有一个文件目录contiperf-report,打开该目录下面唯一的index.html文件,里面即为测试完成的结果。
由于本地压力测试时,有Reddis缓存报错(待解决),以及事务,可能有延迟。导致结果不理想,后面改进。
我本地是起30个线程,执行3000次的结果。