|
| 1 | +plugins { |
| 2 | + java |
| 3 | + alias(libs.plugins.osdetector) |
| 4 | + id("me.champeau.jmh") version "0.7.2" |
| 5 | +} |
| 6 | + |
| 7 | +// JMH can use modern bytecode; benchmarks aren't shipped. |
| 8 | +java { |
| 9 | + sourceCompatibility = JavaVersion.VERSION_21 |
| 10 | + targetCompatibility = JavaVersion.VERSION_21 |
| 11 | +} |
| 12 | + |
| 13 | +val buf: Configuration by configurations.creating |
| 14 | + |
| 15 | +tasks.register("configureBuf") { |
| 16 | + description = "Installs the Buf CLI." |
| 17 | + File(buf.asPath).setExecutable(true) |
| 18 | +} |
| 19 | + |
| 20 | +tasks.register<Copy>("filterBufGenYaml") { |
| 21 | + from(files("buf.gen.yaml")) |
| 22 | + includeEmptyDirs = false |
| 23 | + into(layout.buildDirectory.dir("buf-gen-templates")) |
| 24 | + expand("protocJavaPluginVersion" to "v${libs.versions.protobuf.get().substringAfter('.')}") |
| 25 | + filteringCharset = "UTF-8" |
| 26 | +} |
| 27 | + |
| 28 | +tasks.register<Exec>("generateBenchmarkSources") { |
| 29 | + dependsOn("configureBuf", "filterBufGenYaml") |
| 30 | + description = "Generates Java sources for benchmark protos via buf generate." |
| 31 | + val template = layout.buildDirectory.file("buf-gen-templates/buf.gen.yaml") |
| 32 | + inputs.files(buf) |
| 33 | + inputs.dir("src/jmh/proto") |
| 34 | + inputs.file("buf.yaml") |
| 35 | + inputs.file(template) |
| 36 | + outputs.dir(layout.buildDirectory.dir("generated/sources/bufgen")) |
| 37 | + commandLine(buf.asPath, "generate", "--template", template.get().asFile.absolutePath) |
| 38 | +} |
| 39 | + |
| 40 | +sourceSets { |
| 41 | + named("jmh") { |
| 42 | + java { |
| 43 | + srcDir(layout.buildDirectory.dir("generated/sources/bufgen")) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +tasks.matching { it.name == "compileJmhJava" }.configureEach { |
| 49 | + dependsOn("generateBenchmarkSources") |
| 50 | +} |
| 51 | + |
| 52 | +// Ensure `./gradlew build` (and `make build`) compiles the JMH sources so CI |
| 53 | +// catches breakages in benchmark code. Execution remains gated behind the |
| 54 | +// explicit `:benchmarks:jmh` task. |
| 55 | +tasks.named("build") { |
| 56 | + dependsOn("compileJmhJava") |
| 57 | +} |
| 58 | + |
| 59 | +dependencies { |
| 60 | + jmhImplementation(project(":")) |
| 61 | + jmhImplementation(libs.protobuf.java) |
| 62 | + buf("build.buf:buf:${libs.versions.buf.get()}:${osdetector.classifier}@exe") |
| 63 | +} |
| 64 | + |
| 65 | +// Benchmarks produce fresh timing data each run; disable Gradle's up-to-date |
| 66 | +// check so the task always executes (otherwise -Pbench changes are ignored). |
| 67 | +tasks.named("jmh") { |
| 68 | + outputs.upToDateWhen { false } |
| 69 | +} |
| 70 | + |
| 71 | +jmh { |
| 72 | + // Defaults tuned for fast local A/B runs (~90s total). |
| 73 | + // For higher-confidence numbers bump iteration time and fork count. |
| 74 | + warmupIterations.set(3) |
| 75 | + warmup.set("2s") |
| 76 | + iterations.set(5) |
| 77 | + timeOnIteration.set("2s") |
| 78 | + fork.set(2) |
| 79 | + timeUnit.set("ns") |
| 80 | + benchmarkMode.set(listOf("avgt")) |
| 81 | + resultFormat.set("JSON") |
| 82 | + // GC profiler reports bytes allocated per op (gc.alloc.rate.norm), which |
| 83 | + // jmhCompare can diff alongside timing. ~5-10% overhead on timings. |
| 84 | + profilers.set(listOf("gc")) |
| 85 | + // For allocation flame graphs (requires async-profiler installed locally): |
| 86 | + // profilers.set(listOf("async:event=alloc;output=flamegraph;dir=build/reports/jmh/async")) |
| 87 | + |
| 88 | + // Filter to a subset of benchmarks via `-Pbench=<regex>`. Example: |
| 89 | + // ./gradlew :benchmarks:jmh -Pbench=validateSimple |
| 90 | + // ./gradlew :benchmarks:jmh -Pbench='compile.*' |
| 91 | + project.findProperty("bench")?.toString()?.let { |
| 92 | + includes.set(listOf(it)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +val jmhResults = layout.buildDirectory.file("results/jmh/results.json") |
| 97 | +val jmhBaseline = layout.buildDirectory.file("results/jmh/results-before.json") |
| 98 | + |
| 99 | +// Saves the latest JMH results.json as the baseline for jmhCompare. |
| 100 | +// |
| 101 | +// Usage: |
| 102 | +// ./gradlew :benchmarks:jmh :benchmarks:jmhSaveBaseline |
| 103 | +// # apply change... |
| 104 | +// ./gradlew :benchmarks:jmh :benchmarks:jmhCompare |
| 105 | +tasks.register<Copy>("jmhSaveBaseline") { |
| 106 | + description = "Copies the latest JMH results.json to results-before.json as the baseline." |
| 107 | + from(jmhResults) |
| 108 | + into(jmhResults.get().asFile.parentFile) |
| 109 | + rename { "results-before.json" } |
| 110 | + mustRunAfter("jmh") |
| 111 | +} |
| 112 | + |
| 113 | +// Diffs two JMH results.json files as a concise benchstat-style table. |
| 114 | +// Defaults to comparing results-before.json (written by jmhSaveBaseline) |
| 115 | +// against the latest results.json. |
| 116 | +// |
| 117 | +// Override paths: |
| 118 | +// ./gradlew :benchmarks:jmhCompare -Pbefore=a.json -Pafter=b.json |
| 119 | +tasks.register<Exec>("jmhCompare") { |
| 120 | + description = "Diffs two JMH result JSON files as a concise table." |
| 121 | + val before = project.findProperty("before")?.toString() |
| 122 | + ?: jmhBaseline.get().asFile.absolutePath |
| 123 | + val after = project.findProperty("after")?.toString() |
| 124 | + ?: jmhResults.get().asFile.absolutePath |
| 125 | + val jqScript = file("jmh-compare.jq").absolutePath |
| 126 | + commandLine( |
| 127 | + "bash", "-c", |
| 128 | + "jq --slurp --raw-output --from-file \"\$1\" \"\$2\" \"\$3\" | column -t -s \$'\\t'", |
| 129 | + "jmh-compare", // $0 |
| 130 | + jqScript, // $1 |
| 131 | + before, // $2 |
| 132 | + after, // $3 |
| 133 | + ) |
| 134 | +} |
0 commit comments