The JVM 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.
@Benchmark classes, so
the setup is close to the Java one. Kotlin only changes how benchmark classes
have to be declared, and which generator a Maven build has to run.
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. It generates the harness from
the compiled Kotlin classes in src/jmh/kotlin, so no annotation processing
setup is needed. Add it to your build.gradle.kts:build.gradle.kts
Creating benchmarks
Write your benchmarks using standard JMH annotations:FibBenchmark.kt
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:
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
.github/workflows/codspeed.yml
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. The
annotated property has to be a var, since JMH assigns it before the benchmark
runs and rejects final fields:
ParamBenchmark.kt
Shared state
Use@State to share setup logic across benchmarks and control the scope of the
state object:
StateBenchmark.kt
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 21 or later is required.
- Benchmark classes must not be final, since JMH subclasses them to generate the measurement harness.
- 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.