0%

JMH: Java微基准测试

Java Microbenchmark Harness

JMH 是 Java Microbenchmark Harness(微基准测试)

性能调优必备利器之 JMH - 武培轩 - 博客园 (cnblogs.com)

https://github.com/openjdk/jmh.git

测试项目构建

官方archetype

1
2
3
4
5
6
7
8
mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=org.openjdk.jmh \
-DarchetypeArtifactId=jmh-java-benchmark-archetype \
-DgroupId=com.tencent.demo.jmh \
-DartifactId=jmh-log \
-Dversion=1.0

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyBenchmark {

@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@Threads(value = 1)
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks. Edit as
// needed.
// Put your benchmark code here.
LogBenchMarkWorker.getInstance().logBatch();
}
}

使用

1
mvn clean verify

运行

1
java -jar target/benchmark.jar

JMH运行原理

大牛推荐的文献:

  1. https://shipilev.net
  2. https://shipilev.net/jvm/anatomy-quarks/