Introduction to JMH – Java Microbenchmark Harness
Level of expertise required to implement: Experienced in Java and Maven.
Concept
Benchmark enables us to correctly measure the performance of small parts of the application. The JMH toolkit helps us to compare different implementations for the same problem which in turn helps with code optimization.
What is JMH?
JMH, provided by openJDK, is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java. The recommended method for performing benchmarks is to create a maven project as a standalone application.
Prerequsites:
- Download Maven and set classpath manually
Steps for creating Benchmark using JMH
- Use the below command with no line breaks to create a new maven project. The command will create a new project “Test-Benchmark”
- Update the pom.xml file with below dependencies
- The maven command will create a java package named “com.raj” under “Test-Benchmark\src\main\java”. Create a java program, for example, “MyBenchmark.java” under it. In the below program we are generating a list of random numbers and we are going to measure the performance of the default Arrays.sort() method.
- Use “mvn clean install exec:exec > result.log” run the above java program from the commandline. The command will create the “result.log” file with benchmark results. Find below the content of the file “result.log”. The default benchmark mode is 1(Throughput – operations per unit of time) and in this case higher value is better.
References
http://openjdk.java.net/projects/code-tools/jmh/