id: go-benchmark-regression-gate
namespace: company.engineering
inputs:
- id: repository
type: STRING
displayName: Repository
description: GitHub repository as owner/name. A pull request webhook body wins
over this value.
defaults: google/uuid
- id: base_ref
type: STRING
displayName: Base ref
description: Commit SHA, tag or branch the head is compared against.
defaults: v1.5.0
- id: head_ref
type: STRING
displayName: Head ref
description: Commit SHA, tag or branch under test.
defaults: v1.6.0
- id: pull_request
type: INT
displayName: Pull request number
description: When set, the report is posted on this pull request. Needs the
GITHUB_TOKEN secret.
required: false
- id: packages
type: STRING
displayName: Packages
description: Go package pattern to benchmark.
defaults: ./...
- id: bench
type: STRING
displayName: Benchmark filter
description: Regular expression passed to go test -bench.
defaults: .
- id: count
type: INT
displayName: Runs per benchmark
description: benchstat needs several samples per side to tell a real change from
noise. 10 is the usual minimum.
defaults: 10
- id: benchtime
type: STRING
displayName: Time per run
description: Passed to go test -benchtime.
defaults: 100ms
- id: head_goflags
type: STRING
displayName: Extra GOFLAGS for the head build
description: Empty by default. Set it to -gcflags=all=-N to build the head
without optimizations and watch the gate block it, or use it to evaluate a
build change such as -pgo=auto.
required: false
- id: max_geomean_regression_pct
type: INT
displayName: Max overall slowdown (%)
description: Block when the geometric mean of all benchmarks got slower by more
than this.
defaults: 5
- id: max_single_regression_pct
type: INT
displayName: Max slowdown of one benchmark (%)
description: Block when a single benchmark got significantly slower by more than
this, even if the overall change is small.
defaults: 50
variables:
repository: "{{ trigger.body.repository.full_name ?? inputs.repository }}"
base_url: "{{ trigger.body.pull_request.base.repo.clone_url ??
('https://github.com/' ~ inputs.repository ~ '.git') }}"
head_url: "{{ trigger.body.pull_request.head.repo.clone_url ??
('https://github.com/' ~ inputs.repository ~ '.git') }}"
base_ref: "{{ trigger.body.pull_request.base.sha ?? inputs.base_ref }}"
head_ref: "{{ trigger.body.pull_request.head.sha ?? inputs.head_ref }}"
pull_request: "{{ trigger.body.pull_request.number ?? inputs.pull_request ?? '' }}"
triggers:
- id: github_pull_request
type: io.kestra.plugin.core.trigger.Webhook
description: >
Point a GitHub repository webhook at this trigger with the "Pull requests"
event and content type application/json. Only opened, reopened and
synchronized pull requests start a run; other events get a 204 and no
execution. Replace the key with a long random value first.
key: replace-with-a-long-random-key
when: "{{ ['opened', 'reopened', 'synchronize'] contains (trigger.body.action ??
'') }}"
tasks:
- id: workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
tasks:
- id: clone_base
type: io.kestra.plugin.git.Clone
url: "{{ render(vars.base_url) }}"
commit: "{{ render(vars.base_ref) }}"
directory: base
- id: clone_head
type: io.kestra.plugin.git.Clone
description: Cloned from the head repository, so pull requests from forks work too.
url: "{{ render(vars.head_url) }}"
commit: "{{ render(vars.head_ref) }}"
directory: head
- id: bench
type: io.kestra.plugin.scripts.shell.Commands
description: >
Run the same benchmarks on both checkouts in one container, so both
sides share the same machine, then compare them with benchstat.
summarize.awk reads the sec/op table and prints the overall and worst
changes as Kestra outputs.
timeout: PT30M
containerImage: golang:1.26
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
env:
PACKAGES: "{{ inputs.packages }}"
BENCH: "{{ inputs.bench }}"
COUNT: "{{ inputs.count }}"
BENCHTIME: "{{ inputs.benchtime }}"
HEAD_GOFLAGS: "{{ inputs.head_goflags ?? '' }}"
inputFiles:
summarize.awk: |
/sec\/op/ && !seen { inside = 1; seen = 1; next }
inside && NF == 0 { inside = 0; next }
inside && $1 == "geomean" { geomean = $6; next }
inside {
benchmarks++
if ($6 ~ /^\+/) {
change = $6; gsub(/[+%]/, "", change)
regressions++
if (change + 0 > worst) { worst = change + 0; worst_name = $1 }
}
}
END {
gsub(/[+%]/, "", geomean)
if (geomean !~ /^-?[0-9.]+$/) geomean = 0
printf "::{\"outputs\":{\"benchmarks\":%d,\"regressions\":%d,\"geomean_pct\":%s,\"worst_pct\":%s,\"worst_benchmark\":\"%s\"}}::\n", benchmarks, regressions, geomean, worst + 0, worst_name
}
outputFiles:
- report.txt
- base.txt
- head.txt
commands:
- go install
golang.org/x/perf/cmd/benchstat@v0.0.0-20261009192801-be2c69fb417e
- (cd base && go test -run='^$' -bench="$BENCH"
-benchtime="$BENCHTIME" -count="$COUNT" $PACKAGES) > base.txt
- (cd head && GOFLAGS="$HEAD_GOFLAGS" go test -run='^$'
-bench="$BENCH" -benchtime="$BENCHTIME" -count="$COUNT" $PACKAGES) >
head.txt
- /go/bin/benchstat base.txt head.txt > report.txt
- /go/bin/benchstat -format csv base.txt head.txt | awk -F, -f
summarize.awk
- id: verdict
type: io.kestra.plugin.core.output.OutputValues
description: >
NO_BENCHMARKS when nothing matched the filter, REGRESSED when the overall
or a single benchmark got slower than allowed, PASS otherwise. Only
changes benchstat finds statistically significant count towards the
single-benchmark limit.
values:
state: >-
{%- set r = outputs.bench.vars -%} {%- if r.benchmarks == 0
-%}NO_BENCHMARKS {%- elseif r.geomean_pct >
inputs.max_geomean_regression_pct or r.worst_pct >
inputs.max_single_regression_pct -%}REGRESSED {%- else -%}PASS{%- endif
-%}
summary: >-
{{ outputs.bench.vars.benchmarks }} benchmarks, overall {{
outputs.bench.vars.geomean_pct }}% (limit {{
inputs.max_geomean_regression_pct }}%), {{
outputs.bench.vars.regressions }} significantly slower{% if
outputs.bench.vars.regressions > 0 %}, worst {{
outputs.bench.vars.worst_benchmark }} +{{ outputs.bench.vars.worst_pct
}}% (limit {{ inputs.max_single_regression_pct }}%){% endif %}.
- id: log_verdict
type: io.kestra.plugin.core.log.Log
message: >-
{{ outputs.verdict.values.state }} for {{ render(vars.repository) }} {{
render(vars.base_ref) }}..{{ render(vars.head_ref) }}: {{
outputs.verdict.values.summary }}
- id: comment_on_pr
type: io.kestra.plugin.core.flow.If
condition: "{{ render(vars.pull_request) is not empty }}"
then:
- id: pr_report
type: io.kestra.plugin.github.issues.Comment
description: One comment per pull request, updated in place on every push thanks
to updateTag.
oauthToken: "{{ secret('GITHUB_TOKEN') }}"
repository: "{{ render(vars.repository) }}"
issueNumber: "{{ render(vars.pull_request) }}"
updateTag: go-benchmark-gate
body: |
### Go benchmark gate: {{ outputs.verdict.values.state }}
`{{ render(vars.base_ref) }}` vs `{{ render(vars.head_ref) }}`: {{ outputs.verdict.values.summary }}
```
{{ read(outputs.bench.outputFiles['report.txt']) }}
```
Kestra execution `{{ execution.id }}`
- id: gate
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.verdict.values.state != 'PASS' }}"
then:
- id: block
type: io.kestra.plugin.core.execution.Fail
description: End the run as FAILED, so a required status check or the calling
pipeline can block the merge.
errorMessage: "{{ outputs.verdict.values.state }}: {{
outputs.verdict.values.summary }}"