|
| 1 | +// Per-commit wall-clock trend for the perf spec (#18). |
| 2 | +// |
| 3 | +// node tools/perf_trend.js check <wall-clock.json> <history-dir> |
| 4 | +// node tools/perf_trend.js record <wall-clock.json> <history-dir> <sha> <commit-date> |
| 5 | +// |
| 6 | +// <wall-clock.json> is what tests/browser/perf.spec.js writes when |
| 7 | +// PERF_WALLCLOCK_OUT is set. <history-dir> holds one JSON file per commit on |
| 8 | +// main. CI keeps it on the perf-history branch. |
| 9 | +// |
| 10 | +// check compares render p50/p90/p99 and droppedFrames, per canvas side, |
| 11 | +// against the median of the last HISTORY_WINDOW entries. It fails only when a |
| 12 | +// value moves by FACTOR or more in either direction. The numbers come from a |
| 13 | +// GitHub-hosted runner, so smaller changes are noise. Values are raised to a |
| 14 | +// floor before comparing, so 0.5ms against 2ms does not count as 4x. With no |
| 15 | +// history yet, check passes. |
| 16 | +// |
| 17 | +// record writes one file named <commit-date>-<sha>, so the files sort in |
| 18 | +// commit order and two runs never touch the same file. |
| 19 | + |
| 20 | +const fs = require("fs"); |
| 21 | +const path = require("path"); |
| 22 | + |
| 23 | +const FACTOR = 10; |
| 24 | +const HISTORY_WINDOW = 10; |
| 25 | +// Render times are in ms. droppedFrames is a count of renders over 100ms. |
| 26 | +const FLOOR = { p50: 5, p90: 5, p99: 5, droppedFrames: 1 }; |
| 27 | + |
| 28 | +function usage() { |
| 29 | + console.log("usage: node tools/perf_trend.js check <wall-clock.json> <history-dir>"); |
| 30 | + console.log(" node tools/perf_trend.js record <wall-clock.json> <history-dir> <sha> <commit-date>"); |
| 31 | + process.exit(2); |
| 32 | +} |
| 33 | + |
| 34 | +function readJson(file) { |
| 35 | + return JSON.parse(fs.readFileSync(file, "utf8")); |
| 36 | +} |
| 37 | + |
| 38 | +// Flatten one wall-clock result to { "F.p50": 5.7, "F.droppedFrames": 0, ... }. |
| 39 | +// Sides that did not render are left out. |
| 40 | +function metrics(wallClock) { |
| 41 | + const out = {}; |
| 42 | + const stats = wallClock.renderStats || {}; |
| 43 | + for (const side of Object.keys(stats)) { |
| 44 | + const s = stats[side]; |
| 45 | + if (!s) continue; |
| 46 | + out[side + ".p50"] = s.total.p50; |
| 47 | + out[side + ".p90"] = s.total.p90; |
| 48 | + out[side + ".p99"] = s.total.p99; |
| 49 | + out[side + ".droppedFrames"] = s.droppedFrames; |
| 50 | + } |
| 51 | + return out; |
| 52 | +} |
| 53 | + |
| 54 | +function median(values) { |
| 55 | + const v = values.slice().sort((a, b) => a - b); |
| 56 | + const mid = Math.floor(v.length / 2); |
| 57 | + return v.length % 2 ? v[mid] : (v[mid - 1] + v[mid]) / 2; |
| 58 | +} |
| 59 | + |
| 60 | +function loadHistory(dir) { |
| 61 | + if (!fs.existsSync(dir)) return []; |
| 62 | + return fs.readdirSync(dir) |
| 63 | + .filter((f) => f.endsWith(".json")) |
| 64 | + .sort() |
| 65 | + .map((f) => readJson(path.join(dir, f))); |
| 66 | +} |
| 67 | + |
| 68 | +function check(wallClockFile, historyDir) { |
| 69 | + const current = metrics(readJson(wallClockFile)); |
| 70 | + const history = loadHistory(historyDir).slice(-HISTORY_WINDOW); |
| 71 | + if (history.length === 0) { |
| 72 | + console.log("no perf history yet, nothing to compare against"); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + const past = history.map((h) => metrics(h.wallClock)); |
| 76 | + console.log("comparing against the median of the last " + history.length + " commits on main"); |
| 77 | + console.log("fails on a change of " + FACTOR + "x or more\n"); |
| 78 | + |
| 79 | + let failed = 0; |
| 80 | + for (const key of Object.keys(current)) { |
| 81 | + const values = past.map((m) => m[key]).filter((v) => typeof v === "number"); |
| 82 | + if (values.length === 0) continue; |
| 83 | + const ref = median(values); |
| 84 | + const floor = FLOOR[key.split(".")[1]]; |
| 85 | + const ratio = Math.max(current[key], floor) / Math.max(ref, floor); |
| 86 | + const bad = ratio >= FACTOR || ratio <= 1 / FACTOR; |
| 87 | + if (bad) failed++; |
| 88 | + console.log( |
| 89 | + (bad ? "FAIL " : "ok ") + key.padEnd(18) + |
| 90 | + " now " + String(round(current[key])).padStart(8) + |
| 91 | + " median " + String(round(ref)).padStart(8) + |
| 92 | + " x" + round(ratio) |
| 93 | + ); |
| 94 | + } |
| 95 | + if (failed) { |
| 96 | + console.log("\n" + failed + " wall-clock metric(s) changed by " + FACTOR + "x or more"); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +function round(x) { |
| 103 | + return Math.round(x * 100) / 100; |
| 104 | +} |
| 105 | + |
| 106 | +function record(wallClockFile, historyDir, sha, commitDate) { |
| 107 | + const date = new Date(commitDate); |
| 108 | + if (isNaN(date)) { |
| 109 | + console.log("bad commit date: " + commitDate); |
| 110 | + return 2; |
| 111 | + } |
| 112 | + // 2026-10-04T12:34:56.000Z -> 20261004T123456Z |
| 113 | + const stamp = date.toISOString().replace(/[-:]/g, "").replace(/\.\d+/, ""); |
| 114 | + const file = path.join(historyDir, stamp + "-" + sha.slice(0, 12) + ".json"); |
| 115 | + const entry = { sha: sha, date: date.toISOString(), wallClock: readJson(wallClockFile) }; |
| 116 | + fs.mkdirSync(historyDir, { recursive: true }); |
| 117 | + fs.writeFileSync(file, JSON.stringify(entry, null, 2) + "\n"); |
| 118 | + console.log("wrote " + file); |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +const [cmd, wallClockFile, historyDir, sha, commitDate] = process.argv.slice(2); |
| 123 | +if (cmd === "check" && wallClockFile && historyDir) { |
| 124 | + process.exit(check(wallClockFile, historyDir)); |
| 125 | +} else if (cmd === "record" && wallClockFile && historyDir && sha && commitDate) { |
| 126 | + process.exit(record(wallClockFile, historyDir, sha, commitDate)); |
| 127 | +} else { |
| 128 | + usage(); |
| 129 | +} |