Sub topic 13
Running benchmarks from the command line and from code
This chapter assumes you have built target/benchmarks.jar from the JMH Maven archetype and that SumBenchmark from the previous chapters exists. JMH can run that jar from a terminal, or you can start the same benchmarks from a Java main method.
Running the jar
The archetype builds a self-contained jar. With no arguments, it runs every benchmark in the project, which is slow once you have several.
mvn clean verify
java -jar target/benchmarks.jarPass a regular expression to select benchmarks. JMH matches it against the fully qualified benchmark name, such as org.sample.SumBenchmark.sum.
java -jar target/benchmarks.jar SumBenchmark -wi 3 -i 5 -f 1 -prof gcThe flags in that command do the following:
| Flag | Meaning |
|---|---|
| `-wi 3` | Three warmup iterations |
| `-i 5` | Five measurement iterations |
| `-f 1` | One fork, meaning one fresh JVM |
| `-prof gc` | Add the GC allocation profiler |
Other flags you will use often:
-llists the benchmarks that match, without running them.-p size=100overrides a@Paramvalue.-bm avgtand-tu nschange the mode and time unit.-rf json -rff results.jsonwrites results to a file.-hprints every option.
Running from code
The Runner API does the same job from a main method. This is convenient in an IDE, where you can start it with one click.
package org.sample;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class BenchmarkRunner {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(SumBenchmark.class.getSimpleName())
.warmupIterations(3)
.measurementIterations(5)
.forks(1)
.param("size", "100")
.addProfiler(GCProfiler.class)
.build();
new Runner(options).run();
}
}include takes the same kind of regular expression as the command line. Each builder method matches a command-line flag.
Which setting wins
Settings come from three places. Annotations on the class or method are the defaults. Options passed to the runner override them, and command-line flags work as runner options. If SumBenchmark says @Fork(2) and you pass -f 1, JMH uses one fork.
Common errors
- No benchmarks run, or the list is empty. Your regular expression matched nothing. Check it with
-l. - Running
mainfrom an IDE finds no benchmarks. JMH needs its annotation processor to generate benchmark code. Build with Maven, or enable annotation processing in the IDE. - Setting
forks(0)to make runs faster. This runs the benchmark inside the launching JVM. Results are less reliable because of profile pollution from earlier code. Use it only for debugging.
Exercise
You run java -jar target/benchmarks.jar SumBenchmark -f 1 -p size=10000. The class has @Fork(2) and @Param({"100", "10000"}). How many forks run, and how many parameter values are benchmarked?
Show answer
One fork runs, because the command-line -f 1 overrides the @Fork(2) annotation. Only size=10000 is benchmarked, because -p replaces the values listed in @Param.