forked from ember-tooling/ember-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-bench-comment.mjs
More file actions
92 lines (78 loc) · 2.96 KB
/
format-bench-comment.mjs
File metadata and controls
92 lines (78 loc) · 2.96 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
/**
* Format benchmark comparison results into a GitHub PR comment.
*
* Reads the plain-text mitata output and (optionally) the JSON results from
* the bench run, then produces a GitHub-flavored markdown comment with:
* 1. A summary table (when comparison data is available)
* 2. Full mitata output in a collapsible <details> section
*
* Environment variables:
* BENCH_OUTPUT_FILE - Path to the plain-text bench output
* BENCH_JSON_OUTPUT - Path to the JSON bench results (optional)
* BENCH_JOB_SUCCESS - Set to "true" if the benchmark job succeeded
*/
import { readFileSync } from 'node:fs';
import { formatTime, deltaEmoji, parsePairs, readBenchJSON } from './bench-utils.mjs';
const marker = '<!-- bench-compare -->';
// ---------------------------------------------------------------------------
// Read raw mitata output
// ---------------------------------------------------------------------------
let rawOutput;
try {
rawOutput = readFileSync(process.env.BENCH_OUTPUT_FILE, 'utf8').trim();
} catch {
console.warn('Warning: could not read BENCH_OUTPUT_FILE; using placeholder text.');
rawOutput = '(no output — benchmark may have failed to start)';
}
// Strip any lines before the mitata header (safety net for leaked setup messages)
const benchStart = rawOutput.search(/^(clk:|benchmark\b)/m);
if (benchStart > 0) {
rawOutput = rawOutput.slice(benchStart);
}
// ---------------------------------------------------------------------------
// Read JSON results (if available) and build summary
// ---------------------------------------------------------------------------
let summarySection = '';
const jsonPath = process.env.BENCH_JSON_OUTPUT;
if (jsonPath) {
try {
const rows = parsePairs(readBenchJSON(jsonPath));
if (rows.length > 0) {
const tableRows = rows.map(({ name, control, experiment, delta }) => {
const emoji = deltaEmoji(delta);
const sign = delta > 0 ? '+' : '';
return `| ${emoji} | ${name} | ${formatTime(control)} | ${formatTime(experiment)} | ${sign}${delta.toFixed(1)}% |`;
});
summarySection = [
'',
'| | Benchmark | Control (p50) | Experiment (p50) | Δ |',
'|---|---|---:|---:|---:|',
...tableRows,
'',
'> 🟢 faster · 🔴 slower · 🟠 slightly slower · ⚪ within 2%',
'',
].join('\n');
}
} catch {
// JSON not available or malformed — skip summary
}
}
// ---------------------------------------------------------------------------
// Assemble comment
// ---------------------------------------------------------------------------
const success = process.env.BENCH_JOB_SUCCESS === 'true';
const heading = success ? '## 🏎️ Benchmark Comparison' : '## ❌ Benchmark Comparison (failed)';
const body = [
marker,
heading,
summarySection,
'<details>',
'<summary>Full mitata output</summary>',
'',
'```',
rawOutput,
'```',
'',
'</details>',
].join('\n');
process.stdout.write(body + '\n');