Implementation:Apache Kafka JMH Benchmark Script
| Knowledge Sources | |
|---|---|
| Domains | Benchmarking, Performance, CLI |
| Last Updated | 2026-02-09 12:00 GMT |
Overview
External Tool Doc for building and running JMH (Java Microbenchmark Harness) benchmarks via the jmh.sh convenience script.
Description
The jmh-benchmarks/jmh.sh script simplifies the benchmark workflow by combining the Gradle build step and JMH execution into a single command. It resolves the Gradle wrapper path (supporting invocation from both the repository root and the jmh-benchmarks directory), runs ./gradlew :jmh-benchmarks:clean :jmh-benchmarks:shadowJar to produce a fat JAR, and then executes java -jar on the resulting kafka-jmh-benchmarks-*.jar, forwarding all script arguments directly to JMH.
Usage
Use this script to run performance microbenchmarks against Kafka internals. Developers use it to measure throughput and latency of specific code paths such as record serialization, compression, partition assignment, and protocol parsing.
Code Reference
Source Location
- Repository: Apache_Kafka
- File: jmh-benchmarks/jmh.sh
- Lines: 1-42
Signature
#!/usr/bin/env bash
# Usage: jmh.sh [JMH arguments...]
#
# Steps:
# 1. Resolve Gradle wrapper path (supports repo root or jmh-benchmarks/ cwd)
# 2. Run: ./gradlew :jmh-benchmarks:clean :jmh-benchmarks:shadowJar
# 3. Run: java -jar jmh-benchmarks/build/libs/kafka-jmh-benchmarks-*.jar "$@"
Import
# No import required; invoke from the repository root or jmh-benchmarks/ directory:
jmh-benchmarks/jmh.sh [benchmark_name] [JMH flags...]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| benchmark_name | String | No | Regex pattern to filter which benchmarks to run (e.g., RecordBatchBenchmark) |
| JMH flags | Varies | No | Standard JMH options such as -f (forks), -wi (warmup iterations), -i (iterations), -prof (profiler) |
Outputs
| Name | Type | Description |
|---|---|---|
| Benchmark results | stdout | JMH benchmark results including throughput, average time, and percentile metrics |
| Shadow JAR | File | Built artifact at jmh-benchmarks/build/libs/kafka-jmh-benchmarks-*.jar |
Usage Examples
Run All Benchmarks
# Build and run all JMH benchmarks with default settings
jmh-benchmarks/jmh.sh
Run Specific Benchmark
# Run only the RecordBatch benchmark with 2 forks and 5 iterations
jmh-benchmarks/jmh.sh RecordBatchBenchmark -f 2 -i 5
Run with Profiler
# Run with the async-profiler for flame graph generation
jmh-benchmarks/jmh.sh PartitionerBenchmark -prof async:libPath=/path/to/libasyncProfiler.so
List Available Benchmarks
# List all available benchmark names without running them
jmh-benchmarks/jmh.sh -l