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 ...
随机推荐
- android中使用https是否对服务证书合法性校验的新的体会
package com.cetcs.logreport.utils; import android.content.Context; import org.apache.http.conn.ssl.S ...
- Quartz.Net系列(九):Trigger之DailyTimeIntervalScheduleBuilder详解
1.介绍 中文意义就是每日时间间隔计划生成 2.API讲解 (1)WithInterval.WithIntervalInHours.WithIntervalInMinutes.WithInterval ...
- day01---学习Mysql高级性能优化1
Mysql逻辑架构图
- SpringBoot启动源码及自定义starter
为什么springboot工程能够在mian方法中完成启动呢?需要大家掌握的有几个点:1.SPISPI在springboot中是去读取META-INF/spring.factories目录的配置文件内 ...
- 每天一个LINUX命令(pwd)
每天一个LINUX命令(pwd) 基本信息 pwd: /bin/pwd,显示当前路径的绝对路径 语法:pwd 应用程序位置 which pwd PWD作用 pwd --help ...
- Python3笔记003 - 1.3 python开发工具
第1章 认识python 1.3 python开发工具 IDLE(python自带的python shell) Pycharm(python开发的,选择专业版) 1.进入IDLE模式: C:\Prog ...
- 基础-Junit单元测试_反射_注解
一.Junit单元测试 1.1 测试分类: 黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值. 白盒测试:需要写代码的.关注程序具体的执行流程. 1.2 Junit使用(白盒测试) 使用步 ...
- css3支持动画吗?css3可以用于网页动画的展现吗
CSS3 主要可以分为几个模块:边框和背景,渐变,文字特效,字体,2D/3D转换,动画(过渡动画和动画),选择器,盒模型,多列布局,用户界面. css3动画有2类:一种是transition的,另一种 ...
- Java嵌套类,内部类和外部类
1.嵌套类,内部类 嵌套类是指被定义在一个类内部的类: JAVA的嵌套类有很多种类:1.静态成员类:2.非静态成员类:3.匿名类:4.局部类:其中,除了静态成员类之外,其他的都是内部类,因为静态成员类 ...
- STA树的深度(树型DP)
STA树的深度 题目大意 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 Input 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. Outpu ...