-
Notifications
You must be signed in to change notification settings - Fork 2
141 lines (119 loc) · 4.98 KB
/
benchmark-comment.yml
File metadata and controls
141 lines (119 loc) · 4.98 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
name: Benchmarks PR Comment
on:
workflow_run:
workflows: [ "Benchmarks" ]
types: [ completed ]
jobs:
comment:
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion != 'cancelled' && github.event.workflow_run.conclusion != 'skipped' && github.event.workflow_run.pull_requests[0] != null }}
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: write
steps:
- name: Download benchmark artifact
uses: actions/download-artifact@v8
with:
name: benchmark-results
path: benchmark
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
if-no-files-found: warn
- name: Comment PR with benchmark results
uses: actions/github-script@v9
with:
script: |
const fs = require('fs');
const path = require('path');
const pr = context.payload.workflow_run.pull_requests[0];
if (!pr) {
core.info('No associated pull request; skipping comment.');
return;
}
const resultsPath = path.join(process.cwd(), 'benchmark', 'benchmark-results.json');
if (!fs.existsSync(resultsPath)) {
core.setFailed('benchmark-results.json not found in artifact.');
return;
}
const raw = fs.readFileSync(resultsPath, 'utf8').trim();
if (!raw) {
core.setFailed('benchmark-results.json is empty');
return;
}
let parsed;
try {
parsed = JSON.parse(raw);
} catch (error) {
core.setFailed(`Failed to parse benchmark-results.json: ${error.message}`);
throw error;
}
const benchmarks = Array.isArray(parsed)
? parsed
: Array.isArray(parsed?.benchmarks)
? parsed.benchmarks
: [];
const runUrl = context.payload.workflow_run.html_url;
const commentLines = [
'## 📊 Benchmark Results',
'',
`Triggered by [workflow run](${runUrl}).`,
'',
];
if (benchmarks.length === 0) {
commentLines.push('⚠️ **No benchmark data was produced.** Check the workflow run logs for failures in the benchmark phase.');
} else {
commentLines.push('| Benchmark | Score | Error | Unit |');
commentLines.push('|-----------|-------|-------|------|');
let hasRegression = false;
for (const result of benchmarks) {
const score = Number(result?.primaryMetric?.score ?? NaN);
const error = Number(result?.primaryMetric?.scoreError ?? NaN);
const unit = result?.primaryMetric?.scoreUnit ?? 'ops';
const benchmark = (result?.benchmark ?? 'unknown').split('.').pop();
if (!Number.isFinite(score)) {
commentLines.push(`| ⚠️ ${benchmark} | n/a | n/a | ${unit} |`);
core.warning(`Missing score for benchmark ${result?.benchmark ?? 'unknown'}`);
continue;
}
const emoji = score > 50 ? '🔴' : '🟢';
if (score > 50) hasRegression = true;
const errorDisplay = Number.isFinite(error) ? error.toFixed(3) : 'n/a';
commentLines.push(`| ${emoji} ${benchmark} | ${score.toFixed(3)} | ±${errorDisplay} | ${unit} |`);
}
commentLines.push('');
if (hasRegression) {
commentLines.push('⚠️ **Performance regression detected**: One or more benchmarks exceeded the 50ms threshold.');
} else {
commentLines.push('✅ **All benchmarks passed** performance thresholds.');
}
}
const comment = commentLines.join('\n');
const { owner, repo } = context.repo;
const issue_number = pr.number;
const { data: comments } = await github.rest.issues.listComments({
owner,
repo,
issue_number,
});
const existing = comments.find(comment =>
comment.user.type === 'Bot' && comment.body.includes('📊 Benchmark Results')
);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body: comment,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: comment,
});
}
if (benchmarks.length === 0) {
core.setFailed('No benchmark entries were found in benchmark-results.json.');
}