Benchmark With JMH

DZone 's Guide to

Benchmark With JMH

Test smaller units of code that are considered performance critical using JHM.

· Performance Zone ·
Free Resource

Image title

JMH stands for Java Micro-benchmark Harness and is a toolkit used for implementing benchmarks for applications running on JVM.

For those who are new to the term "benchmarking", it is a process of  identifying indicators for any software application and using these indicators to assess and maintain quality. One of the key factors of ensuring a good quality of a software product is application performance. We can consider the execution time of any software program as one of the main performance indicators. Hence, it becomes important to measure and track this indicator effectively to certify the application's quality.

Almost all software applications have small units of code which are performance-critical. For example, part of an application which involves a lot of computation and data processing is performance-critical, and measuring the execution time of these small units   is called micro-benchmarking.   These operations are executed several times in a certain environment with a standard CPU and memory configuration to produce consistent results. The execution time of each run  is  recorded and  consolidated to obtain a mean or average time, known as micro-benchmarks . 

Using JMH to create benchmarks is fairly simple. A lot of documentation and resources are available over the Internet, and you can follow these steps to implement a simple benchmark project.

Developing Benchmarks Using JMH 

  A benchmark project can be generated using a simple Maven command:

mvn archetype:generate \
          -DinteractiveMode=false \
          -DarchetypeGroupId=org.openjdk.jmh \
          -DarchetypeArtifactId=jmh-java-benchmark-archetype \
          -DgroupId=org.sample \
          -DartifactId=test \

And adding  a couple of Maven dependencies:


Create the benchmark Java class and test methods annotated with JMH annotations. Invoke the API, method, or application for which you want to generate the benchmark results.

For example:

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;

public class MySimpleBenchmark {

   private MathHelper mathHelper;

public void benchmarkAddMethod()
mathHelper.add(10, 15);

Next, build this project using this Maven command:

mvn clean install

Execute the generated benchmark.jar:

java -jar target/benchmarks.jar

Here is an example of results from benchmarking using JMH with 25 integers and 1,000 integers.

JMH Benchmark to Sort 25 Integers

Benchmark                          Mode  Cnt         Score        Error  Units

 JMHSortBenchmark.arraysSort       thrpt   20  28779058.746   446412.014  ops/s 
 JMHSortBenchmark.collectionsSort  thrpt   20  26070145.869   206077.160  ops/s 

JMH Benchmark to Sort 1,000 Integers

Benchmark                          Mode  Cnt        Score       Error  Units

 JMHSortBenchmark.arraysSort       thrpt   20  3795959.757   43679.226  ops/s 
 JMHSortBenchmark.collectionsSort  thrpt   20   853014.250    6256.061  ops/s 

You can refer to a sample benchmark project here.

Here and here are some references on GitHub to the benchmarks developed by Spring.

benchmarking ,microbenchmark ,jmh ,performance engineering ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}