Sub topic 4
Creating a JMH project
This chapter assumes you can read Java classes and annotations, run Maven from a terminal, and know that the JVM compiles hot code at runtime. By the end you will have a project that builds a runnable benchmark jar.
Why use the archetype
JMH is a normal library, but a benchmark needs more than a dependency. JMH generates extra code at build time, and each benchmark runs in a fresh forked JVM. That JVM needs a classpath it can find on its own. The JMH Maven archetype (a project template) sets all of this up. The result is one self-contained jar that holds your benchmarks, JMH, and the generated code.
Generate the project
Run these commands in an empty working directory:
mvn archetype:generate \
-DinteractiveMode=false \
-DarchetypeGroupId=org.openjdk.jmh \
-DarchetypeArtifactId=jmh-java-benchmark-archetype \
-DgroupId=org.sample \
-DartifactId=benchmarks \
-Dversion=1.0
cd benchmarks
mvn clean verify
java -jar target/benchmarks.jarThe first command creates a benchmarks directory. mvn clean verify compiles the code and packages it. java -jar target/benchmarks.jar runs every benchmark in the project, so the first run can take several minutes.
What the archetype gives you
The generated project has a small layout:
pom.xmldeclares thejmh-coredependency and thejmh-generator-annprocessannotation processor. It also configures the Maven Shade plugin, which buildstarget/benchmarks.jar.src/main/java/org/sample/MyBenchmark.javaholds a placeholder class with one empty@Benchmarkmethod.
The annotation processor reads your @Benchmark methods during compilation. It writes generated classes and a list of benchmarks to META-INF/BenchmarkList. At run time, JMH reads that list to find what to run. The shade plugin then packs everything into one jar.
Rebuild after every change
The jar contains compiled code. If you edit a benchmark and run the old jar, you measure the old code. Rebuild each time:
mvn clean verify
java -jar target/benchmarks.jarCommon errors
- "Unable to find the resource: /META-INF/BenchmarkList". The annotation processor did not run. This usually happens when you run benchmarks from an IDE or from plain
mvn execwithout annotation processing enabled. Build the jar with Maven, or turn on annotation processing in the IDE. - Benchmarks seem to ignore your edits. You ran a stale jar. Run
mvn clean verifyagain. - Unexpected compiler or Java version errors. Check the compiler settings in the generated
pom.xmland make sure they match the JDK you run. You can edit them like any other Maven property.
Next step
The empty MyBenchmark method measures nothing useful yet. In the next chapter you will replace it with a real @Benchmark method.
Exercise
You edit a benchmark method, then run java -jar target/benchmarks.jar without rebuilding. Which code does JMH measure, and why?
Show answer
It measures the old code. The jar holds the compiled classes from the last build, so the edit only takes effect after mvn clean verify produces a new target/benchmarks.jar.