tchigher / jmh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Java Microbenchmark Harness (JMH)

JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targeting the JVM.


Basic Considerations

The recommended way to run a JMH benchmark is to use Maven to setup a standalone project that depends on the jar files of your application. This approach is preferred to ensure that the benchmarks are correctly initialized and produce reliable results. It is possible to run benchmarks from within an existing project, and even from within an IDE, however setup is more complex and the results are less reliable.

In all cases, the key to using JMH is enabling the annotation- or bytecode-processors to generate the synthetic benchmark code. Maven archetypes are the primary mechanism used to bootstrap the project that has the proper build configuration. We strongly recommend new users make use of the archetype to setup the correct environment.


In order to understand JMH tests and maybe write your own, it might be useful to work through the JMH Samples. See the test comments for run instructions.

Preferred Usage: Command Line

Step 1. Setting up the benchmarking project. The following command will generate the new JMH-driven project in test folder:

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

If you want to benchmark an alternative JVM language, use another archetype artifact ID from the list of existing ones, it usually amounts to replacing java to another language in the artifact ID given above. Using alternative archetypes may require additional changes in the build configuration, see the pom.xml in the generated project.

Step 2. Building the benchmarks. After the project is generated, you can build it with the following Maven command:

$ cd test/
$ mvn clean verify

Step 3. Running the benchmarks. After the build is done, you will get the self-contained executable JAR, which holds your benchmark, and all essential JMH infrastructure code:

$ java -jar target/benchmarks.jar

Run with -h to see the command line options available.

When dealing with large projects, it is customary to keep the benchmarks in a separate sub-project, which then depends on the tested modules via the usual build dependencies.

IDE Support

While the command line approach is the suggested one, some people prefer to use IDEs. The user experience varies between different IDEs, but we will outline the general things here. Running benchmarks from the IDE is generally not recommended due to generally uncontrolled environment in which the benchmarks run.

Step 1. Setting up the benchmarking project. Some IDEs provide the GUI to create the Maven project from the given archetype. Make sure your IDE knows about Central archetype catalog, and look for org.openjdk.jmh:jmh-${lang}-benchmark-archetype there. Alternatively, you can use the command line to generate the benchmark project, see above.

NOTE: JMH is not intended to be used in the same way as a typical testing library such as JUnit. Simply adding the jmh-core jar file to your build is not enough to be able to run benchmarks.

Step 2. Building the benchmarks. Most IDEs are able to open/import Maven projects, and infer the build configuration from Maven project configuration. IDEA and NetBeans are able to build JMH benchmark projects with little to no effort. Eclipse build configuration may need to set up JMH annotation processors to run.

Step 3. Running the benchmarks. There is no direct support for JMH benchmarks in the IDE, but one can use JMH Java API to invoke the benchmark. It usually amounts to having the main method, which will then call into JMH. See JMH Samples for the examples of this approach. Before you run any benchmark, the project build is required. Most IDEs do this automatically, but some do require explicit build action to be added before the run: adding Maven target verify should help there.

Other Build Systems

JMH project does not ship the build scripts for build systems other that Maven. But there are community-supported bindings:

If you want to build with an alternative build system, you may want to see the Ant sample which describes the steps to build JMH benchmark projects.

Support and Development

Pre-Requisite Steps

Make sure you did this before publishing the benchmark, and/or requesting the JMH feature:

  • JMH annotations Javadocs and Samples are essential reading. Follow the JMH Samples to get familiar with the API, use cases, culprits, and pitfalls in building the benchmarks and using JMH.
  • Your benchmarks should be peer-reviewed. Do not assume that a nice harness will magically free you from considering benchmarking pitfalls. We only promise to make avoiding them easier, not avoiding them completely.

Make sure you tried these things before getting support:

  • Archetypes provide the golden build configuration. Try to generate the clean JMH benchmark project and transplant the benchmark there. This is important to try when upgrading to the newer JMH versions, since the minute differences in the build configurations may attribute to the failures you are seeing.
  • Current development code is usually leaner, meaner, and better. Make sure you are running the latest JMH version, and/or try to run with bleeding edge JMH to see if the issue is already fixed.
  • See if your question/issue was discussed already. Look around mailing list archives to see if there is already an answer.

If all these did not help, you are welcome to report the issue.

Reporting Harness and Test Bugs

If you have the access to JDK Bug System, please submit the bug there:

  • Project: CODETOOLS
  • Component: tools
  • Sub-component: jmh

If you don't have the access to JDK Bug System, submit the bug report at "Issues" here, and wait for maintainers to pick that up.


JMH project accepts pull requests, like other OpenJDK projects. If you have never contributed to OpenJDK before, then bots would require you to sign OCA first. Normally, you don't need to post patches anywhere else, or post to mailing lists, etc. If you do want to have a wider discussion about JMH, please refer to jmh-dev.

Short instructions to build, test bleeding-edge JMH, and install its JAR to local Maven repo:

$ mvn clean install

If you already have the benchmark project, then it is enough to change JMH dependencies version to the latest SNAPSHOT version (look up the actual latest version in root pom.xml). If not, create the JMH benchmark project and change the version there.

GitHub workflow "JMH Pre-Integration Tests" should pass on the changes. It would be triggered for PRs. You can also trigger it manually for your branch.

Related projects

These projects are supported by community, not by OpenJDK/JMH developers.


License:GNU General Public License v2.0


Language:Java 99.5%Language:Ruby 0.3%Language:Groovy 0.1%Language:Kotlin 0.1%Language:Scala 0.1%