-
Notifications
You must be signed in to change notification settings - Fork 0
180 lines (156 loc) · 5.97 KB
/
benchmark.yml
File metadata and controls
180 lines (156 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
name: Benchmark
on:
pull_request:
branches: [main, develop]
push:
branches: [main, develop]
permissions:
contents: read
pull-requests: write
packages: read
env:
GOPRIVATE: github.com/GoCodeAlone/*
GONOSUMCHECK: github.com/GoCodeAlone/*
jobs:
benchmark:
name: Performance Benchmarks
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
packages: read
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.26'
cache: true
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://npm.pkg.github.com'
cache: 'npm'
cache-dependency-path: ui/package-lock.json
- name: Build UI assets
run: cd ui && npm ci && npm run build
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install benchstat
run: go install golang.org/x/perf/cmd/benchstat@latest
- name: Run benchmarks
run: go test -bench=. -benchmem -count=6 -run=^$ -timeout=30m ./... | tee benchmark-results.txt
- name: Restore baseline
if: github.event_name == 'pull_request'
id: restore-baseline
uses: actions/cache/restore@v4
with:
path: baseline-bench.txt
key: go-benchmark-${{ github.event.pull_request.base.ref }}-never
restore-keys: |
go-benchmark-${{ github.event.pull_request.base.ref }}-
- name: Compare with baseline
if: github.event_name == 'pull_request' && steps.restore-baseline.outputs.cache-matched-key != ''
id: compare
run: |
echo "## benchstat: baseline → PR" > benchstat-output.txt
benchstat baseline-bench.txt benchmark-results.txt >> benchstat-output.txt 2>&1 || true
# Detect regressions: benchstat v2 output lines look like:
# BenchmarkName 1.00ns ± 1% 1.25ns ± 2% +25.00% (p=0.002 n=6)
# We look for lines containing a benchmark name (starts with Benchmark)
# followed by a percentage increase >= 20%.
regression=false
while IFS= read -r line; do
if echo "$line" | grep -qP '^\s*(│\s+)?Benchmark.*\+\d+\.\d+%'; then
pct=$(echo "$line" | grep -oP '\+\K\d+\.\d+(?=%)' | tail -1)
if [ -n "$pct" ] && echo "$pct" | awk '{ exit ($1 < 20.0) }'; then
regression=true
break
fi
fi
done < benchstat-output.txt
echo "regression=$regression" >> "$GITHUB_OUTPUT"
if [ "$regression" = "true" ]; then
echo "::warning::Potential performance regression detected (>= 20% slower)"
fi
- name: Comment PR with results
if: github.event_name == 'pull_request' && steps.restore-baseline.outputs.cache-matched-key != ''
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const benchstat = fs.readFileSync('benchstat-output.txt', 'utf8');
const regression = '${{ steps.compare.outputs.regression }}' === 'true';
const body = [
'## ⏱ Benchmark Results',
'',
regression
? '⚠️ **Potential performance regression detected** — one or more benchmarks regressed ≥ 20%.'
: '✅ **No significant performance regressions detected.**',
'',
'<details>',
'<summary>benchstat comparison (baseline → PR)</summary>',
'',
'```',
benchstat,
'```',
'',
'</details>',
'',
'> Benchmarks run with `go test -bench=. -benchmem -count=6`.',
'> Regressions ≥ 20% are flagged. Results compared via [`benchstat`](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat).',
].join('\n');
// Upsert: update existing comment or create a new one.
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const marker = '## ⏱ Benchmark Results';
const existing = comments.find(c => c.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: No baseline available
if: github.event_name == 'pull_request' && steps.restore-baseline.outputs.cache-matched-key == ''
run: |
echo "::notice::No benchmark baseline found for branch '${{ github.event.pull_request.base.ref }}'. Baseline will be created on next push to that branch."
# On push to main/develop, save the results as the new baseline.
- name: Prepare baseline for cache
if: github.event_name == 'push'
run: cp benchmark-results.txt baseline-bench.txt
- name: Save baseline
if: github.event_name == 'push'
uses: actions/cache/save@v4
with:
path: baseline-bench.txt
key: go-benchmark-${{ github.ref_name }}-${{ github.sha }}
- name: Upload benchmark results
if: always()
uses: actions/upload-artifact@v4
with:
name: benchmark-results
path: |
benchmark-results.txt
benchstat-output.txt
retention-days: 30
if-no-files-found: ignore
- name: Fail on regression
if: github.event_name == 'pull_request' && steps.compare.outputs.regression == 'true'
run: |
echo "::error::Performance regression detected. Review the benchmark results in the PR comment."
exit 1