• Stars
    star
    1,857
  • Rank 24,974 (Top 0.5 %)
  • Language
    Java
  • License
    GNU General Publi...
  • Created over 5 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

https://openjdk.org/projects/code-tools/jmh

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.

Usage

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.

Samples

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 \
  -Dversion=1.0

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 OpenJDK Bug System, please submit the bug there:

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

Alternatively, you can join the JMH Mailing List and report the bugs there.

Development

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.

More Repositories

1

jdk

JDK main-line development https://openjdk.org/projects/jdk
Java
17,577
star
2

jfx

JavaFX mainline development
C++
2,700
star
3

loom

https://openjdk.org/projects/loom
Java
1,873
star
4

jmc

Repository for OpenJDK Mission Control, a production time profiling and diagnostics tools suite. https://openjdk.org/projects/jmc
Java
836
star
5

jol

https://openjdk.org/projects/code-tools/jol
Java
810
star
6

valhalla

https://openjdk.org/projects/valhalla
Java
538
star
7

nashorn

https://openjdk.org/projects/nashorn
Java
399
star
8

jcstress

https://openjdk.org/projects/code-tools/jcstress
Java
368
star
9

panama-foreign

https://openjdk.org/projects/panama
Java
291
star
10

jextract

https://openjdk.org/projects/code-tools
Java
237
star
11

skara

https://openjdk.org/projects/skara
Java
203
star
12

jdk8u

https://wiki.openjdk.org/display/jdk8u
Java
187
star
13

wakefield

https://openjdk.org/projects/wakefield
Java
186
star
14

amber

https://openjdk.org/projects/amber
Java
181
star
15

crac

https://openjdk.org/projects/crac
Java
175
star
16

jdk17

https://openjdk.org/projects/jdk/17 released 2021-09-14
Java
164
star
17

zgc

The Z Garbage Collector https://wiki.openjdk.org/display/zgc
Java
164
star
18

amber-docs

https://openjdk.org/projects/amber
HTML
144
star
19

mobile

https://openjdk.org/projects/mobile
Java
134
star
20

jdk11u

https://openjdk.org/projects/jdk-updates
Java
130
star
21

shenandoah

https://openjdk.org/projects/shenandoah
Java
117
star
22

leyden

https://openjdk.org/projects/leyden
Java
97
star
23

panama-vector

https://openjdk.org/projects/panama
Java
82
star
24

shenandoah-visualizer

https://openjdk.org/projects/shenandoah
Java
78
star
25

jdk17u

https://wiki.openjdk.org/display/JDKUpdates/JDK+17u
Java
76
star
26

aarch64-port

Port: AArch64 Project
Java
71
star
27

babylon

https://openjdk.org/projects/babylon
Java
69
star
28

asmtools

https://wiki.openjdk.org/display/CodeTools/asmtools
Java
66
star
29

jdk21

https://openjdk.org/projects/jdk/21 released 2023-09-19
Java
65
star
30

valhalla-docs

https://openjdk.org/projects/valhalla
CSS
59
star
31

jdk-sandbox

JDK Committers Sandbox
Java
59
star
32

guide

OpenJDK Developers' Guide https://openjdk.org/guide
Makefile
58
star
33

jdk16

https://openjdk.org/projects/jdk/16 released 2021-03-16
Java
57
star
34

jtreg

https://openjdk.org/projects/code-tools/jtreg
Java
56
star
35

jdk11u-dev

https://openjdk.org/projects/jdk-updates
Java
54
star
36

lilliput

https://openjdk.org/projects/lilliput
Java
50
star
37

riscv-port

https://openjdk.org/projects/riscv-port
Java
46
star
38

jdk8

https://openjdk.org/projects/jdk8 released 2014-03-18
Java
40
star
39

jdk8u-dev

https://wiki.openjdk.org/display/jdk8u
Java
40
star
40

jdk15

https://openjdk.org/projects/jdk/15 released 2020-09-15
Java
39
star
41

jdk19

https://openjdk.org/projects/jdk/19 released 2022-09-20
Java
39
star
42

jmh-jdk-microbenchmarks

https://openjdk.org/projects/code-tools/jmh-jdk-microbenchmarks
Java
38
star
43

jdk17u-dev

https://openjdk.org/projects/jdk-updates
Java
32
star
44

jdk13

https://openjdk.org/projects/jdk/13 released 2019-09-17
Java
32
star
45

jdk21u

https://openjdk.org/projects/jdk-updates
Java
30
star
46

jdk16u

https://openjdk.org/projects/jdk-updates last released 2021-07-20
Java
26
star
47

lanai

https://openjdk.org/projects/lanai
Java
26
star
48

tsan

https://openjdk.org/projects/tsan
Java
25
star
49

portola

https://openjdk.org/projects/portola
Java
24
star
50

jdk18

https://openjdk.org/projects/jdk/18 released 2022-03-22
Java
23
star
51

duke

https://openjdk.org/projects/duke
Mathematica
21
star
52

jdk19u

https://openjdk.org/projects/jdk-updates last released 2023-01-17
Java
20
star
53

jdk11

https://openjdk.org/projects/jdk/11 released 2018-09-25
20
star
54

jdk20

https://openjdk.org/projects/jdk/20 released 2023-03-21
Java
20
star
55

jdk13u-dev

https://openjdk.org/projects/jdk-updates development (identical to jdk13u)
Java
17
star
56

jcov

https://wiki.openjdk.org/display/CodeTools/jcov
Java
17
star
57

jdk14u

https://openjdk.org/projects/jdk-updates last released 2020-07-14
Java
16
star
58

jdk12u

https://openjdk.org/projects/jdk-updates last released 2019-07-16
Java
16
star
59

metropolis

https://openjdk.org/projects/metropolis
Java
16
star
60

jdk20u

https://openjdk.org/projects/jdk-updates last released 2023-07-18
Java
15
star
61

jdk14

https://openjdk.org/projects/jdk/14 released 2020-03-17
Java
15
star
62

galahad

https://openjdk.org/projects/galahad
Java
15
star
63

jfx-sandbox

OpenJFX Committers Sandbox
C++
14
star
64

jdk6

https://openjdk.org/projects/jdk6
Java
14
star
65

jtharness

https://wiki.openjdk.org/display/CodeTools/JT+Harness
Java
13
star
66

jdk7u

https://wiki.openjdk.org/display/jdk7u last released 2022-07-19
Java
12
star
67

jfx11u

JavaFX 11 updates development
C++
12
star
68

jmc7

https://openjdk.org/projects/jmc
Java
12
star
69

jdk15u

https://wiki.openjdk.org/display/JDKUpdates/JDK+15u last released 2023-01-18
Java
11
star
70

jemmy-v2

UI test automation library - v2
Java
11
star
71

jfx20u

JavaFX 20.x: last released 2023-07-18
C++
10
star
72

jdk7

https://openjdk.org/projects/jdk7 released 2011-07-28
Java
10
star
73

jdk12

https://openjdk.org/projects/jdk/12 released 2019-03-19
8
star
74

webrevs

Automatically generated JSON files for webrevs
8
star
75

jdk15u-dev

https://openjdk.org/projects/jdk-updates development (identical to jdk15u)
Java
8
star
76

jfx21u

JavaFX 21 updates development
C++
7
star
77

jdk13u

https://wiki.openjdk.org/display/JDKUpdates/JDK+13u last released 2023-01-18
Java
7
star
78

client

JDK 16 era Client Libraries development
Java
7
star
79

jfx17u

JavaFX 17 updates development
C++
6
star
80

jdk18u

https://openjdk.org/projects/jdk-updates last released 2022-08-18
Java
6
star
81

playground

Repository for experimenting. May be synced and/or reset at any time without notice.
Java
6
star
82

lilliput-jdk17u

https://openjdk.org/projects/lilliput
Java
6
star
83

cr

https://openjdk.org/projects/skara
HTML
6
star
84

jdk22

https://openjdk.org/projects/jdk/22
Java
6
star
85

riscv-port-jdk17u

https://openjdk.org/projects/riscv-port
Java
6
star
86

sigtest

https://wiki.openjdk.org/display/CodeTools/sigtest
Java
5
star
87

shenandoah-jdk8u

https://openjdk.org/projects/shenandoah
Java
5
star
88

friday-stats

https://openjdk.org/projects/code-tools/friday-stats
Java
4
star
89

jdk9

https://openjdk.org/projects/jdk9 released 2017-09-21
Java
4
star
90

aarch32-port-jdk8u

https://openjdk.org/projects/aarch32-port
Java
4
star
91

jdk11u-ri

JSR 384 Reference Implementation (Java SE 11) [not for production use]
Java
4
star
92

jdk9u

https://openjdk.org/projects/jdk-updates last released 2018-01-16
Java
3
star
93

jfx12

JavaFX 12.x : last released 2019-07-19
C++
3
star
94

jfx13

JavaFX 13.x : last released 2020-01-15
C++
3
star
95

jmc-graphics

https://openjdk.org/projects/jmc
3
star
96

jdk10u

https://openjdk.org/projects/jdk-updates last released 2018-07-17
3
star
97

jemmy-v3

UI test automation library - v3
Java
2
star
98

webrev

https://openjdk.org/projects/code-tools/webrev
Shell
2
star
99

riscv-port-jdk11u

https://openjdk.org/projects/riscv-port
Java
2
star
100

lilliput-jdk21u

https://openjdk.org/projects/lilliput
Java
2
star