Skip to main content
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.
Integrating CodSpeed into your Java project works through a fork of JMH (Java Microbenchmark Harness). You write standard JMH benchmarks and swap in the CodSpeed JMH fork as a dependency. When running in CI with CodSpeed, the results are automatically collected and reported.

Installation

CodSpeed provides a fork of JMH that collects walltime results and sends them to CodSpeed.
Add the CodSpeed JMH fork as a Git submodule:
Then include it as a composite build in your settings.gradle.kts, with dependency substitution to redirect JMH dependencies to the CodSpeed fork:
settings.gradle.kts
We recommend using the JMH Gradle Plugin as it handles benchmark compilation and provides the 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.
For an in-depth tutorial on JMH, see the How to Benchmark Java with JMH guide.

Testing the benchmarks locally

To run the benchmarks with CodSpeed locally, first install the codspeed runner:
Then run your benchmarks with CodSpeed:
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.
If you want more details on how to configure the CodSpeed action, you can check out the Continuous Reporting section.
Here is an example of a GitHub Actions workflow that runs the benchmarks and reports the results to CodSpeed on every push to the main branch and every pull request:

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
For a deeper dive into JMH, see the dedicated guide:

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 @BenchmarkMode annotations in your code are ignored.
If you run into issues or require certain features, please open an issue or join our Discord to get help.

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.