Skip to main content

Development

Java Microbenchmark Harness

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:

  1. 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”

Steps for creating Benchmark using JMH

  • Update the pom.xml file with below dependencies

Steps for creating Benchmark using JMH

  • 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.

Steps for creating Benchmark using JMH

  • 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.Steps for creating Benchmark using JMH

 

References

http://openjdk.java.net/projects/code-tools/jmh/

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Rajasekar Chockalingam

More from this Author

Categories
Follow Us