1.main方法

  1. public class Sample1_HelloWorld {
  2. @Benchmark //JMH会自己找到被注解标记的方法进行测试
  3. public void wellHelloThere() {
  4. //空方法
  5. }
  6. public static void main(String[] args) throws RunnerException {
  7. Options opt = new OptionsBuilder()
  8. .include(Sample1_HelloWorld.class.getSimpleName())
  9. .forks(1) //设置总共测几轮
  10. .build();
  11. new Runner(opt).run();
  12. }
  13. }
  • 直接运行main方法即可

2. 看看执行结果

耐心等待一会儿, 就能看到结果报告了

  1. 17:36:03: Executing task ':Sample1_HelloWorld.main()'...
  2. Starting Gradle Daemon...
  3. Gradle Daemon started in 1 s 433 ms
  4. > Task :compileJava UP-TO-DATE
  5. > Task :processResources NO-SOURCE
  6. > Task :classes UP-TO-DATE
  7. > Task :Sample1_HelloWorld.main()
  8. # JMH version: 1.33
  9. # VM version: JDK 14, OpenJDK 64-Bit Server VM, 14+36-1461
  10. # VM invoker: D:\library\jdks\openjdk-14\bin\java.exe
  11. # VM options: -Dfile.encoding=UTF-8 -Duser.country=CN -Duser.language=zh -Duser.variant
  12. # Blackhole mode: full + dont-inline hint (default, use -Djmh.blackhole.autoDetect=true to auto-detect)
  13. # Warmup: 5 iterations, 10 s each
  14. # Measurement: 5 iterations, 10 s each
  15. # Timeout: 10 min per iteration
  16. # Threads: 1 thread, will synchronize iterations
  17. # Benchmark mode: Throughput, ops/time
  18. # Benchmark: com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere
  19. # Run progress: 0.00% complete, ETA 00:01:40
  20. # Fork: 1 of 1Result
  21. # Warmup Iteration 1: 3929548185.188 ops/s
  22. # Warmup Iteration 2: 3660918456.273 ops/s
  23. # Warmup Iteration 3: 3763137692.570 ops/s
  24. # Warmup Iteration 4: 3987410602.494 ops/s
  25. # Warmup Iteration 5: 3919027111.180 ops/s
  26. Iteration 1: 4018568329.775 ops/s
  27. Iteration 2: 3936715837.231 ops/s
  28. Iteration 3: 3776468903.377 ops/s
  29. Iteration 4: 3760608499.312 ops/s
  30. Iteration 5: 3945780892.039 ops/s
  31. Result "com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere":
  32. 3887628492.347 ±(99.9%) 436615492.380 ops/s [Average]
  33. (min, avg, max) = (3760608499.312, 3887628492.347, 4018568329.775), stdev = 113387656.839
  34. CI (99.9%): [3451012999.967, 4324243984.727] (assumes normal distribution)
  35. # Run complete. Total time: 00:01:40
  36. REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
  37. why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
  38. experiments, perform baseline and negative tests that provide experimental control, make sure
  39. the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
  40. Do not assume the numbers tell you what you want them to tell.
  41. Benchmark Mode Cnt Score Error Units
  42. Sample1_HelloWorld.wellHelloThere thrpt 5 3887628492.347 ± 436615492.380 ops/s
  43. BUILD SUCCESSFUL in 1m 46s
  44. 2 actionable tasks: 1 executed, 1 up-to-date
  45. 17:37:55: Task execution finished ':Sample1_HelloWorld.main()'.

我们来大概看一下执行的结果报告

  1. # Warmup: 5 iterations, 10 s each
  2. # Warmup Iteration 1: 3929548185.188 ops/s
  3. # Warmup Iteration 2: 3660918456.273 ops/s
  4. # Warmup Iteration 3: 3763137692.570 ops/s
  5. # Warmup Iteration 4: 3987410602.494 ops/s
  6. # Warmup Iteration 5: 3919027111.180 ops/s
  • 预热了5轮, 每轮执行10秒
  1. # Measurement: 5 iterations, 10 s each
  2. Iteration 1: 4018568329.775 ops/s
  3. Iteration 2: 3936715837.231 ops/s
  4. Iteration 3: 3776468903.377 ops/s
  5. Iteration 4: 3760608499.312 ops/s
  6. Iteration 5: 3945780892.039 ops/s
  • 测试了5轮, 每轮执行10秒
  1. Result "com.zyj.benchmark.Sample1_HelloWorld.wellHelloThere":
  2. 3887628492.347 ±(99.9%) 436615492.380 ops/s [Average]
  3. (min, avg, max) = (3760608499.312, 3887628492.347, 4018568329.775), stdev = 113387656.839
  4. CI (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]
  1. Benchmark Mode Cnt Score Error Units
  2. Sample1_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 每秒执行次数