-
Notifications
You must be signed in to change notification settings - Fork 215
193 lines (163 loc) · 6.77 KB
/
benchmark.yml
File metadata and controls
193 lines (163 loc) · 6.77 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
# Performance Benchmark CI Workflow
#
# This workflow automatically runs performance benchmarks on pull requests
# and posts a comparison comment showing speed differences between the PR
# and the base branch (master).
#
# The workflow:
# 1. Runs benchmarks on the PR branch
# 2. Runs benchmarks on the base branch (master)
# 3. Compares the results and identifies performance regressions/improvements
# 4. Posts a detailed comparison table as a PR comment
#
# Performance changes > 10% are flagged as significant to help maintainers
# catch performance regressions before merging.
name: Performance Benchmark
on:
pull_request:
branches: [master]
permissions:
contents: read
pull-requests: write
jobs:
benchmark:
runs-on: ubuntu-latest
env:
BENCHMARK_PATH: tests/benchmarks/
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: Run benchmarks on PR branch
run: |
python3 -m pytest \
--benchmark-only \
--benchmark-json=pr-benchmark.json \
--benchmark-columns=mean,stddev,iqr,ops,rounds \
${{ env.BENCHMARK_PATH }}
- name: Store PR benchmark result
uses: actions/upload-artifact@v4
with:
name: pr-benchmark
path: pr-benchmark.json
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
- name: Install dependencies for base branch
run: |
pip install -r requirements.txt
pip install -r requirements_dev.txt
- name: Run benchmarks on base branch
run: |
python3 -m pytest \
--benchmark-only \
--benchmark-json=base-benchmark.json \
--benchmark-columns=mean,stddev,iqr,ops,rounds \
${{ env.BENCHMARK_PATH }}
- name: Store base benchmark result
uses: actions/upload-artifact@v4
with:
name: base-benchmark
path: base-benchmark.json
# Checkout PR branch again to ensure artifact downloads happen in correct context
- name: Checkout PR branch again
uses: actions/checkout@v4
- name: Download PR benchmark result
uses: actions/download-artifact@v4
with:
name: pr-benchmark
- name: Download base benchmark result
uses: actions/download-artifact@v4
with:
name: base-benchmark
- name: Compare benchmarks and comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
// Read benchmark results
const prBenchmark = JSON.parse(fs.readFileSync('pr-benchmark.json', 'utf8'));
const baseBenchmark = JSON.parse(fs.readFileSync('base-benchmark.json', 'utf8'));
// Create a map of base benchmarks for easy lookup
const baseMap = {};
baseBenchmark.benchmarks.forEach(bench => {
baseMap[bench.name] = bench;
});
// Build comparison table
let tableRows = [];
let significantChanges = [];
prBenchmark.benchmarks.forEach(prBench => {
const baseBench = baseMap[prBench.name];
if (!baseBench) {
tableRows.push({
name: prBench.name,
status: '🆕 NEW',
prMean: prBench.stats.mean,
baseMean: '-',
change: '-'
});
return;
}
const prMean = prBench.stats.mean;
const baseMean = baseBench.stats.mean;
const changePercent = ((prMean - baseMean) / baseMean) * 100;
let status = '✅';
let changeStr = changePercent.toFixed(2) + '%';
// Flag significant changes (> 10% regression)
if (changePercent > 10) {
status = '⚠️ SLOWER';
significantChanges.push(`${prBench.name}: ${changePercent.toFixed(2)}% slower`);
} else if (changePercent < -10) {
status = '🚀 FASTER';
}
tableRows.push({
name: prBench.name,
status: status,
prMean: prMean,
baseMean: baseMean,
change: changeStr
});
});
// Format table
let comment = '## 📊 Performance Benchmark Results\n\n';
if (significantChanges.length > 0) {
comment += '### ⚠️ Significant Performance Changes Detected\n\n';
significantChanges.forEach(change => {
comment += `- ${change}\n`;
});
comment += '\n';
}
comment += '### Detailed Comparison\n\n';
comment += '| Benchmark | Status | PR Mean (s) | Base Mean (s) | Change |\n';
comment += '|-----------|--------|-------------|---------------|--------|\n';
tableRows.forEach(row => {
const prMeanStr = typeof row.prMean === 'number' ? row.prMean.toExponential(4) : row.prMean;
const baseMeanStr = typeof row.baseMean === 'number' ? row.baseMean.toExponential(4) : row.baseMean;
comment += `| ${row.name} | ${row.status} | ${prMeanStr} | ${baseMeanStr} | ${row.change} |\n`;
});
comment += '\n---\n';
comment += `**Base Branch:** \`${context.payload.pull_request.base.ref}\` (${context.payload.pull_request.base.sha.substring(0, 7)})\n`;
comment += `**PR Branch:** \`${context.payload.pull_request.head.ref}\` (${context.payload.pull_request.head.sha.substring(0, 7)})\n`;
if (prBenchmark.machine_info) {
if (prBenchmark.machine_info.python_version) {
comment += `**Python Version:** ${prBenchmark.machine_info.python_version}\n`;
}
if (prBenchmark.machine_info.system && prBenchmark.machine_info.release) {
comment += `**Platform:** ${prBenchmark.machine_info.system} ${prBenchmark.machine_info.release}\n`;
}
}
// Post comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});