1.main方法
public class Sample1_HelloWorld {@Benchmark //JMH会自己找到被注解标记的方法进行测试public void wellHelloThere() {//空方法}public static void main(String[] args) throws RunnerException {Options opt = new OptionsBuilder().include(Sample1_HelloWorld.class.getSimpleName()).forks(1) //设置总共测几轮.build();new Runner(opt).run();}}
- 直接运行main方法即可
 
2. 看看执行结果
耐心等待一会儿, 就能看到结果报告了
17:36:03: Executing task ':Sample1_HelloWorld.main()'...Starting Gradle Daemon...Gradle Daemon started in 1 s 433 ms> Task :compileJava UP-TO-DATE> Task :processResources NO-SOURCE> Task :classes UP-TO-DATE> Task :Sample1_HelloWorld.main()# JMH version: 1.33# VM version: JDK 14, OpenJDK 64-Bit Server VM, 14+36-1461# VM invoker: D:\library\jdks\openjdk-14\bin\java.exe# VM options: -Dfile.encoding=UTF-8 -Duser.country=CN -Duser.language=zh -Duser.variant# Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)# Warmup: 5 iterations, 10 s each# Measurement: 5 iterations, 10 s each# Timeout: 10 min per iteration# Threads: 1 thread, will synchronize iterations# Benchmark mode: Throughput, ops/time# Benchmark: com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere# Run progress: 0.00% complete, ETA 00:01:40# Fork: 1 of 1Result# Warmup Iteration 1: 3929548185.188 ops/s# Warmup Iteration 2: 3660918456.273 ops/s# Warmup Iteration 3: 3763137692.570 ops/s# Warmup Iteration 4: 3987410602.494 ops/s# Warmup Iteration 5: 3919027111.180 ops/sIteration 1: 4018568329.775 ops/sIteration 2: 3936715837.231 ops/sIteration 3: 3776468903.377 ops/sIteration 4: 3760608499.312 ops/sIteration 5: 3945780892.039 ops/sResult "com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere":3887628492.347 ±(99.9%) 436615492.380 ops/s [Average](min, avg, max) = (3760608499.312, 3887628492.347, 4018568329.775), stdev = 113387656.839CI (99.9%): [3451012999.967, 4324243984.727] (assumes normal distribution)# Run complete. Total time: 00:01:40REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up onwhy the numbers are the way they are. Use profilers (see -prof, -lprof), design factorialexperiments, perform baseline and negative tests that provide experimental control, make surethe 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 UnitsSample1_HelloWorld.wellHelloThere thrpt 5 3887628492.347 ± 436615492.380 ops/sBUILD SUCCESSFUL in 1m 46s2 actionable tasks: 1 executed, 1 up-to-date17:37:55: Task execution finished ':Sample1_HelloWorld.main()'.
我们来大概看一下执行的结果报告
# Warmup: 5 iterations, 10 s each# Warmup Iteration 1: 3929548185.188 ops/s# Warmup Iteration 2: 3660918456.273 ops/s# Warmup Iteration 3: 3763137692.570 ops/s# Warmup Iteration 4: 3987410602.494 ops/s# Warmup Iteration 5: 3919027111.180 ops/s
- 预热了5轮, 每轮执行10秒
 
# Measurement: 5 iterations, 10 s eachIteration 1: 4018568329.775 ops/sIteration 2: 3936715837.231 ops/sIteration 3: 3776468903.377 ops/sIteration 4: 3760608499.312 ops/sIteration 5: 3945780892.039 ops/s
- 测试了5轮, 每轮执行10秒
 
Result "com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere":3887628492.347 ±(99.9%) 436615492.380 ops/s [Average](min, avg, max) = (3760608499.312, 3887628492.347, 4018568329.775), stdev = 113387656.839CI (99.9%): [3451012999.967, 4324243984.727] (assumes normal distribution)
- 这个是被测试方法的执行结果
 - 被测试的方法平均每秒执行次数: 3887628492.347 ±(99.9%) 436615492.380 ops/s
 - (min, avg, max) = (3760608499.312, 3887628492.347, 4018568329.775)
 - 标准差: stdev = 113387656.839
 - 99.9%置信区间: [3451012999.967, 4324243984.727]
 
Benchmark Mode Cnt Score Error UnitsSample1_HelloWorld.wellHelloThere thrpt 5 3887628492.347 ± 436615492.380 ops/s
- 这是此次Benchmark最终的统计报告
 - Mode: thrpt表示测试的指标是吞吐量
 - Cnt: iteration的组数, 我们这里是5, 就表示这个方法被测试了5轮
 - Score: 被统计的指标, 与 Units和Error是对应上的; 此例中是3887628492.347次/秒
 - Error: 统计指标的误差; 此例中是 ± 436615492.380
 - Units: Score与Error的单位; 此例中是 ops/s 每秒执行次数
 
