The Java integration is still in early development, and only the walltime
instrument is currently supported.If you have any feedback, please reach out to us via
Discord or
email our support.
Installation
CodSpeed provides a fork of JMH that collects walltime results and sends them to CodSpeed.- Gradle
- Maven
Add the CodSpeed JMH fork as a Git submodule:Then include it as a composite build in your We recommend using the
JMH Gradle Plugin as it handles
benchmark compilation and provides the
settings.gradle.kts, with
dependency substitution to redirect JMH dependencies to the CodSpeed fork:settings.gradle.kts
jmh task. Add it to your
build.gradle.kts:build.gradle.kts
Creating benchmarks
Write your benchmarks using standard JMH annotations:FibBenchmark.java
JMH benchmarks must return their result or use
Blackhole.consume() to
prevent the JVM from eliminating dead code. All examples on this page return
the computed value.Testing the benchmarks locally
To run the benchmarks with CodSpeed locally, first install thecodspeed
runner:
- Gradle
- Maven
terminal
Running the benchmarks in your CI
To generate performance reports, you need to run the benchmarks in your CI. This allows CodSpeed to automatically run benchmarks and warn you about regressions during development. Here is an example of a GitHub Actions workflow that runs the benchmarks and reports the results to CodSpeed on every push to themain branch and every
pull request:
- Gradle
- Maven
Advanced usage
JMH provides many features for writing expressive benchmarks. Below is a selection that can be useful in CodSpeed benchmarks.Parameterized benchmarks
Use@Param to run the same benchmark with different input values:
ParamBenchmark.java
Shared state
Use@State to share setup logic across benchmarks and control the scope of the
state object:
StateBenchmark.java
How to Benchmark Java with JMH
An in-depth guide to writing JMH benchmarks: project setup, annotations,
parameters, common pitfalls, and CodSpeed CI integration.
Compatibility
- JDK 17 or later is required.
- All standard JMH annotations are supported.
- Only the Walltime instrument is supported
- CodSpeed uses a custom mode for all benchmarks to collect statistically
significant results. Any
@BenchmarkModeannotations in your code are ignored.
Next steps
Example repository
The CodSpeed JVM repository with example JMH benchmarks.
How to Benchmark Java with JMH
An in-depth guide to writing JMH benchmarks with CodSpeed.
Walltime instrument
Learn more about the Walltime instrument and how to use it.
Dive into performance changes
Learn more about profiling and how to read flame graphs.