JMH benchmark design
What you will learn
A benchmark answers a specific performance question under specified conditions. First prove both implementations do equivalent work, then measure them. JMH is the OpenJDK microbenchmark harness; the companion local experiment is exploratory, not a substitute for JMH.
- Specify workload and invariant before timing.
- Explain dead-code elimination and constant folding.
- Design a JMH experiment with setup, forks and measured methods.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples; measurements are separate. JMH is source-reviewed, not executed.
Prove equivalent results
public class Main { static long baseline(int[] a) { return sum(a.clone()); } static long variant(int[] a) { return sum(a); } static long sum(int[] a) { long n=0; for(int x:a) n+=x; return n; } public static void main(String[] args) { int[] a={4,-2,9}; System.out.println(baseline(a)); System.out.println(variant(a)); System.out.println(java.util.Arrays.toString(a)); }}Expected output:
1111[4, -2, 9]This proves behavior, not speed. Both variants preserve the read-only input.
Define the question
For a read-only array sum, does removing an unnecessary clone change time and allocation? Both functions must accept the same data, produce the same sum and leave the array untouched. This does not justify removing defensive copies from APIs that expose mutable state. Record data size, distribution, JDK, heap and CPU limits; change one factor.
Design the benchmark
Use @State(Scope.Thread) for per-worker data, @Param for input sizes, @Setup(Level.Trial) to prepare inputs, and @Benchmark methods returning a result. A JMH Blackhole can consume multiple results. Returning a result helps prevent discarded work but cannot guarantee every optimization is avoided. Avoid foldable constant inputs, arbitrary manual loops and including unrelated setup. The companion JMH 1.37 project is source-reviewed only; dependencies require an approved environment.
Exercise varied inputs
public class Main { static long baseline(int[] a) { return sum(a.clone()); } static long variant(int[] a) { return sum(a); } static long sum(int[] a) { long n=0; for(int x:a) n+=x; return n; } public static void main(String[] args) { var random=new java.util.Random(51); int[] a=new int[32]; for(int i=0;i<a.length;i++) a[i]=random.nextInt(101)-50; int[] before=a.clone(); System.out.println(baseline(a)==variant(a)); System.out.println(java.util.Arrays.equals(a,before)); }}Expected output:
truetrueSeeded data is repeatable; it does not prove that folding is impossible.
Avoid overflow
public class Main { static long baseline(int[] a) { return sum(a.clone()); } static long variant(int[] a) { return sum(a); } static long sum(int[] a) { long n=0; for(int x:a) n+=x; return n; } public static void main(String[] args) { System.out.println(variant(new int[]{Integer.MAX_VALUE,Integer.MAX_VALUE})); System.out.println(variant(new int[0])); }}Expected output:
42949672940A long accumulator preserves the bounded int-array sum.
Common mistakes
Do not compare different outputs, discard computed values, or remove a copy required for ownership isolation.
Practice
Check equivalence for empty, mixed-sign and maximum-integer arrays. Print true if all agree.
Expected output:
trueCompare with the manual review criteria.
Further reading: JMH · Dead code · Constant folding. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JMH benchmark design — knowledge check
Performance engineering reasoning.
Two sum implementations are timed, but one changes the input array. What should happen first?
A benchmark computes a sum but discards it. Which change addresses the immediate measurement risk?
The goal is to time summation only, not input generation. Where prepare repeatable input arrays?
Two JMH workers need distinct mutable input state. Which scope fits?
An API returns a mutable array owned by the service. When is removing its defensive copy unsafe?