拓展阅读
junit5 系列教程
基于 junit5 实现 junitperf 源码分析
Auto generate mock data for java test.(便于 Java 测试主动生成对象信息)
Junit performance rely on junit5 and jdk8+.(java 性能测试框架。压测+测试报告生成。)
junitperf
junitperf 是一款为 java 开发者设计的性能测试框架。
为什么使用?
- 可以和 Junit5 美满契合。
- 使用简单,便于项目开发过程中的测试实用。
- 提供拓展,用户可进行自界说开发。
特性
- 支持 I18N
- 支持多种报告生成方式,支持自界说
- Junt5 美满支持,便于 Java 开发者使用
快速开始
项目依赖
maven 导入
- <dependency>
- <groupId>com.github.houbb</groupId>
- <artifactId>junitperf</artifactId>
- <version>2.0.7</version>
- </dependency>
复制代码 入门案例
入门案例地址
- public class HelloWorldTest {
- @JunitPerfConfig(duration = 1000)
- public void helloTest() throws InterruptedException {
- Thread.sleep(100);
- System.out.println("Hello Junit5");
- }
- }
复制代码 配置分析
测试注解指定
@JunitPerfConfig
指定测试时的属性配置。(必填项)
属性分析类型默认值备注threads实行时使用多少线程实行int1warmUp准备时间long0单位:毫秒duration实行时间long60_000(1分钟)单位:毫秒latencyStatistics统计实现StatisticsCalculatorDefaultStatisticsCalculatorreporter报告实现ReporterConsoleReporter使用如下:- public class JunitPerfConfigTest {
- /**
- * 2个线程运行。
- * 准备时间:1000ms
- * 运行时间: 2000ms
- * @throws InterruptedException if any
- */
- @JunitPerfConfig(threads = 2, warmUp = 1000, duration = 2000)
- public void junitPerfConfigTest() throws InterruptedException {
- System.out.println("junitPerfConfigTest");
- Thread.sleep(200);
- }
- }
复制代码 各种报告的实现
这里主要是对于性能测试统计的输出方式。
支持以下方式:
方式案例默认方式DefaultReporterTest命令行ConsoleReporterTestHTMLHtmlReporterTest组合方式MultiReporterTest自界说方式DefineReporterTest@JunitPerfRequire
指定测试时需要达到的要求。(选填项)
属性分析类型默认值备注min最佳的运行耗时float-1最快的运行耗时假如高于这个值,则视为失败。单位:毫秒max平均的运行耗时float-1最坏的运行耗时假如高于这个值,则视为失败。单位:毫秒average平均的运行耗时float-1平均的运行耗时假如高于这个值,则视为失败。单位:毫秒timesPerSecond每秒的最小实行次数int0假如低于这个最小实行次数,则视为失败。percentiles对于实行耗时的限定String[]{}percentiles={"20:220", "30:250"}。20% 的数据实行耗时不得凌驾 220ms;30% 的数据实行耗时不得凌驾 250ms;使用如下:- public class JunitPerfRequireTest {
- /**
- * 配置:2个线程运行。准备时间:1000ms。运行时间: 2000ms。
- * 要求:最快不可低于 210ms, 最慢不得低于 250ms, 平均不得低于 225ms, 每秒运行次数不得低于 4 次。
- * 20% 的数据不低于 220ms, 50% 的数据不得低于 230ms;
- *
- * @throws InterruptedException if any
- */
- @JunitPerfConfig(threads = 2, warmUp = 1000, duration = 2000)
- @JunitPerfRequire(min = 210, max = 250, average = 225, timesPerSecond = 4, percentiles = {"20:220", "50:230"})
- public void junitPerfConfigTest() throws InterruptedException {
- System.out.println("junitPerfConfigTest");
- Thread.sleep(200);
- }
- }
复制代码 报告方式
命令行方式
大致如下:- [INFO] [2020-06-16 20:05:53.618] [c.g.h.j.e.HelloWorldTest.helloTest] - Started at: 2020-06-16 20:05:52.512
- [INFO] [2020-06-16 20:05:53.619] [c.g.h.j.e.HelloWorldTest.helloTest] - Invocations: 9
- [INFO] [2020-06-16 20:05:53.620] [c.g.h.j.e.HelloWorldTest.helloTest] - Success: 9
- [INFO] [2020-06-16 20:05:53.620] [c.g.h.j.e.HelloWorldTest.helloTest] - Errors: 0
- [INFO] [2020-06-16 20:05:53.621] [c.g.h.j.e.HelloWorldTest.helloTest] - Thread Count: 1
- [INFO] [2020-06-16 20:05:53.623] [c.g.h.j.e.HelloWorldTest.helloTest] - Warm up: 0ms
- [INFO] [2020-06-16 20:05:53.623] [c.g.h.j.e.HelloWorldTest.helloTest] - Execution time: 1000ms
- [INFO] [2020-06-16 20:05:53.624] [c.g.h.j.e.HelloWorldTest.helloTest] - Throughput: 9/s (Required: -1/s) - PASSED
- [INFO] [2020-06-16 20:05:53.625] [c.g.h.j.e.HelloWorldTest.helloTest] - Memory cost: 16byte
- [INFO] [2020-06-16 20:05:53.635] [c.g.h.j.e.HelloWorldTest.helloTest] - Min latency: 100.191414ms (Required: -1.0ms) - PASSED
- [INFO] [2020-06-16 20:05:53.635] [c.g.h.j.e.HelloWorldTest.helloTest] - Max latency: 105.2382ms (Required: -1.0ms) - PASSED
- [INFO] [2020-06-16 20:05:53.636] [c.g.h.j.e.HelloWorldTest.helloTest] - Avg latency: 101.43268ms (Required: -1.0ms) - PASSED
复制代码 HTML 方式
页面如下:
后期会进行样式调整。
指定方法实行序次
分析
方法的实行序次会影响到最终的报告体现序次。
假如你想严格指定同一个类方法的实行序次,保举使用 Test Execution Order 的方式。
@TestMethodOrder 需要 junit5 在 5.4 及其以后版本
示例代码
参考 OrderedHtmlTest
对于 junit4 的支持
引入 jar
- <dependency>
- <groupId>com.github.houbb</groupId>
- <artifactId>junitperf</artifactId>
- <version>1.0.3</version>
- </dependency>
复制代码 相关文档
junit4 相关使用
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |