|
| 1 | +// Compares two result files produced by perf/run.mjs (one or more passes |
| 2 | +// each) and prints a markdown summary with the median time per benchmark. |
| 3 | +// |
| 4 | +// usage: node perf/compare.mjs <before.json> <after.json> |
| 5 | + |
| 6 | +import * as fs from "node:fs"; |
| 7 | + |
| 8 | +const [beforeFile, afterFile] = process.argv.slice(2); |
| 9 | +if (beforeFile === undefined || afterFile === undefined) { |
| 10 | + console.error("usage: node perf/compare.mjs <before.json> <after.json>"); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +// [{name, ms, value} | {name, error}] per pass -> {name: {median, value, error}} |
| 15 | +function aggregate(file) { |
| 16 | + const passes = JSON.parse(fs.readFileSync(file, "utf-8")); |
| 17 | + const byName = new Map(); |
| 18 | + for (const pass of passes) { |
| 19 | + for (const r of pass) { |
| 20 | + const agg = byName.get(r.name) ?? {times: []}; |
| 21 | + if (r.error !== undefined) { |
| 22 | + agg.error = r.error; |
| 23 | + } else { |
| 24 | + agg.times.push(r.ms); |
| 25 | + agg.value = r.value; |
| 26 | + } |
| 27 | + byName.set(r.name, agg); |
| 28 | + } |
| 29 | + } |
| 30 | + for (const agg of byName.values()) { |
| 31 | + agg.times.sort((x, y) => x - y); |
| 32 | + agg.median = agg.times[Math.floor(agg.times.length / 2)]; |
| 33 | + } |
| 34 | + return byName; |
| 35 | +} |
| 36 | + |
| 37 | +const before = aggregate(beforeFile); |
| 38 | +const after = aggregate(afterFile); |
| 39 | +const passCount = JSON.parse(fs.readFileSync(afterFile, "utf-8")).length; |
| 40 | + |
| 41 | +const lines = []; |
| 42 | +lines.push("## Performance regression results"); |
| 43 | +lines.push(""); |
| 44 | +lines.push("| Benchmark | Base | PR | Change |"); |
| 45 | +lines.push("|---|---:|---:|---:|"); |
| 46 | + |
| 47 | +for (const [name, a] of after) { |
| 48 | + const b = before.get(name); |
| 49 | + const col = agg => agg === undefined ? "n/a" : agg.error !== undefined ? "ERROR" : `${Math.round(agg.median)}ms`; |
| 50 | + let changeCol = ""; |
| 51 | + if (b !== undefined && b.error === undefined && a.error === undefined) { |
| 52 | + const delta = ((a.median - b.median) / b.median) * 100; |
| 53 | + const sign = delta >= 0 ? "+" : ""; |
| 54 | + let marker = ""; |
| 55 | + if (delta > 15) { |
| 56 | + marker = " 🔺"; |
| 57 | + } else if (delta < -15) { |
| 58 | + marker = " 🟢"; |
| 59 | + } |
| 60 | + changeCol = `${sign}${delta.toFixed(1)}%${marker}`; |
| 61 | + if (a.value !== b.value) { |
| 62 | + const fmt = v => String(v).replace(/\r?\n/g, "\\n").replace(/\|/g, "\\|"); |
| 63 | + changeCol += ` ⚠️ result changed: \`${fmt(b.value)}\` → \`${fmt(a.value)}\``; |
| 64 | + } |
| 65 | + } else if (a.error !== undefined || b?.error !== undefined) { |
| 66 | + changeCol = `⚠️ ${a.error ?? b.error}`; |
| 67 | + } |
| 68 | + lines.push(`| ${name} | ${col(b)} | ${col(a)} | ${changeCol} |`); |
| 69 | +} |
| 70 | + |
| 71 | +lines.push(""); |
| 72 | +lines.push(`Median of ${passCount} interleaved passes of the transpiled interpreter — small deltas are noise.`); |
| 73 | + |
| 74 | +console.log(lines.join("\n")); |
0 commit comments