|
| 1 | +# JMH Configuration for doma-core |
| 2 | + |
| 3 | +This document explains how JMH (Java Microbenchmark Harness) is configured in the doma-core module using the JMH Gradle plugin. |
| 4 | + |
| 5 | +## Configuration Details |
| 6 | + |
| 7 | +### Plugin Configuration |
| 8 | + |
| 9 | +The JMH plugin is configured in `gradle/libs.versions.toml`: |
| 10 | +```toml |
| 11 | +[plugins] |
| 12 | +jmh = { id = "me.champeau.jmh", version = "0.7.3" } |
| 13 | +``` |
| 14 | + |
| 15 | +And applied in `doma-core/build.gradle.kts`: |
| 16 | +```kotlin |
| 17 | +plugins { |
| 18 | + alias(libs.plugins.jmh) |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +### JMH Settings |
| 23 | + |
| 24 | +The benchmark configuration supports multiple profiles for different use cases: |
| 25 | + |
| 26 | +#### Development Profile (Default - Fast) |
| 27 | +```bash |
| 28 | +./gradlew :doma-core:jmh # Uses dev profile by default |
| 29 | +./gradlew :doma-core:jmh -Pjmh.profile=dev # Explicit dev profile |
| 30 | +``` |
| 31 | +- **Time**: ~30 seconds for all benchmarks |
| 32 | +- **Iterations**: 1 measurement, 1 warmup |
| 33 | +- **Forks**: 1 |
| 34 | +- **Duration**: 1 second per iteration |
| 35 | +- **Use case**: Quick feedback during development |
| 36 | + |
| 37 | +#### CI Profile (Balanced) |
| 38 | +```bash |
| 39 | +./gradlew :doma-core:jmh -Pjmh.profile=ci |
| 40 | +``` |
| 41 | +- **Time**: ~3-5 minutes for all benchmarks |
| 42 | +- **Iterations**: 3 measurement, 2 warmup |
| 43 | +- **Forks**: 1 |
| 44 | +- **Duration**: 3 seconds per iteration |
| 45 | +- **Use case**: Continuous integration, pull request validation |
| 46 | + |
| 47 | +#### Production Profile (Accurate) |
| 48 | +```bash |
| 49 | +./gradlew :doma-core:jmh -Pjmh.profile=production |
| 50 | +``` |
| 51 | +- **Time**: ~20-30 minutes for all benchmarks |
| 52 | +- **Iterations**: 10 measurement, 5 warmup |
| 53 | +- **Forks**: 3 |
| 54 | +- **Duration**: 10 seconds per iteration |
| 55 | +- **Use case**: Release benchmarks, performance regression analysis |
| 56 | + |
| 57 | +The JMH task is configured to bypass Gradle's caching mechanism to ensure benchmarks always run and provide fresh performance measurements. |
| 58 | + |
| 59 | +### Dependency Management |
| 60 | + |
| 61 | +The JMH plugin automatically handles all JMH dependencies including: |
| 62 | +- `org.openjdk.jmh:jmh-core` |
| 63 | +- `org.openjdk.jmh:jmh-generator-annprocess` |
| 64 | +- Code generation and annotation processing |
| 65 | + |
| 66 | +No manual dependency configuration is required. |
| 67 | + |
| 68 | +### Benchmark Source Location |
| 69 | + |
| 70 | +Benchmark classes are located in: |
| 71 | +- `src/jmh/java/org/seasar/doma/internal/util/SqlTokenUtilBenchmark.java` |
| 72 | +- `src/jmh/java/org/seasar/doma/internal/jdbc/sql/SqlTokenizerBenchmark.java` |
| 73 | + |
| 74 | +## Available Gradle Tasks |
| 75 | + |
| 76 | +### `./gradlew :doma-core:jmh` |
| 77 | +Runs all JMH benchmarks with the configured settings: |
| 78 | +- 5 measurement iterations (10 seconds each by default) |
| 79 | +- 3 warmup iterations (10 seconds each by default) |
| 80 | +- 2 forks |
| 81 | +- Results in nanoseconds |
| 82 | +- JVM args: `-Xms2G -Xmx2G` |
| 83 | + |
| 84 | +### `./gradlew :doma-core:jmhJar` |
| 85 | +Creates a self-contained JAR file with all benchmarks. |
| 86 | + |
| 87 | +### `./gradlew :doma-core:jmhCompileGeneratedClasses` |
| 88 | +Compiles JMH generated classes. |
| 89 | + |
| 90 | +### `./gradlew :doma-core:jmhRunBytecodeGenerator` |
| 91 | +Runs the JMH bytecode generator. |
| 92 | + |
| 93 | +## Running Specific Benchmarks |
| 94 | + |
| 95 | +### Method 1: Configure in build.gradle.kts |
| 96 | +You can configure the JMH plugin to run specific benchmarks by modifying the build file: |
| 97 | +```kotlin |
| 98 | +jmh { |
| 99 | + includes.set(listOf(".*SqlTokenUtil.*")) // Run only SqlTokenUtil benchmarks |
| 100 | + excludes.set(listOf(".*classic.*")) // Exclude classic benchmarks |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### Method 2: Dynamic filtering with Gradle properties |
| 105 | +The current build script already supports dynamic filtering: |
| 106 | +```kotlin |
| 107 | +val jmhIncludes: String? = findProperty("jmh.includes") as String? |
| 108 | +jmh { |
| 109 | + iterations.set(5) |
| 110 | + warmupIterations.set(3) |
| 111 | + fork.set(2) |
| 112 | + timeUnit.set("ns") |
| 113 | + resultFormat.set("TEXT") |
| 114 | + jvmArgs.set(listOf("-Xms2G", "-Xmx2G")) |
| 115 | + |
| 116 | + if (jmhIncludes != null) { |
| 117 | + includes.set(listOf(jmhIncludes)) |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +Then run with: |
| 123 | +```bash |
| 124 | +./gradlew :doma-core:jmh -Pjmh.includes=".*SqlTokenUtil.*" |
| 125 | +``` |
| 126 | + |
| 127 | +## Writing Benchmarks |
| 128 | + |
| 129 | +1. Create benchmark classes in `src/jmh/java` |
| 130 | +2. Annotate with JMH annotations like `@Benchmark`, `@State`, `@Setup` |
| 131 | +3. Configuration is handled by the Gradle plugin, not annotations |
| 132 | +4. Follow JMH best practices for microbenchmarking |
| 133 | + |
| 134 | +Example structure: |
| 135 | +```java |
| 136 | +@BenchmarkMode(Mode.AverageTime) |
| 137 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 138 | +@State(Scope.Benchmark) |
| 139 | +public class MyBenchmark { |
| 140 | + @Benchmark |
| 141 | + public void testMethod() { |
| 142 | + // benchmark code |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## Troubleshooting |
| 148 | + |
| 149 | +If benchmarks don't run: |
| 150 | +1. Ensure benchmark classes are in `src/jmh/java` |
| 151 | +2. Check that the JMH plugin is properly applied |
| 152 | +3. Clean and rebuild: `./gradlew :doma-core:clean :doma-core:jmh` |
0 commit comments