name: Benchmarks
on:
workflow_dispatch:
inputs:
pr_number:
description: 'Pull Request number'
required: true
type: number
permissions: {}
env:
BENCH_COUNT: 6
BENCH_FILTER: 'BenchmarkCompare/(mem|os)fs'
BENCH_PATH: ./test/...
jobs:
setup:
name: Get PR Info
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
base_ref: ${{ steps.pr-info.outputs.base_ref }}
steps:
- name: Get PR base ref
id: pr-info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
base_ref=$(gh pr view ${{ inputs.pr_number }} --repo ${{ github.repository }} --json baseRefName --jq .baseRefName)
echo "base_ref=$base_ref" >> $GITHUB_OUTPUT
bench-base:
name: Benchmark base branch
runs-on: ubuntu-latest
needs: setup
permissions: {}
outputs:
outcome: ${{ steps.bench.outcome }}
steps:
- name: Checkout base branch
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ needs.setup.outputs.base_ref }}
persist-credentials: false
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: stable
cache-dependency-path: go.sum
- name: Run benchmarks
id: bench
run: go test -run='^$' -bench="$BENCH_FILTER" -benchmem -count=$BENCH_COUNT $BENCH_PATH > base.txt 2>&1
continue-on-error: true
- name: Upload results
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
name: base-bench
path: base.txt
bench-pr:
name: Benchmark PR branch
runs-on: ubuntu-latest
needs: setup
permissions: {}
outputs:
outcome: ${{ steps.bench.outcome }}
steps:
- name: Checkout PR branch
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: refs/pull/${{ inputs.pr_number }}/head
persist-credentials: false
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: stable
cache-dependency-path: go.sum
- name: Run benchmarks
id: bench
run: go test -run='^$' -bench="$BENCH_FILTER" -benchmem -count=$BENCH_COUNT $BENCH_PATH > pr.txt 2>&1
continue-on-error: true
- name: Upload results
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
name: pr-bench
path: pr.txt
compare:
name: Compare and report
runs-on: ubuntu-latest
needs: [bench-base, bench-pr]
if: always()
permissions:
pull-requests: write
steps:
- name: Set up Go
uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417 # v6.3.0
with:
go-version: stable
- name: Install benchstat
# renovate: datasource=go depName=golang.org/x/perf/cmd/benchstat
run: go install golang.org/x/perf/cmd/benchstat@v0.0.0-20260211190930-8161c38c6cdc
- name: Download base results
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
with:
name: base-bench
- name: Download PR results
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
with:
name: pr-bench
- name: Compare and report
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pr_number }}
REPO: ${{ github.repository }}
BASE_OUTCOME: ${{ needs.bench-base.outputs.outcome }}
PR_OUTCOME: ${{ needs.bench-pr.outputs.outcome }}
run: |
if [ "$BASE_OUTCOME" != "success" ] || [ "$PR_OUTCOME" != "success" ]; then
{
echo "## ⚠️ Benchmark Run Failed"
echo ""
echo "One or more benchmark runs did not complete successfully."
echo "Please check the workflow logs for details."
echo ""
echo "| Step | Outcome |"
echo "| --- | --- |"
echo "| Base branch benchmarks | \`${BASE_OUTCOME}\` |"
echo "| PR branch benchmarks | \`${PR_OUTCOME}\` |"
} > comment.md
else
benchstat base.txt pr.txt > stat.txt 2>&1
# Extract lines with regressions greater than 5% on any metric.
# Also capture infinite regressions (+∞ / +Inf) that appear when the
# baseline measurement was zero.
awk '
/\+∞/ || /\+Inf/ { print; next }
match($0, /\+[0-9]+\.?[0-9]*%/) {
pct = substr($0, RSTART + 1, RLENGTH - 2)
if (pct + 0 > 5) print
}
' stat.txt > regressions.txt
if [ -s regressions.txt ]; then
{
echo "## ⚠️ Benchmark Regressions Detected (>5%)"
echo ""
echo "The following benchmarks have degraded by more than 5% in time, memory, or allocations:"
echo ""
echo "\`\`\`"
cat regressions.txt
echo "\`\`\`"
echo ""
echo ""
echo "Full benchmark comparison (benchstat)
"
echo ""
echo "\`\`\`"
cat stat.txt
echo "\`\`\`"
echo " "
} > comment.md
else
{
echo "## ✅ No Benchmark Regressions"
echo ""
echo "No benchmark regressions greater than 5% were detected."
echo ""
echo ""
echo "Full benchmark comparison (benchstat)
"
echo ""
echo "\`\`\`"
cat stat.txt
echo "\`\`\`"
echo " "
} > comment.md
fi
fi
gh pr comment "$PR_NUMBER" --edit-last --create-if-none --body-file comment.md --repo "$REPO"