-
Notifications
You must be signed in to change notification settings - Fork 4
262 lines (221 loc) · 8.29 KB
/
Copy pathbenchmark.yml
File metadata and controls
262 lines (221 loc) · 8.29 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
name: Benchmark PR
on:
pull_request:
branches: [master]
permissions:
contents: read
pull-requests: write
jobs:
benchmark-base:
name: Benchmark base branch
runs-on: ubuntu-latest
steps:
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install .
- name: Run benchmarks
run: |
pytest test/ -k bench --benchmark-only --benchmark-json=results.json --benchmark-disable-gc
- name: Upload benchmark results
uses: actions/upload-artifact@v4
with:
name: benchmark-base
path: results.json
retention-days: 1
benchmark-pr:
name: Benchmark PR branch
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pip install .
- name: Run benchmarks
run: |
pytest test/ -k bench --benchmark-only --benchmark-json=results.json --benchmark-disable-gc
- name: Upload benchmark results
uses: actions/upload-artifact@v4
with:
name: benchmark-pr
path: results.json
retention-days: 1
compare:
name: Compare benchmarks
needs: [benchmark-base, benchmark-pr]
runs-on: ubuntu-latest
steps:
- name: Download base benchmark results
uses: actions/download-artifact@v4
with:
name: benchmark-base
path: base
- name: Download PR benchmark results
uses: actions/download-artifact@v4
with:
name: benchmark-pr
path: pr
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Compare benchmarks and generate report
id: compare
run: |
python3 << 'EOF'
import json
import os
import sys
# Load benchmark results
with open('base/results.json', 'r') as f:
base_results = json.load(f)
with open('pr/results.json', 'r') as f:
pr_results = json.load(f)
# Extract benchmark data into dictionaries
def extract_benchmarks(data):
benchmarks = {}
for bench in data.get('benchmarks', []):
name = bench['name']
# Use median as the primary metric (more stable than mean)
stats = bench['stats']
benchmarks[name] = {
'mean': stats['mean'] * 1000, # Convert to ms
'median': stats['median'] * 1000,
'stddev': stats['stddev'] * 1000,
'rounds': stats['rounds'],
}
return benchmarks
base_benchmarks = extract_benchmarks(base_results)
pr_benchmarks = extract_benchmarks(pr_results)
# Compare benchmarks
results = []
has_severe_regression = False
all_names = set(base_benchmarks.keys()) | set(pr_benchmarks.keys())
for name in sorted(all_names):
base = base_benchmarks.get(name)
pr = pr_benchmarks.get(name)
if base is None:
results.append({
'name': name,
'base_time': 'N/A',
'pr_time': f"{pr['median']:.4f}ms",
'delta': 'NEW',
'status': '🆕',
'pct_change': 0,
})
continue
if pr is None:
results.append({
'name': name,
'base_time': f"{base['median']:.4f}ms",
'pr_time': 'N/A',
'delta': 'REMOVED',
'status': '🗑️',
'pct_change': 0,
})
continue
# Calculate percentage change (negative = faster, positive = slower)
pct_change = ((pr['median'] - base['median']) / base['median']) * 100
# Determine status
if pct_change < -5:
status = '✅' # Faster by more than 5%
elif pct_change <= 5:
status = '➖' # Within noise (±5%)
elif pct_change <= 20:
status = '⚠️' # Slower by 5-20%
else:
status = '❌' # Severe regression (>20%)
has_severe_regression = True
# Format delta string
if pct_change < 0:
delta = f"{abs(pct_change):.1f}% faster"
elif pct_change > 0:
delta = f"{pct_change:.1f}% slower"
else:
delta = "no change"
results.append({
'name': name,
'base_time': f"{base['median']:.4f}ms",
'pr_time': f"{pr['median']:.4f}ms",
'delta': delta,
'status': status,
'pct_change': pct_change,
})
# Generate markdown report
report_lines = [
"## 📊 Benchmark Comparison Results",
"",
"| Status | Benchmark | Base | PR | Delta |",
"|:------:|:----------|-----:|---:|:------|",
]
for r in results:
# Truncate long benchmark names
name = r['name']
if len(name) > 60:
name = name[:57] + '...'
report_lines.append(
f"| {r['status']} | `{name}` | {r['base_time']} | {r['pr_time']} | {r['delta']} |"
)
report_lines.extend([
"",
"### Legend",
"- ✅ **Faster** (>5% improvement)",
"- ➖ **Within noise** (±5%)",
"- ⚠️ **Slower** (5-20% regression)",
"- ❌ **Severe regression** (>20% slower)",
"- 🆕 **New benchmark**",
"- 🗑️ **Removed benchmark**",
"",
f"*Comparing {len(base_benchmarks)} base benchmarks with {len(pr_benchmarks)} PR benchmarks*",
])
if has_severe_regression:
report_lines.extend([
"",
"---",
"⛔ **This PR has severe performance regressions (>20% slower).** Please investigate before merging.",
])
report = '\n'.join(report_lines)
# Write report to file for the comment action
with open('benchmark-report.md', 'w') as f:
f.write(report)
# Set outputs using GitHub Actions environment files
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f"has_regression={'true' if has_severe_regression else 'false'}\n")
# Print report to console
print(report)
EOF
- name: Find existing comment
uses: peter-evans/find-comment@v3
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: '📊 Benchmark Comparison Results'
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-path: benchmark-report.md
edit-mode: replace
- name: Fail on severe regression
if: steps.compare.outputs.has_regression == 'true'
run: |
echo "::error::Severe performance regression detected (>20% slower)"
exit 1