|
13 | 13 | */ |
14 | 14 |
|
15 | 15 | import { readFileSync } from 'node:fs'; |
| 16 | +import { formatTime, deltaEmoji, parsePairs, readBenchJSON } from './bench-utils.mjs'; |
16 | 17 |
|
17 | 18 | const marker = '<!-- bench-compare -->'; |
18 | 19 |
|
@@ -43,74 +44,30 @@ const jsonPath = process.env.BENCH_JSON_OUTPUT; |
43 | 44 |
|
44 | 45 | if (jsonPath) { |
45 | 46 | try { |
46 | | - const json = JSON.parse(readFileSync(jsonPath, 'utf8')); |
47 | | - summarySection = buildSummary(json); |
| 47 | + const rows = parsePairs(readBenchJSON(jsonPath)); |
| 48 | + |
| 49 | + if (rows.length > 0) { |
| 50 | + const tableRows = rows.map(({ name, control, experiment, delta }) => { |
| 51 | + const emoji = deltaEmoji(delta); |
| 52 | + const sign = delta > 0 ? '+' : ''; |
| 53 | + return `| ${emoji} | ${name} | ${formatTime(control)} | ${formatTime(experiment)} | ${sign}${delta.toFixed(1)}% |`; |
| 54 | + }); |
| 55 | + |
| 56 | + summarySection = [ |
| 57 | + '', |
| 58 | + '| | Benchmark | Control (p50) | Experiment (p50) | Δ |', |
| 59 | + '|---|---|---:|---:|---:|', |
| 60 | + ...tableRows, |
| 61 | + '', |
| 62 | + '> 🟢 faster · 🔴 slower · 🟠 slightly slower · ⚪ within 2%', |
| 63 | + '', |
| 64 | + ].join('\n'); |
| 65 | + } |
48 | 66 | } catch { |
49 | 67 | // JSON not available or malformed — skip summary |
50 | 68 | } |
51 | 69 | } |
52 | 70 |
|
53 | | -function formatTime(ns) { |
54 | | - if (ns >= 1e6) return `${(ns / 1e6).toFixed(2)} ms`; |
55 | | - if (ns >= 1e3) return `${(ns / 1e3).toFixed(2)} µs`; |
56 | | - return `${ns.toFixed(2)} ns`; |
57 | | -} |
58 | | - |
59 | | -function deltaEmoji(pct) { |
60 | | - const abs = Math.abs(pct); |
61 | | - // negative pct means experiment is faster (lower time = better) |
62 | | - if (abs < 2) return '⚪'; |
63 | | - if (pct <= -5) return '🟢'; |
64 | | - if (pct >= 5) return '🔴'; |
65 | | - if (pct < 0) return '🟢'; |
66 | | - return '🟠'; |
67 | | -} |
68 | | - |
69 | | -function buildSummary(json) { |
70 | | - const benchmarks = json.benchmarks || []; |
71 | | - |
72 | | - // In comparison mode, benchmarks come in pairs inside summary groups. |
73 | | - // Each benchmark alias is like "gts small (control)" / "gts small (experiment)". |
74 | | - // Group them by stripping the suffix. |
75 | | - const pairs = new Map(); |
76 | | - |
77 | | - for (const trial of benchmarks) { |
78 | | - for (const r of trial.runs || []) { |
79 | | - if (!r.stats) continue; |
80 | | - const m = r.name.match(/^(.+)\s+\((control|experiment)\)$/); |
81 | | - if (!m) continue; |
82 | | - const [, key, role] = m; |
83 | | - if (!pairs.has(key)) pairs.set(key, {}); |
84 | | - pairs.get(key)[role] = r.stats; |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - if (pairs.size === 0) return ''; |
89 | | - |
90 | | - const rows = []; |
91 | | - for (const [name, { control, experiment }] of pairs) { |
92 | | - if (!control || !experiment) continue; |
93 | | - const delta = ((experiment.avg - control.avg) / control.avg) * 100; |
94 | | - const emoji = deltaEmoji(delta); |
95 | | - const sign = delta > 0 ? '+' : ''; |
96 | | - rows.push( |
97 | | - `| ${emoji} | ${name} | ${formatTime(control.avg)} | ${formatTime(experiment.avg)} | ${sign}${delta.toFixed(1)}% |` |
98 | | - ); |
99 | | - } |
100 | | - |
101 | | - if (rows.length === 0) return ''; |
102 | | - |
103 | | - return [ |
104 | | - '', |
105 | | - '| | Benchmark | Control (avg) | Experiment (avg) | Δ |', |
106 | | - '|---|---|---:|---:|---:|', |
107 | | - ...rows, |
108 | | - '', |
109 | | - '> 🟢 faster · 🔴 slower · 🟠 slightly slower · ⚪ within 2%', |
110 | | - '', |
111 | | - ].join('\n'); |
112 | | -} |
113 | | - |
114 | 71 | // --------------------------------------------------------------------------- |
115 | 72 | // Assemble comment |
116 | 73 | // --------------------------------------------------------------------------- |
|
0 commit comments