Backend Development 14 min read

Using JMH for Java Microbenchmarking: Demo Project, Annotations and Result Analysis

This article explains why simple timing is unreliable in Java due to JIT compilation, introduces the official JMH tool for microbenchmarking, outlines best‑practice tips, demonstrates how to set up a Maven project, write benchmark code, run tests, interpret results, and details each JMH annotation.

Top Architect
Top Architect
Top Architect
Using JMH for Java Microbenchmarking: Demo Project, Annotations and Result Analysis

In daily development, measuring code performance is essential, but the JVM’s mixed JIT and interpreter execution makes simple timing unreliable because hot code is continuously compiled and optimized.

JMH (Java Microbenchmark Harness) is an official OpenJDK tool designed for precise micro‑benchmarks at the method level, with accuracy down to microseconds.

Key points for reliable Java benchmarks include warming up the JVM, preventing dead‑code elimination, testing concurrency, and presenting results clearly.

Typical JMH use cases are quantifying optimization effects of hot methods, measuring execution time relative to input variables, and comparing multiple implementations of a function.

Demo: build a JMH test project using Maven archetype:

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

Alternatively, add the JMH dependencies to an existing Maven project:

<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>${jmh.version}</version>
</dependency>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>${jmh.version}</version>
    <scope>provided</scope>
</dependency>

Write a benchmark class that compares LinkedList iteration by index versus foreach, using annotations such as @State, @Setup, @Benchmark, @BenchmarkMode, @OutputTimeUnit, @Threads, etc.:

/**
 * @author Richard_yyf
 */
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.SECONDS)
@Threads(Threads.MAX)
public class LinkedListIterationBenchMark {
    private static final int SIZE = 10000;
    private List
list = new LinkedList<>();

    @Setup
    public void setUp() {
        for (int i = 0; i < SIZE; i++) {
            list.add(String.valueOf(i));
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void forIndexIterate() {
        for (int i = 0; i < list.size(); i++) {
            list.get(i);
            System.out.print("");
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void forEachIterate() {
        for (String s : list) {
            System.out.print("");
        }
    }
}

Run the benchmark either by building a jar and executing it:

$ mvn clean install
$ java -jar target/benchmarks.jar

or directly from an IDE using a main method with OptionsBuilder:

public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
        .include(LinkedListIterationBenchMark.class.getSimpleName())
        .forks(1)
        .warmupIterations(2)
        .measurementIterations(2)
        .output("E:/Benchmark.log")
        .build();
    new Runner(opt).run();
}

Sample output shows that foreach iteration achieves about 1192 ops/s while index iteration reaches only about 207 ops/s, illustrating the performance difference.

The article then provides a detailed annotation guide, explaining the purpose and usage of @BenchmarkMode, @Warmup, @Measurement, @Threads, @Fork, @OutputTimeUnit, @Benchmark, @Param, @Setup, @TearDown, and @State, each with example code snippets.

In conclusion, JMH is a powerful tool for accurately measuring Java code performance and can be applied to benchmark logging frameworks, BeanCopy utilities, and many other libraries.

JavaPerformancemavenAnnotationsBenchmarkingMicrobenchmarkJMH
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.