Use JMH for micro benchmarking
Purpose and introduction
- Sometimes we want to test the execution efficiency of a method, a function, and a class. The most earthly method is to make a time difference:
long timebegin = System.nanoTime();
for(int timei = 0; timei < 10; timei++){
do somethings;
}
long timeend = System.nanoTime();
System.out.println("cost time:" + (timeend - timebegin));
- Although this method is convenient and simple, it is rough and inaccurate. Today, we will introduce JMH, a Java microbenchmark test tool, which can accurately give the actual efficiency of this method.
Construction and design ideas
- Import JMH jar package
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.19</version>
<scope>provided</scope>
</dependency>
</dependencies>
- Main note
@BenchmarkMode
@OutputTimeUnit
@Iteration
@WarmUp
@State
@Fork
@Meansurement
@Setup
@TearDown
@Benchmark
@Param
Actual use case
- Explanation: I want to test the efficiency of the twelve sorting methods I wrote , so I respectively are as follows:
- Main method
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class InnerSortRealizeTimeTest {
@Test
public void sortTimeTest() throws RunnerException {
Options options = new OptionsBuilder().
include("SortTest")//此处是模糊匹配
.warmupIterations(2)//预热2轮
.measurementIterations(2)//实际2轮
.forks(2)//预热2轮+实际2轮算一组,这是循环2组
.build();
new Runner(options).run();
}
}
- Add to all methods to be tested
@Benchmark
@BenchmarkMode(Mode.Throughput)
- Test Results
Benchmark Mode Cnt Score Error Units
normalSortTest.BucketSortTest.TestInnerSortRealize thrpt 4 5235.110 ± 2652.157 ops/s
normalSortTest.CountSortTest.TestInnerSortRealize thrpt 4 9764.645 ± 6352.699 ops/s
normalSortTest.MergeSortTest.TestInnerSortRealize thrpt 4 2833.303 ± 483.073 ops/s
normalSortTest.RadixSortTest.TestInnerSortRealize thrpt 4 3862.049 ± 3743.130 ops/s
normalSortTest.ShellSortTest.TestInnerSortRealize thrpt 4 4150.777 ± 237.711 ops/s
quickSortTest.QuickSortDuplexingTest.TestInnerSortRealize thrpt 4 15079.496 ± 7921.672 ops/s
quickSortTest.QuickSortOptTest.TestInnerSortRealize thrpt 4 19513.197 ± 12820.824 ops/s
quickSortTest.QuickSortSimplexTest.TestInnerSortRealize thrpt 4 4347.717 ± 1105.995 ops/s
simpleSortTest.BubbleSortTest.TestInnerSortRealize thrpt 4 1780.624 ± 82.313 ops/s
simpleSortTest.HeapSortTest.TestInnerSortRealize thrpt 4 6892.012 ± 2643.572 ops/s
simpleSortTest.InsertSortTest.TestInnerSortRealize thrpt 4 9119.023 ± 3375.271 ops/s
simpleSortTest.SelectSortTest.TestInnerSortRealize thrpt 4 3108.758 ± 284.871 ops/s
-
in conclusion
- The sorting of the insertion change is the best, 19513.197 ± 12820.824 times per second, but ± 12820.824 also indicates that the algorithm is unstable every time.
- The bubble sort performance is the most stable, at 1,780.624 ± 82.313 times per second.
-
JMH trap
-
JMH plugin
-
JMH visualization
-
Please refer to the following blog:
-
Reference blog