介绍
JMH 是 Java Microbenchmark Harness 的缩写。中文意思大致是 “JAVA 微基准测试套件”
优势
可重复性:可进行重复性的测试,这样做有利于比较每次的测试结果,得到性能结果的长期变化趋势,为系统调优和上线前的容量规划做参考。
可观测性:通过全方位的监控(包括测试开始到结束,执行机、服务器、数据库),及时了解和分析测试过程发生了什么。
可展示性:相关人员可以直观明了的了解测试结果(web界面、仪表盘、折线图树状图等形式)。
真实性:测试的结果反映了客户体验到的真实的情况(真实准确的业务场景+与生产一致的配置+合理正确的测试方法)
可执行性:相关人员可以快速的进行测试验证修改调优(可定位可分析)
注解
@Benchmark
用于声明是测试
@Warmup
预热 :iterations次数,time时间
@Warmup(iterations = 1, time = 3)
@Fork(5)
用多少线程进行执行
@BenchmarkMode(Mode.Throughput)
基准测试的模式,
测试模式: Throughput(吞吐量), AverageTime(平均时间),SampleTime(在测试中,随机进行采样执行的时间),SingleShotTime(在每次执行中计算耗时),All
@Measurement (iterations = 1, time = 3)
方法调用多少次,多少时间
配置
- 添加依赖
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-generator-annprocess -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
<scope>test</scope>
</dependency>
- 配置idea
idea安装JMH插件 JMH plugin v1.0.3
配置可以识别注解
compiler -> Annotation Processors -> Enable Annotation Processing
- 写测试
需在test/下面
@Benchmark
@Warmup(iterations = 1, time = 3)
@Fork(5)
@BenchmarkMode(Mode.Throughput)
@Measurement(iterations = 1, time = 3)
public void testForEach() {
PS.foreach();
}
- 测试结果展示
# JMH version: 1.21
# VM version: JDK 1.8.0_201, Java HotSpot(TM) 64-Bit Server VM, 25.201-b09
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/jre/bin/java
# VM options: -Dfile.encoding=UTF-8
# Warmup: 1 iterations, 3 s each
# Measurement: 1 iterations, 3 s each
# Timeout: 10 min per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: com.mashibing.jmh.PSTest.testForEach
# 第一次
# Run progress: 0.00% complete, ETA 00:00:30
# Fork: 1 of 5
# Warmup Iteration 1: 7.122 ops/s
Iteration 1: 7.964 ops/s
# 第二次
# Run progress: 20.00% complete, ETA 00:00:26
# Fork: 2 of 5
# Warmup Iteration 1: 9.070 ops/s
Iteration 1: 7.902 ops/s
# 第三次
# Run progress: 40.00% complete, ETA 00:00:19
# Fork: 3 of 5
# Warmup Iteration 1: 8.458 ops/s
Iteration 1: 8.374 ops/s
# 第四次
# Run progress: 60.00% complete, ETA 00:00:13
# Fork: 4 of 5
# Warmup Iteration 1: 10.739 ops/s
Iteration 1: 10.859 ops/s
# 第五次
# Run progress: 80.00% complete, ETA 00:00:06
# Fork: 5 of 5
# Warmup Iteration 1: 8.819 ops/s
Iteration 1: 8.868 ops/s
Result "com.mashibing.jmh.PSTest.testForEach":
8.794 ±(99.9%) 4.689 ops/s [Average]
(min, avg, max) = (7.902, 8.794, 10.859), stdev = 1.218
CI (99.9%): [4.105, 13.482] (assumes normal distribution)
# Run complete. Total time: 00:00:32
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
// 最后结果
Benchmark Mode Cnt Score Error Units
PSTest.testForEach thrpt 5 8.794 ± 4.689 ops/s
Process finished with exit code 0