最近新写了一基于phoneix存储的系统,想做一下接口的压力测试
简单搜索了一下相关开源组件,找了几个不错的组件,都是基于junit整合的。ps: 搞Java的好处就是先去github找一下是否有成熟的组件,自己写太累,也不完善。
先说结论:如果你的框架没有强制依赖guava的话,那么选哪个都可以,如果强制依赖了guava的话,那么用com.github.javatlacati » contiperf
目前找了几个 :
junitperf » junitperf。最近一直更新已经是2005年了,应该早不维护了。
com.github.noconnor » junitperf : 最新的是这个2019年,最新版本是1.15.0 ,目前是支持junit5
com.github.javatlacati » contiperf : 2019年还在更新,基于junit4,它的前身是org.databene » contiperf 不过最近更新也是在2014年了
com.github.houbb » junitperf : 2020年更新,底层依赖的是 com.github.noconnor » junitperf和com.github.javatlacati » contiperf ,是对于两者的重新包装。 目前是支持junit5
组件对比
组件
|
noconnor » junitperf
|
javatlacati » contiperf
|
houbb » junitperf
|
最新junit版本
|
5
|
4
|
5
|
输出报告
|
console,html,csv,custom,multiple
|
html
|
console,html,csv,custom,multiple
|
指标
|
吞吐、min、max、avg、success、error
|
吞吐、min、max、avg、med、90线
|
同noconnor » junitperf
|
定制指标
|
可以随意定制90线,95线等指标
|
可以随意定制90线,95线等指标
|
可以随意定制90线,95线等指标
|
多线程
|
支持
|
支持
|
支持
|
结果预期
|
支持
|
支持
|
支持
|
输出
|
通过JUnitPerfRule定义
|
通过标注 report定义
|
|
参数
|
丰富
|
丰富
|
有裁剪,没有所有都支持
|
从更新时间来说,junitperf比较持续,而且功能上也没有什么区别,就采用了junitperf来做junit的集成性能测试。
houbb » junitperf 其实是包装了 noconnor » junitperf 和javatlacati » contiperf 。 是一个封装器,也改写了使用方法。
使用姿势
noconnor » junitperf的使用姿势
public class PerformanceTest { //定义输出模式,CsvReportGenerator,HtmlReportGenerator,CustomStatisticsCalculatorImpl,CustomStatisticsCalculatorImpl @Rule public JUnitPerfRule perfTestRule = new JUnitPerfRule(new ConsoleReportGenerator()); /** * threads :执行线程数 * durationMs :执行时间 * rampUpPeriodMs:平滑预热时间,默认是精致 * warmUpMs : 准备时间 * maxExecutionsPerSecond:每秒执行个数 * @throws InterruptedException */ @Test @JUnitPerfTest(threads = 50, durationMs = 125_000, rampUpPeriodMs = 2_000, warmUpMs = 10_000, maxExecutionsPerSecond = 11_000) public void test() throws InterruptedException { System.out.println("haha"); ThreadLocalRandom current = ThreadLocalRandom.current(); TimeUnit.SECONDS.sleep(current.nextInt(10)); } }
houbb » junitperf 使用姿势
/** * @author lukexue * @create 2020-06-10 15:02 **/ public class PerformanceTest2 { /** * 配置:2个线程运行。准备时间:1000ms。运行时间: 2000ms。 * 要求:最快不可低于 210ms, 最慢不得低于 250ms, 平均不得低于 225ms, 每秒运行次数不得低于 4 次。 * 20% 的数据不低于 220ms, 50% 的数据不得低于 230ms; * reporter : 输出格式html */ @Test @JunitPerfConfig(threads = 50, warmUp = 10000, duration = 125000, reporter = {ConsoleReporter.class}) //ConsoleReporter,HtmlReporter @JunitPerfRequire(min = 210, max = 250, average = 225, timesPerSecond = 4, percentiles = {"20:220", "50:230"}) public void test() throws InterruptedException { System.out.println("haha"); ThreadLocalRandom current = ThreadLocalRandom.current(); TimeUnit.SECONDS.sleep(current.nextInt(10)); } }
结果输出
最终结果输出
</div> <div>15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Started at: 2020-06-10 15:07:26.854 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Invocations: 1340 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Success: 1340 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Errors: 0 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Thread Count: 50 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Warm up: 10000ms 15:09:31.976 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Execution time: 125000ms 15:09:31.977 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Throughput: 11/s (Required: 4/s) - PASSED 15:09:31.978 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Min latency: 0.003639ms (Required: 210.0ms) - PASSED 15:09:31.978 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Max latency: 9005.115ms (Required: 250.0ms) - FAILED 15:09:31.979 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Ave latency: 4351.276ms (Required: 225.0ms) - FAILED 15:09:31.979 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Percentile: 50% 4004.3013ms (Required: 230.0ms) - FAILED 15:09:31.980 [main] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Percentile: 20% 1004.48737ms (Required: 220.0ms) - FAILED 15:09:31.982 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Started at: 2020-06-10 15:07:26.854 15:09:31.982 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Invocations: 1340 15:09:31.982 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Success: 1340 15:09:31.982 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Errors: 0 15:09:31.982 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Thread Count: 50 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Warm up: 10000ms 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Execution time: 125000ms 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Throughput: 11/s (Required: 4/s) - PASSED 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Min latency: 0.003639ms (Required: 210.0ms) - PASSED 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Max latency: 9005.115ms (Required: 250.0ms) - FAILED 15:09:31.983 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Ave latency: 4351.276ms (Required: 225.0ms) - FAILED 15:09:31.984 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Percentile: 50% 4004.3013ms (Required: 230.0ms) - FAILED 15:09:31.984 [pool-2-thread-1] INFO com.github.houbb.junitperf.core.report.impl.ConsoleReporter - Percentile: 20% 1004.48737ms (Required: 220.0ms) - FAILED</div> </div> <div>
唯一的遗憾是包是强依赖与 guava,如果你自己用了guava版本比它还低的话就跑不起来,因为我的业务也用了hbase,里面依赖的低版本的guava,结果很尴尬。这个也是java的问题
参考资料:
https://segmentfault.com/a/1190000015722861
https://github.com/houbb/junitperf
https://github.com/noconnor/JUnitPerf#multiple-reports