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-cli.mjs
More file actions
72 lines (59 loc) · 1.97 KB
/
format-bench-cli.mjs
File metadata and controls
72 lines (59 loc) · 1.97 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
/* eslint-disable n/no-process-exit */
/**
* Format benchmark JSON results as a CLI-friendly summary table.
*
* Environment variables:
* BENCH_JSON_OUTPUT - Path to the JSON bench results
*/
import { formatTime, deltaEmoji, parsePairs, readBenchJSON } from './bench-utils.mjs';
const jsonPath = process.env.BENCH_JSON_OUTPUT;
if (!jsonPath) {
console.error('BENCH_JSON_OUTPUT not set');
process.exit(1);
}
let json;
try {
json = readBenchJSON(jsonPath);
} catch (e) {
console.error(`Could not read ${jsonPath}: ${e.message}`);
process.exit(1);
}
const rows = parsePairs(json);
if (rows.length === 0) {
console.log('No comparison data found.');
process.exit(0);
}
// Calculate column widths
const nameW = Math.max('Benchmark'.length, ...rows.map((r) => r.name.length));
const ctrlW = Math.max('Control (p50)'.length, ...rows.map((r) => formatTime(r.control).length));
const expW = Math.max(
'Experiment (p50)'.length,
...rows.map((r) => formatTime(r.experiment).length)
);
const deltaW = Math.max(
'Δ'.length,
...rows.map((r) => {
const sign = r.delta > 0 ? '+' : '';
return `${sign}${r.delta.toFixed(1)}%`.length;
})
);
// Print table
const pad = (s, w, right) => (right ? s.padStart(w) : s.padEnd(w));
console.log();
console.log(
` ${pad('Benchmark', nameW)} ${pad('Control (p50)', ctrlW, true)} ${pad('Experiment (p50)', expW, true)} ${pad('Δ', deltaW, true)}`
);
console.log(
` ${'─'.repeat(nameW)} ${'─'.repeat(ctrlW)} ${'─'.repeat(expW)} ${'─'.repeat(deltaW)}`
);
for (const row of rows) {
const emoji = deltaEmoji(row.delta);
const sign = row.delta > 0 ? '+' : '';
const deltaStr = `${sign}${row.delta.toFixed(1)}%`;
console.log(
`${emoji} ${pad(row.name, nameW)} ${pad(formatTime(row.control), ctrlW, true)} ${pad(formatTime(row.experiment), expW, true)} ${pad(deltaStr, deltaW, true)}`
);
}
console.log();
console.log('🟢 faster · 🔴 slower · 🟠 slightly slower · ⚪ within 2%');
console.log();