Sub topic 9
Comparing input sizes with @Param
This chapter assumes you can write a basic @Benchmark method and use @State and @Setup. You will use @Param to run one benchmark against several input values without copying the code.
Why parameterize
Performance often depends on input size. A loop over 100 elements fits in the CPU cache, and a loop over 10 million may not. If you hard-code one size, you measure one point. With @Param, JMH runs the same benchmark once for each value and reports each result on its own line.
Declaring a parameter
Put @Param on a field of a @State class. List the values as strings. JMH converts each string to the field's type, such as int, long, boolean, an enum or String.
package org.sample;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class SizeBenchmark {
@Param({"100", "10000"})
public int size;
private int[] data;
@Setup(Level.Trial)
public void setUp() {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = i;
}
}
@Benchmark
public long sum() {
long total = 0;
for (int value : data) {
total += value;
}
return total;
}
}JMH assigns size before it calls @Setup. This is why setUp can use size to build the array. Each parameter value gets its own trial, so Level.Trial setup runs once per value.
Combining several parameters
With two or more @Param fields, JMH runs the cross product of all values.
package org.sample;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
@State(Scope.Benchmark)
public class ListBenchmark {
@Param({"1000", "100000"})
public int size;
@Param({"ArrayList", "LinkedList"})
public String type;
private List<Integer> list;
@Setup(Level.Trial)
public void setUp() {
list = type.equals("ArrayList") ? new ArrayList<>() : new LinkedList<>();
for (int i = 0; i < size; i++) {
list.add(i);
}
}
@Benchmark
public long sum() {
long total = 0;
for (int value : list) {
total += value;
}
return total;
}
}This produces 2 × 2 = 4 combinations. The results table has one row per combination, with the parameter values in extra columns.
| Benchmark | (size) | (type) | Mode | Score | Error | Units |
|---|---|---|---|---|---|---|
| ListBenchmark.sum | 1000 | ArrayList | avgt | *your result* | *± error* | us/op |
Overriding values from the command line
The -p option replaces the annotation values for one run:
java -jar target/benchmarks.jar SizeBenchmark -p size=1000,100000This is useful for trying a size without editing and rebuilding the code.
Common errors
- Run time grows quickly. Total time is roughly combinations × forks × (warmup + measurement). Four combinations with two forks means eight separate JVM runs.
- Wrong field declaration. A
@Paramfield must be non-final and non-private, and its class must be a@Stateclass. - Bad conversion. A value that does not parse to the field type, such as
"abc"for anint, stops the run with an error. - Unfair comparison. If one parameter changes allocation or warmup behavior, compare the results with care.
Exercise
A benchmark class has @Param({"10", "100", "1000"}) int size and @Param({"true", "false"}) boolean sorted, and runs with @Fork(2). How many JVM forks does JMH start in total?
Show answer
The cross product gives 3 × 2 = 6 combinations. Each runs with 2 forks, so JMH starts 12 forked JVMs.