ContiPerf
概述
ContiPerf 是一个轻量级的单元测试工具,基于JUnit 4二次开发,使用它基于注解的方式,快速在本地进行单元压测并提供详细的报告。
Example
1. 新建 SpringBoot 工程
核心依赖如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.databene</groupId>
<artifactId>contiperf</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
2. 测试接口以及实现
package com.wuwenze.contiperf.service;
import java.util.List;
public interface ContiperfExampleService {
List<String> findAll();
}
import com.wuwenze.contiperf.service.ContiperfExampleService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class ContiperfExampleServiceImpl implements ContiperfExampleService {
private final Random RANDOM = new Random();
@Override
public List<String> findAll() {
try {
int sleepSecond = RANDOM.nextInt(10);
log.info("#findAll(): sleep {} seconds..", sleepSecond);
Thread.sleep(sleepSecond * 1000);
} catch (InterruptedException e) {
// ignore
}
List<String> resultList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
resultList.add("string_" + i);
}
return resultList;
}
}
3. 构建单元测试
package com.wuwenze.contiperf.service;
import com.wuwenze.contiperf.ContiperfExamplesApplication;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
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;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ContiperfExamplesApplication.class)
public class ContiperfExampleServiceTest {
@Rule
public ContiPerfRule i = new ContiPerfRule();
@Autowired
private ContiperfExampleService contiperfExampleService;
@Test
@PerfTest(threads = 1000, duration = 1500)
public void findAll() {
contiperfExampleService
.findAll()
.forEach(System.out::println);
}
}
4. 最终执行效果
查看测试报告:
总结
1)PerfTest参数
@PerfTest(invocations = 300):执行300次,和线程数量无关,默认值为1,表示执行1次;
@PerfTest(threads=30):并发执行30个线程,默认值为1个线程;
@PerfTest(duration = 20000):重复地执行测试至少执行20s。
三个属性可以组合使用,其中Threads必须和其他两个属性组合才能生效。当Invocations和Duration都有指定时,以执行次数多的为准。
例,@PerfTest(invocations = 300, threads = 2, duration = 100),如果执行方法300次的时候执行时间还没到100ms,则继续执行到满足执行时间等于100ms,如果执行到50次的时候已经100ms了,则会继续执行之100次。
如果你不想让测试连续不间断的跑完,可以通过注释设置等待时间,例,@PerfTest(invocations = 1000, threads = 10, timer = RandomTimer.class, timerParams = { 30, 80 }) ,每执行完一次会等待30~80ms然后才会执行下一次调用。
在开多线程进行并发压测的时候,如果一下子达到最大进程数有些系统可能会受不了,ContiPerf还提供了“预热”功能,例,@PerfTest(threads = 10, duration = 60000, rampUp = 1000) ,启动时会先起一个线程,然后每个1000ms起一线程,到9000ms时10个线程同时执行,那么这个测试实际执行了69s,如果只想衡量全力压测的结果,那么可以在注释中加入warmUp,即@PerfTest(threads = 10, duration = 60000, rampUp = 1000, warmUp = 9000) ,那么统计结果的时候会去掉预热的9s。
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。
3)测试报告
最终的测试报告位于target/contiperf-report/index.html,使用浏览器打开即可。
ContiPerf的更多相关文章
- 性能工具之代码级性能测试工具ContiPerf
前言 做性能的同学一定遇到过这样的场景:应用级别的性能测试发现一个操作的响应时间很长,然后要花费很多时间去逐级排查,最后却发现罪魁祸首是代码中某个实现低效的底层算法.这种自上而下的逐级排查定位的方法, ...
- 基于Twitter的Snowflake算法实现分布式高效有序ID生产黑科技(无懈可击)
参考美团文档:https://tech.meituan.com/2017/04/21/mt-leaf.html Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万 ...
- SpringBoot | 第十三章:测试相关(单元测试、性能测试)
前言 前面写了这么多章节,都是通过浏览器访问的形式,进行接口方法访问进而验证方法的正确与否.显然在服务或者接口比较少时,这么做没有啥问题,但一旦一个项目稍微复杂或者接口方法比较多时,这么验证就有点不符 ...
- Beta之前-凡事预则立(校园帮-追光的人)
所属课程 软件工程1916 作业要求 Beta之前-凡事预则立 团队名称 追光的人 作业目标 在Beta冲刺之前,提前做好准备和规划 议题 1.讨论组长是否重选的议题和结论. 2.下一阶段需要改进完善 ...
- springboot项目中进行并发测试
一 利用工具包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
随机推荐
- 并发02--JAVA内存模型
在并发编程中,需要解决两个问题:线程间如何通信&线程间如何同步 线程同步:控制不同线程操作顺序的机制 解决这两个问题的方案有两种:共享内存&消息传递 共享内存:通过使用共享内存,隐式通 ...
- JNI调用Cython生成库‘undefined symbol: PyInit_’问题
最近项目需要提升所有 Python 算法的执行时间,并给 Java 框架调用,根据 Python一键转Jar包,Java调用Python新姿势!的思路可以用 Cython 将 Python 代码转换为 ...
- vue全家桶(3.2)
4.5.创建实例 可以创建一个自定义实例应用于多个请求接口 <template> <div class="page"> </div> </ ...
- js事件入门(1)
1.事件相关概念 1.1 什么是事件? 事件是用户在访问页面时执行的操作,也就是用户访问页面时的行为.当浏览器探测到一个事件时,比如鼠标点击或者按键.它可以触发与这个事件相关的JavaScript对象 ...
- could not resolve property(无法解析属性)
could not resolve property(无法解析属性) 顾名思义在写hql语句的时候,属性写错了! 请检查大小写,是实体类的,不是数据库表的! 一个一个检查,仔细看!
- 【floyd+矩阵乘法】POJ 3613 Cow Relays
Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a rel ...
- Spring Security(三) —— 核心配置解读
摘要: 原创出处 https://www.cnkirito.moe/spring-security-3/ 「老徐」欢迎转载,保留摘要,谢谢! 3 核心配置解读 上一篇文章<Spring Secu ...
- 机器学习入门:极度舒适的GBDT原理拆解
机器学习入门:极度舒适的GBDT拆解 本文旨用小例子+可视化的方式拆解GBDT原理中的每个步骤,使大家可以彻底理解GBDT Boosting→Gradient Boosting Boosting是集成 ...
- 最简单的博弈论——HDU - 5963 朋友 (博弈)
OK,好的先看一下题意: B君在围观一群男生和一群女生玩游戏,具体来说游戏是这样的: 给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根. ...
- C#字符串拼接
var name = "李哈哈"; var t = $"我是{name}";