- Mastering High Performance with Kotlin
- Igor Kucherenko
- 353字
- 2021-06-25 20:55:24
Benchmark modes
You can find examples of how to use the JMH at the following link: http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/. One way is by using the JMHSample_02_BenchmarkModes class, which demonstrates the use of different modes. There are five modes:
- Mode.Throughput: This means that the benchmark method is being continuously invoked in a time-bound iteration and calculates how many times this method can be invoked.
Mode.AverageTime: This measures similarly to Mode.Throughput, but calculates the average execution time.
- Mode.SampleTime: This measures similarly to Mode.Throughput, but samples the execution time for time-bound iterations.
- Mode.SingleShotTime: This measures the invocation time of a single method, which can be useful for cold startup testing. Using this mode, you specify that you don't want to call the benchmark method continuously.
- Mode.All: This does all of the above. Multiple modes let you specify modes that you want separated by commas—@BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime, Mode.SingleShotTime}).
We could rewrite our testMethod() as follows:
@Benchmark
@BenchmarkMode(Mode.AverageTime)
public void testMethod() {
int a = 3;
int b = 4;
int c = a + b;
}
And by running it with five iterations and a single fork, the output will be as follows:
# Run progress: 0.00% complete, ETA 00:00:10
# Fork: 1 of 1
# Warmup Iteration 1: ≈ 10?? s/op
# Warmup Iteration 2: ≈ 10?? s/op
# Warmup Iteration 3: ≈ 10?? s/op
# Warmup Iteration 4: ≈ 10?? s/op
# Warmup Iteration 5: ≈ 10?? s/op
Iteration 1: ≈ 10?? s/op
Iteration 2: ≈ 10?? s/op
Iteration 3: ≈ 10?? s/op
Iteration 4: ≈ 10?? s/op
Iteration 5: ≈ 10?? s/op
Result "org.sample.MyBenchmark.testMethod":
≈ 10?? s/op
# Run complete. Total time: 00:00:10
Benchmark Mode Cnt Score Error Units
MyBenchmark.testMethod avgt 5 ≈ 10?? s/op
This means that the average execution time for testMethod() is 10-9 which is equal to 0.000000001 seconds. If you want to get the results in another time unit, you have to use the @OutputTimeUnit annotation.
推薦閱讀
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Java多線程編程實戰指南:設計模式篇(第2版)
- C#完全自學教程
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- 跟小海龜學Python
- PHP網絡編程學習筆記
- 機械工程師Python編程:入門、實戰與進階
- Java程序設計:原理與范例
- Learning Salesforce Einstein
- 第一行代碼 C語言(視頻講解版)
- Java面向對象程序設計
- Python一行流:像專家一樣寫代碼
- Machine Learning for OpenCV
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- Mastering Node.js