Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

docs(testing): bisect the startup slowdown, retract the editor-open one

EtienneLescot30b268726659
diff --git a/scripts/perf-ab/analyze-profiles.mjs b/scripts/perf-ab/analyze-profiles.mjs
new file mode 100644
index 000000000..25d5f4334
--- /dev/null
+++ b/scripts/perf-ab/analyze-profiles.mjs
@@ -0,0 +1,60 @@
+// Self time per script and per function in main-process startup profiles, median over runs per build.
+import fs from "node:fs";
+import path from "node:path";
+
+const dir = process.argv[2];
+const med = (a) => {
+ const s = [...a].sort((x, y) => x - y);
+ const h = s.length >> 1;
+ return s.length ? (s.length % 2 ? s[h] : (s[h - 1] + s[h]) / 2) : 0;
+};
+const byVer = {};
+for (const f of fs.readdirSync(dir).filter((f) => f.endsWith(".cpuprofile"))) {
+ const meta = JSON.parse(
+ fs.readFileSync(path.join(dir, f.replace(".cpuprofile", ".json")), "utf8"),
+ );
+ const p = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8"));
+ const nodes = new Map(p.nodes.map((n) => [n.id, n]));
+ const self = { file: {}, fn: {}, total: 0, idle: 0 };
+ let t = 0;
+ const limit = meta.hudTargetAt * 1000; // µs: main process until the HUD page exists
+ for (let i = 0; i < p.samples.length; i++) {
+ t += p.timeDeltas[i];
+ if (t > limit) break;
+ const n = nodes.get(p.samples[i]);
+ const cf = n.callFrame;
+ const dt = (p.timeDeltas[i + 1] ?? 0) / 1000;
+ if (cf.functionName === "(idle)") {
+ self.idle += dt;
+ continue;
+ }
+ const file = (cf.url || cf.functionName).split(/[/]/).slice(-2).join("/");
+ self.file[file] = (self.file[file] ?? 0) + dt;
+ const key = `${cf.functionName || "(anon)"} @ ${file}:${cf.lineNumber}`;
+ self.fn[key] = (self.fn[key] ?? 0) + dt;
+ self.total += dt;
+ }
+ if (!byVer[meta.ver]) byVer[meta.ver] = [];
+ byVer[meta.ver].push(self);
+}
+for (const [ver, list] of Object.entries(byVer)) {
+ console.log(
+ `\n== ${ver} (n=${list.length}) busy ${med(list.map((s) => s.total)).toFixed(0)} ms, idle ${med(list.map((s) => s.idle)).toFixed(0)} ms before the HUD page`,
+ );
+ const files = [...new Set(list.flatMap((s) => Object.keys(s.file)))]
+ .map((k) => [k, med(list.map((s) => s.file[k] ?? 0))])
+ .sort((x, y) => y[1] - x[1])
+ .slice(0, 8);
+ for (const [k, v] of files) console.log(` ${v.toFixed(1).padStart(7)} ms ${k}`);
+}
+const fnDiff = () => {
+ const all = new Set(Object.values(byVer).flatMap((l) => l.flatMap((s) => Object.keys(s.fn))));
+ const rows = [...all].map((k) => [
+ k,
+ med((byVer.main ?? []).map((s) => s.fn[k] ?? 0)) -
+ med((byVer.v200 ?? []).map((s) => s.fn[k] ?? 0)),
+ ]);
+ return rows.sort((x, y) => y[1] - x[1]).slice(0, 15);
+};
+console.log("\n== functions with the most extra self time in main (ms)");
+for (const [k, v] of fnDiff()) console.log(` ${v.toFixed(1).padStart(6)} ${k}`);
diff --git a/scripts/perf-ab/attribute-hud.cjs b/scripts/perf-ab/attribute-hud.cjs
new file mode 100644
index 000000000..892021e28
--- /dev/null
+++ b/scripts/perf-ab/attribute-hud.cjs
@@ -0,0 +1,88 @@
+// Self time of HUD startup profiles attributed to original sources through the source maps.
+// node attribute-hud.cjs <profilesDir> <ver>=<distAssetsDir> ...
+const fs = require("node:fs");
+const path = require("node:path");
+const { SourceMapConsumer } = require("source-map-js");
+const [dir, ...specs] = process.argv.slice(2);
+const med = (a) => {
+ const s = [...a].sort((x, y) => x - y);
+ const h = s.length >> 1;
+ return s.length ? (s.length % 2 ? s[h] : (s[h - 1] + s[h]) / 2) : 0;
+};
+const maps = {};
+const per = {};
+for (const spec of specs) {
+ const [ver, assets] = spec.split("=");
+ maps[ver] = {};
+ for (const f of fs.readdirSync(assets).filter((f) => f.endsWith(".js.map")))
+ maps[ver][f.replace(".map", "")] = new SourceMapConsumer(
+ JSON.parse(fs.readFileSync(path.join(assets, f), "utf8")),
+ );
+ per[ver] = [];
+ for (const f of fs
+ .readdirSync(dir)
+ .filter(
+ (f) => f.startsWith(`hud-${ver}-`) && f.endsWith(".cpuprofile") && !f.includes("-r0."),
+ )) {
+ const p = JSON.parse(fs.readFileSync(path.join(dir, f), "utf8"));
+ const nodes = new Map(p.nodes.map((n) => [n.id, n]));
+ const acc = { "(total busy)": 0 };
+ const cache = new Map();
+ for (let i = 0; i < p.samples.length; i++) {
+ const cf = nodes.get(p.samples[i]).callFrame;
+ if (cf.functionName === "(idle)") continue;
+ const dt = (p.timeDeltas[i + 1] ?? 0) / 1000;
+ acc["(total busy)"] += dt;
+ const file = (cf.url || "").split("/").pop();
+ let key = cf.url ? `[${file.replace(/-[A-Za-z0-9_-]{8}\.js$/, ".js")}]` : cf.functionName;
+ const m = maps[ver][file];
+ if (m) {
+ const ck = `${file}:${cf.lineNumber}:${cf.columnNumber}`;
+ if (!cache.has(ck)) {
+ const o = m.originalPositionFor({ line: cf.lineNumber + 1, column: cf.columnNumber });
+ cache.set(
+ ck,
+ o.source
+ ? `${o.source.replace(/^.*?(node_modules|src)\//, "$1/")}${o.name ? ` ${o.name}` : ""}`
+ : key,
+ );
+ }
+ key = cache.get(ck);
+ }
+ acc[key] = (acc[key] ?? 0) + dt;
+ }
+ per[ver].push(acc);
+ }
+}
+const [a, b] = Object.keys(per);
+const keys = [...new Set(Object.values(per).flatMap((l) => l.flatMap((x) => Object.keys(x))))];
+const rows = keys.map((k) => [
+ k,
+ med(per[a].map((x) => x[k] ?? 0)),
+ med(per[b].map((x) => x[k] ?? 0)),
+]);
+console.log(`${a} n=${per[a].length}, ${b} n=${per[b].length}; self ms ${a} → ${b}`);
+for (const [k, x, y] of rows.sort((p, q) => q[2] - q[1] - (p[2] - p[1])).slice(0, 25))
+ console.log(
+ `${x.toFixed(1).padStart(6)} → ${y.toFixed(1).padStart(6)} ${(y - x >= 0 ? "+" : "") + (y - x).toFixed(1)} ${k}`,
+ );
+// by source file
+const fileOf = (k) => k.replace(/ [^ ]+$/, "");
+const fk = [...new Set(keys.map(fileOf))];
+const frows = fk.map((f) => [
+ f,
+ ...[a, b].map((v) =>
+ med(
+ per[v].map((x) =>
+ Object.entries(x)
+ .filter(([k]) => fileOf(k) === f)
+ .reduce((s, [, t]) => s + t, 0),
+ ),
+ ),
+ ),
+]);
+console.log("\nby source file, biggest increases");
+for (const [f, x, y] of frows.sort((p, q) => q[2] - q[1] - (p[2] - p[1])).slice(0, 15))
+ console.log(
+ `${x.toFixed(1).padStart(6)} → ${y.toFixed(1).padStart(6)} ${(y - x >= 0 ? "+" : "") + (y - x).toFixed(1)} ${f}`,
+ );
diff --git a/scripts/perf-ab/cdp.mjs b/scripts/perf-ab/cdp.mjs
index ba3effe0f..d88b6d9bc 100644
--- a/scripts/perf-ab/cdp.mjs
+++ b/scripts/perf-ab/cdp.mjs
@@ -41,11 +41,11 @@ export async function connect(target) {
for (const l of listeners) l(msg);
}
};
- const send = (method, params = {}) =>
+ const send = (method, params = {}, sessionId = undefined) =>
new Promise((ok, ko) => {
const id = next++;
pending.set(id, { ok, ko });
- ws.send(JSON.stringify({ id, method, params }));
+ ws.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) }));
});
const evaluate = async (expression) => {
const r = await send("Runtime.evaluate", {
diff --git a/scripts/perf-ab/open-bench.mjs b/scripts/perf-ab/open-bench.mjs
new file mode 100644
index 000000000..fdbd98232
--- /dev/null
+++ b/scripts/perf-ab/open-bench.mjs
@@ -0,0 +1,181 @@
+// Startup and editor open, split into phases. No playback: one session takes ~10 s.
+// node open-bench.mjs --ver <label> --app <dir> --profile <dir> --project <file> --out <dir> --run <n>
+import { execFileSync, spawn } from "node:child_process";
+import fs from "node:fs";
+import path from "node:path";
+import { parseArgs } from "node:util";
+import { connect, sleep, waitTarget } from "./cdp.mjs";
+
+process.on("uncaughtException", (e) => {
+ console.error(e);
+ process.exit(1);
+});
+const { values: a } = parseArgs({
+ options: {
+ ver: { type: "string" },
+ app: { type: "string" },
+ profile: { type: "string" },
+ project: { type: "string" },
+ out: { type: "string" },
+ run: { type: "string", default: "0" },
+ nopin: { type: "boolean", default: false },
+ },
+});
+const tag = `open-${a.ver}${a.nopin ? "-nopin" : ""}-r${a.run}`;
+fs.mkdirSync(a.out, { recursive: true });
+const projDir = path.join(a.profile, "projects");
+fs.mkdirSync(projDir, { recursive: true });
+for (const f of fs.readdirSync(projDir))
+ fs.rmSync(path.join(projDir, f), { recursive: true, force: true });
+fs.copyFileSync(a.project, path.join(projDir, path.basename(a.project)));
+fs.writeFileSync(
+ path.join(a.profile, "editor-window.json"),
+ JSON.stringify({ x: 0, y: 0, width: 1600, height: 900, maximized: true }),
+);
+
+// Watches, from inside a page, the moments that matter; times are the page's performance.now().
+const WATCH = `(() => {
+ if (window.__open) return true;
+ const o = (window.__open = {});
+ const mark = (k, ok) => { if (o[k] === undefined && ok()) o[k] = performance.now(); };
+ o.visibility = [[performance.now(), document.visibilityState]];
+ document.addEventListener("visibilitychange", () => o.visibility.push([performance.now(), document.visibilityState]));
+ const tick = () => {
+ mark("root", () => document.querySelector("#root")?.childElementCount > 0);
+ mark("hudButton", () => !!document.querySelector('[data-testid="launch-record-button"]'));
+ mark("video", () => !!document.querySelector("video"));
+ mark("videoMeta", () => [...document.querySelectorAll("video")].some((v) => v.readyState >= 1));
+ mark("canvas", () => !!document.querySelector("canvas[data-painted]"));
+ if (o.canvas === undefined || o.hudButton === undefined) setTimeout(tick, 1);
+ };
+ tick();
+ return true;
+})()`;
+const pageTimes = (c) =>
+ c.evaluate(`(() => { const n = performance.getEntriesByType("navigation")[0] || {};
+ const lt = performance.getEntriesByType("longtask").map((e) => [Math.round(e.startTime), Math.round(e.duration)]);
+ return { timeOrigin: performance.timeOrigin, dcl: n.domContentLoadedEventEnd, load: n.loadEventEnd, fcp: performance.getEntriesByName("first-contentful-paint")[0]?.startTime, marks: window.__open, lt,
+ scripts: performance.getEntriesByType("resource").filter((r) => r.initiatorType === "script" || /\\.js$/.test(r.name)).map((r) => [r.name.split("/").pop(), Math.round(r.startTime), Math.round(r.duration), r.transferSize || r.decodedBodySize]) }; })()`);
+
+const HERE = path.dirname(new URL(import.meta.url).pathname).replace(/^\/([A-Za-z]:)/, "$1");
+const exePath = path.resolve(a.app, "Openscreen.exe");
+const pin = a.nopin
+ ? null
+ : spawn(
+ "powershell",
+ [
+ "-NoProfile",
+ "-ExecutionPolicy",
+ "Bypass",
+ "-File",
+ path.join(HERE, "pin-watch.ps1"),
+ "-ExePath",
+ exePath,
+ "-Seconds",
+ "40",
+ ],
+ { stdio: ["ignore", "pipe", "ignore"] },
+ );
+if (pin)
+ await new Promise((ok) => pin.stdout.on("data", (b) => String(b).includes("ready") && ok()));
+await sleep(500);
+// 9333/9334 are a dev build's CDP and inspector ports on this machine: stay clear of them.
+const port = 9400 + Math.floor(Math.random() * 500);
+const log = fs.openSync(path.join(a.out, `${tag}.app.log`), "w");
+const t0 = Date.now();
+const app = spawn(
+ path.join(a.app, "Openscreen.exe"),
+ [`--user-data-dir=${a.profile}`, `--remote-debugging-port=${port}`],
+ { stdio: ["ignore", log, log] },
+);
+const r = { tag, ver: a.ver, run: Number(a.run), t0, pinned: !a.nopin };
+const until = async (fn, ms = 60000) => {
+ const end = Date.now() + ms;
+ while (Date.now() < end) {
+ try {
+ if (await fn()) return;
+ } catch {
+ // page not ready yet
+ }
+ await sleep(5);
+ }
+ throw new Error("timeout");
+};
+try {
+ const hudT = await waitTarget(
+ port,
+ (ts) => ts.find((t) => t.url.includes("hud-overlay")),
+ 60000,
+ 5,
+ );
+ r.hudTargetAt = Date.now() - t0;
+ const hud = await connect(hudT);
+ // The target can exist before its document: install the watch for the next document too.
+ await hud.send("Page.addScriptToEvaluateOnNewDocument", { source: WATCH });
+ await hud.evaluate(WATCH);
+ await until(() => hud.evaluate("window.__open.hudButton !== undefined"));
+ r.hud = await pageTimes(hud);
+ r.hudReadyAt = r.hud.timeOrigin + r.hud.marks.hudButton - t0;
+ await sleep(1500);
+ r.procsAtHud = Number(
+ execFileSync(
+ "powershell",
+ [
+ "-NoProfile",
+ "-Command",
+ `(Get-CimInstance Win32_Process | ? { $_.ExecutablePath -like '${a.app.replaceAll("/", "\\")}*' }).Count`,
+ ],
+ { encoding: "utf8" },
+ ).trim(),
+ );
+ r.switchAt = Date.now() - t0;
+ await hud.evaluate("void window.electronAPI.switchToEditor(), true").catch(() => {
+ // the HUD closes as the editor opens
+ });
+ const edT = await waitTarget(
+ port,
+ (ts) => ts.find((t) => t.url.includes("windowType=editor")),
+ 60000,
+ 5,
+ );
+ r.editorTargetAt = Date.now() - t0;
+ const ed = await connect(edT);
+ await ed.send("Page.addScriptToEvaluateOnNewDocument", { source: WATCH });
+ await ed.evaluate(WATCH);
+ await until(() => ed.evaluate("window.__open.canvas !== undefined"), 90000);
+ r.editor = await pageTimes(ed);
+ const at = (k) => r.editor.timeOrigin + r.editor.marks[k] - t0 - r.switchAt;
+ r.phases = {
+ windowToNavStart: r.editor.timeOrigin - t0 - r.switchAt,
+ domContentLoaded: r.editor.dcl,
+ rootRendered: r.editor.marks.root,
+ videoElement: r.editor.marks.video,
+ videoMetadata: r.editor.marks.videoMeta,
+ firstFrame: r.editor.marks.canvas,
+ total: at("canvas"),
+ };
+ r.ok = true;
+} catch (e) {
+ r.ok = false;
+ r.error = String(e?.stack ?? e);
+} finally {
+ try {
+ execFileSync("taskkill", ["/T", "/F", "/PID", String(app.pid)], { stdio: "ignore" });
+ if (pin) execFileSync("taskkill", ["/T", "/F", "/PID", String(pin.pid)], { stdio: "ignore" });
+ } catch {
+ // already gone
+ }
+ fs.writeFileSync(path.join(a.out, `${tag}.json`), JSON.stringify(r));
+ console.log(
+ JSON.stringify({
+ tag,
+ ok: r.ok,
+ err: r.error?.slice(0, 200),
+ hudReady: Math.round(r.hudReadyAt),
+ phases:
+ r.phases &&
+ Object.fromEntries(Object.entries(r.phases).map(([k, v]) => [k, Math.round(v)])),
+ }),
+ );
+ process.exit(0);
+}
diff --git a/scripts/perf-ab/pin-watch.ps1 b/scripts/perf-ab/pin-watch.ps1
new file mode 100644
index 000000000..d6b0bd43e
--- /dev/null
+++ b/scripts/perf-ab/pin-watch.ps1
@@ -0,0 +1,48 @@
+# Pins every visible top-level window whose process runs $ExePath on top as soon as it appears,
+# for $Seconds. A window a background process opens lands behind the foreground one, and a fully
+# covered Chromium window is throttled, which would time the cover instead of the app.
+# Start it BEFORE the app and wait for "ready": compiling the helper costs a CPU burst that must not
+# land on the app's startup.
+param([Parameter(Mandatory)][string]$ExePath, [int]$Seconds = 40)
+Add-Type @"
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+public static class Pin {
+ delegate bool EnumProc(IntPtr h, IntPtr l);
+ [DllImport("user32.dll")] static extern bool EnumWindows(EnumProc f, IntPtr l);
+ [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
+ [DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
+ [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
+ static readonly HashSet<IntPtr> done = new HashSet<IntPtr>();
+ static readonly Dictionary<uint, bool> ours = new Dictionary<uint, bool>();
+ static bool IsOurs(uint pid, string exe) {
+ bool v;
+ if (ours.TryGetValue(pid, out v)) return v;
+ try { v = string.Equals(Process.GetProcessById((int)pid).MainModule.FileName, exe, StringComparison.OrdinalIgnoreCase); }
+ catch { v = false; }
+ ours[pid] = v;
+ return v;
+ }
+ public static void Sweep(string exe) {
+ EnumWindows((h, l) => {
+ uint p;
+ GetWindowThreadProcessId(h, out p);
+ if (!done.Contains(h) && IsWindowVisible(h) && IsOurs(p, exe)) {
+ done.Add(h);
+ // HWND_TOPMOST, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE
+ SetWindowPos(h, new IntPtr(-1), 0, 0, 0, 0, 0x0013);
+ }
+ return true;
+ }, IntPtr.Zero);
+ }
+}
+"@
+[Pin]::Sweep($ExePath)
+"ready"
+$end = (Get-Date).AddSeconds($Seconds)
+while ((Get-Date) -lt $end) {
+ [Pin]::Sweep($ExePath)
+ Start-Sleep -Milliseconds 5
+}
diff --git a/scripts/perf-ab/profile-hud.mjs b/scripts/perf-ab/profile-hud.mjs
new file mode 100644
index 000000000..500554ee6
--- /dev/null
+++ b/scripts/perf-ab/profile-hud.mjs
@@ -0,0 +1,104 @@
+// CPU profile of the HUD renderer from its first script to HUD ready: the browser target pauses
+// every new page (waitForDebuggerOnStart), the profiler starts, then the page runs.
+// node profile-hud.mjs --ver <label> --app <dir> --profile <dir> --out <dir> --run <n>
+import { execFileSync, spawn } from "node:child_process";
+import fs from "node:fs";
+import path from "node:path";
+import { parseArgs } from "node:util";
+import { connect, sleep } from "./cdp.mjs";
+
+process.on("uncaughtException", (e) => {
+ console.error(e);
+ process.exit(1);
+});
+const { values: a } = parseArgs({
+ options: {
+ ver: { type: "string" },
+ app: { type: "string" },
+ profile: { type: "string" },
+ out: { type: "string" },
+ run: { type: "string", default: "0" },
+ },
+});
+fs.mkdirSync(a.out, { recursive: true });
+const tag = `hud-${a.ver}-r${a.run}`;
+const port = 9400 + Math.floor(Math.random() * 500);
+const app = spawn(
+ path.join(a.app, "Openscreen.exe"),
+ [`--user-data-dir=${a.profile}`, `--remote-debugging-port=${port}`],
+ { stdio: "ignore" },
+);
+try {
+ let ver = null;
+ for (let i = 0; i < 2000 && !ver; i++) {
+ try {
+ ver = await (await fetch(`http://127.0.0.1:${port}/json/version`)).json();
+ } catch {
+ await sleep(5);
+ }
+ }
+ const browser = await connect({ webSocketDebuggerUrl: ver.webSocketDebuggerUrl });
+ let resolveSession;
+ const sessionReady = new Promise((r) => (resolveSession = r));
+ browser.on((m) => {
+ if (m.method === "Target.attachedToTarget" && m.params.targetInfo.url.includes("hud-overlay"))
+ resolveSession(m.params.sessionId);
+ else if (m.method === "Target.attachedToTarget")
+ browser.send("Runtime.runIfWaitingForDebugger", {}, m.params.sessionId).catch(() => {
+ // a target that went away needs no resume
+ });
+ });
+ await browser.send("Target.setAutoAttach", {
+ autoAttach: true,
+ waitForDebuggerOnStart: true,
+ flatten: true,
+ });
+ const sid = await sessionReady;
+ await browser.send("Profiler.enable", {}, sid);
+ await browser.send("Profiler.setSamplingInterval", { interval: 100 }, sid);
+ await browser.send("Profiler.start", {}, sid);
+ await browser.send("Runtime.runIfWaitingForDebugger", {}, sid);
+ let ready = false;
+ for (let i = 0; i < 2000 && !ready; i++) {
+ try {
+ const r = await browser.send(
+ "Runtime.evaluate",
+ {
+ expression: `!!document.querySelector('[data-testid="launch-record-button"]')`,
+ returnByValue: true,
+ },
+ sid,
+ );
+ ready = r.result.value === true;
+ } catch {
+ // no context yet
+ }
+ if (!ready) await sleep(5);
+ }
+ const nav = (
+ await browser.send(
+ "Runtime.evaluate",
+ {
+ expression: "JSON.stringify(performance.getEntriesByType('navigation')[0])",
+ returnByValue: true,
+ },
+ sid,
+ )
+ ).result.value;
+ const { profile } = await browser.send("Profiler.stop", {}, sid);
+ fs.writeFileSync(path.join(a.out, `${tag}.cpuprofile`), JSON.stringify(profile));
+ fs.writeFileSync(
+ path.join(a.out, `${tag}.json`),
+ JSON.stringify({ tag, ver: a.ver, nav: JSON.parse(nav) }),
+ );
+ console.log(
+ JSON.stringify({ tag, ready, dcl: Math.round(JSON.parse(nav).domContentLoadedEventEnd) }),
+ );
+} finally {
+ try {
+ execFileSync("taskkill", ["/T", "/F", "/PID", String(app.pid)], { stdio: "ignore" });
+ } catch {
+ // already gone
+ }
+ process.exit(0);
+}
diff --git a/scripts/perf-ab/profile-startup.mjs b/scripts/perf-ab/profile-startup.mjs
new file mode 100644
index 000000000..75050cc2f
--- /dev/null
+++ b/scripts/perf-ab/profile-startup.mjs
@@ -0,0 +1,79 @@
+// CPU profile of the main process from its first line to HUD ready.
+// node profile-startup.mjs --ver <label> --app <dir> --profile <dir> --out <dir> --run <n>
+import { execFileSync, spawn } from "node:child_process";
+import fs from "node:fs";
+import path from "node:path";
+import { parseArgs } from "node:util";
+import { connect, sleep, waitTarget } from "./cdp.mjs";
+
+process.on("uncaughtException", (e) => {
+ console.error(e);
+ process.exit(1);
+});
+const { values: a } = parseArgs({
+ options: {
+ ver: { type: "string" },
+ app: { type: "string" },
+ profile: { type: "string" },
+ out: { type: "string" },
+ run: { type: "string", default: "0" },
+ },
+});
+fs.mkdirSync(a.out, { recursive: true });
+const tag = `prof-${a.ver}-r${a.run}`;
+const port = 9400 + Math.floor(Math.random() * 500);
+const inspect = port + 600;
+const app = spawn(
+ path.join(a.app, "Openscreen.exe"),
+ [`--inspect-brk=${inspect}`, `--user-data-dir=${a.profile}`, `--remote-debugging-port=${port}`],
+ { stdio: "ignore" },
+);
+const kill = () => {
+ try {
+ execFileSync("taskkill", ["/T", "/F", "/PID", String(app.pid)], { stdio: "ignore" });
+ } catch {
+ // already gone
+ }
+};
+try {
+ let node = null;
+ for (let i = 0; i < 400 && !node; i++) {
+ try {
+ node = (await (await fetch(`http://127.0.0.1:${inspect}/json/list`)).json())[0];
+ } catch {
+ await sleep(25);
+ }
+ }
+ if (!node) throw new Error("no inspector");
+ const main = await connect(node);
+ await main.send("Profiler.enable");
+ await main.send("Profiler.setSamplingInterval", { interval: 200 });
+ await main.send("Profiler.start");
+ const t0 = Date.now();
+ await main.send("Runtime.runIfWaitingForDebugger");
+ const hud = await connect(
+ await waitTarget(port, (ts) => ts.find((t) => t.url.includes("hud-overlay")), 60000, 5),
+ );
+ const hudTargetAt = Date.now() - t0;
+ for (let i = 0; i < 2000; i++) {
+ try {
+ if (await hud.evaluate(`!!document.querySelector('[data-testid="launch-record-button"]')`))
+ break;
+ } catch {
+ // document not there yet
+ }
+ await sleep(5);
+ }
+ const hudReadyAt = Date.now() - t0;
+ await sleep(300);
+ const { profile } = await main.send("Profiler.stop");
+ fs.writeFileSync(path.join(a.out, `${tag}.cpuprofile`), JSON.stringify(profile));
+ fs.writeFileSync(
+ path.join(a.out, `${tag}.json`),
+ JSON.stringify({ tag, ver: a.ver, hudTargetAt, hudReadyAt }),
+ );
+ console.log(JSON.stringify({ tag, hudTargetAt, hudReadyAt }));
+} finally {
+ kill();
+ process.exit(0);
+}
diff --git a/scripts/perf-ab/summarize-open.mjs b/scripts/perf-ab/summarize-open.mjs
new file mode 100644
index 000000000..6675f80f3
--- /dev/null
+++ b/scripts/perf-ab/summarize-open.mjs
@@ -0,0 +1,45 @@
+// Median of each open phase per build (round 0 = warm-up, left out), and a Mann-Whitney z of each
+// build against the first one listed.
+// node summarize-open.mjs <dir> <build> [<build> ...]
+import fs from "node:fs";
+import path from "node:path";
+import { mannWhitneyZ } from "./compare.mjs";
+
+const [dir, ...builds] = process.argv.slice(2);
+const runs = fs
+ .readdirSync(dir)
+ .filter((f) => /^open-.*\.json$/.test(f))
+ .map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf8")))
+ .filter((r) => r.ok && r.run > 0)
+ .map((r) => ({ ...r, ver: r.pinned === false ? `${r.ver}-nopin` : r.ver }));
+const med = (a) => {
+ const s = [...a].sort((x, y) => x - y);
+ const h = s.length >> 1;
+ return s.length % 2 ? s[h] : (s[h - 1] + s[h]) / 2;
+};
+const metrics = {
+ hudReady: (r) => r.hudReadyAt,
+ hudTarget: (r) => r.hudTargetAt,
+ window: (r) => r.phases.windowToNavStart,
+ dcl: (r) => r.phases.domContentLoaded,
+ root: (r) => r.phases.rootRendered,
+ video: (r) => r.phases.videoElement,
+ meta: (r) => r.phases.videoMetadata,
+ frame: (r) => r.phases.firstFrame,
+ total: (r) => r.phases.total,
+};
+const ref = runs.filter((r) => r.ver === builds[0]);
+console.log(
+ `build n ${Object.keys(metrics)
+ .map((k) => k.padStart(9))
+ .join("")} z(total) z(hud)`,
+);
+for (const b of builds) {
+ const g = runs.filter((r) => r.ver === b);
+ if (!g.length) continue;
+ const cells = Object.values(metrics).map((f) => med(g.map(f)).toFixed(0).padStart(9));
+ const z = (f) => mannWhitneyZ(ref.map(f), g.map(f)).toFixed(2).padStart(7);
+ console.log(
+ `${b.padEnd(10)} ${String(g.length).padStart(2)} ${cells.join("")} ${z(metrics.total)} ${z(metrics.hudReady)}`,
+ );
+}
diff --git a/technical-documentation/testing/performance-ab.md b/technical-documentation/testing/performance-ab.md
index 3590b8330..12cf846ac 100644
--- a/technical-documentation/testing/performance-ab.md
+++ b/technical-documentation/testing/performance-ab.md
@@ -7,12 +7,12 @@ log of every comparison run so far. The scripts live in [`scripts/perf-ab/`](../
### 2.0.0 against main `9dc48b5c7`, Windows, 15/10/2026
-**Verdict: main is as smooth and as fast as 2.0.0, at 279 Hz and at 60 Hz, with one small slowdown
-to fix.**
+**Verdict: main is as smooth and as fast as 2.0.0, at 279 Hz and at 60 Hz, except that it takes
+47 ms longer to show the HUD.**
-- **One regression over 5%**: the editor takes 53 ms longer (+5.2%) to show its first frame on
- the heavy project. Starting the app to the HUD takes 31 ms longer (+4.9%). Both are
- significant over 12 and 15 sessions.
+- **One regression over 5%**: from process start to a ready HUD, 625 → 672 ms (+7.5%, p < 0.001,
+ 12 sessions each). The editor-open slowdown first logged here (+53 ms) did not survive a
+ controlled rerun: 1143 → 1136 ms. See [the bisection](#bisecting-the-startup-slowdown).
- **No preview regression established.** The light project is identical. On the heavy project,
late paints are 17% higher at the median but not significantly so (see below).
- **Two improvements.** GIF export is 30 times faster. A recorded screen video keeps a regular
@@ -41,7 +41,7 @@ to fix.**
| # | Finding | Status | Likely cause | Next step |
|---|---|---|---|---|
-| 1 | Editor open +53 ms (1025 → 1078 ms), startup to HUD +31 ms (620 → 651 ms) | Regression, p < 0.05 | More work at launch since 2.0.0: the in-app MCP server (`b81cd3760`, `65444df3e`), the shader effects in the document and the Effects lane (`1b704ff6e`, `74c89035e`), the daily usage ping (`b5ff5a348`). Not bisected. | Bisect editor open on the heavy project |
+| 1 | Startup to a ready HUD +47 ms (625 → 672 ms). Editor open unchanged (1143 → 1136 ms) | Startup: regression, p < 0.001. Editor open: retracted | No single commit. +18 ms before the HUD page starts, which is not main-process JavaScript; +12 ms of CPU in the HUD's first render, spread over a dozen modules | Try a HUD entry of its own, without the editor's code, and measure it |
| 2 | Heavy preview: 87 → 102 paints later than 25 ms per 30 s (+17%) | Within noise, p ≈ 0.2 | Both builds land in a slow mode (paint p95 28.5 ms instead of 22 ms) in about half the sessions: 2.0.0 6 of 12, main 7 of 12. The mode follows the session, not the build. | Find what sets the mode |
| 3 | At 60 Hz the preview paints 51 to 58 frames a second, and the cursor steps backwards 24 to 153 times in 18 s (2.0.0 median 31, main 53) | Pre-existing, both builds; main's count within noise | Not established. First hypothesis: the compositor's own 60 Hz pacing beats against the display's 60 Hz vsync, so a frame composed for one instant shows a vsync early or late | Pace the preview on the display's vsync, measure at 60 Hz |
| 4 | GPU process dedicated memory, one traced session each: 0.8 GB (2.0.0) and 1.4 GB (main) at 11 s of playback, 9.8 GB and 12.8 GB at 2 min | Pre-existing, both builds | Grows linearly with playback, so something per frame is kept: the shared-texture preview path is the first suspect | Trace the shared frames' lifetime |
@@ -92,7 +92,7 @@ on-screen rows are wide because the desktop capture itself misses frames now and
| Private memory, main process (MB) | 634 [633–641] | 624 [623–639] | -1.6% | same |
| Private memory, renderer (MB) | 337 [337–344] | 329 [305–343] | -2.4% | same |
| GPU dedicated memory, GPU process, after 2 min (MB) | 9252 [8989–12847] | 8567 [8009–11063] | -7.4% | within noise |
-| Editor open to first preview frame (ms) | 1025 [991–1101] | 1078 [1025–1244] | +5.2% | **regression** (p < 0.05) |
+| Editor open to first preview frame (ms) | 1025 [991–1101] | 1078 [1025–1244] | +5.2% | not confirmed, see below |
Late paints per session, sorted: 2.0.0 `53 61 67 71 76 84 90 105 106 107 111 113`, main
`54 75 76 83 88 92 111 111 111 112 114 119`. Sessions in the slow mode also use more GPU 3D in
@@ -134,6 +134,43 @@ Sessions again split into a good mode (p95 18 ms) and a bad one (33 ms) in both
residual grows from 6 px to 8 to 10 px and backward steps appear: that is the fidget, and 2.0.0
shows it too.
+#### Bisecting the startup slowdown
+
+The editor-open and startup timings above came from sessions where the editor opened behind
+whatever window was in front. A dedicated bench (`open-bench.mjs`: no playback, every window of the
+app pinned on top from its first frame, phases read from inside each page) reran the two packages,
+12 sessions each, interleaved:
+
+| Metric | 2.0.0 | main | Delta | z |
+|---|---|---|---|---|
+| Process start to HUD navigation (ms) | 449 | 467 | +18 | 2.19 |
+| HUD page: navigation to DOMContentLoaded (ms) | 129 | 134 | +4 | 1.36 |
+| HUD page: navigation to first React render (ms) | 173 | 205 | +32 | 3.84 |
+| Process start to HUD ready (ms) | 625 | 672 | **+47** | 3.81 |
+| Editor open to first preview frame (ms) | 1143 | 1136 | -7 | -0.06 |
+
+Where the 47 ms go:
+
+- **Before the HUD page starts (+18 ms): not JavaScript.** A CPU profile of the main process from
+ its first line to the HUD (`profile-startup.mjs`, `--inspect-brk`) gives the same busy time,
+ 507 ms for 2.0.0 and 510 ms for main.
+- **In the HUD's first render (+12 ms of CPU): many small costs.** Profiles of the HUD renderer
+ mapped through source maps (`profile-hud.mjs`, `attribute-hud.cjs`), 8 each: 194 → 206 ms busy.
+ `LaunchWindow` +2.5 ms, i18next +2.4, the HUD's own toaster (`3a77ef890`) +1.7, zod +1.5,
+ tailwind-merge +1.0, Radix +1.0. The bundle itself barely grew (index chunk 1.33 → 1.37 MB).
+- **No commit owns it.** The HUD-ready time per CI build of main (10 sessions each) moves by
+ steps within the noise: 695 (2.0.0), 755 (`b519f236e`), 708 (`e3bde938a`), 675 (`cb8f89c9d`),
+ 743 (`b668b2c8d`), 782 (`4f4bb997c`), 725 ms (`9dc48b5c7`). The largest step, between
+ `cb8f89c9d` and `b668b2c8d` (first React render, z = 2.80), gave contradictory grafts: on a
+ package built from `b668b2c8d`, its main process and `65444df3e` alone cost 100 to 140 ms, but on
+ a package built from `9dc48b5c7` the same main process started as fast as 2.0.0's (688 ms).
+ Opening the MCP app channel only after the HUD has loaded was built and measured there:
+ 688 → 687 ms, no gain, so it was not kept.
+
+The lever large enough to win the 47 ms back is untested: the HUD evaluates the editor's whole
+bundle (the 1.37 MB index chunk, every zod schema, i18next) to draw a bar of buttons. A HUD entry
+of its own would skip most of it; trimming the dozen small costs above one by one would not.
+
#### Startup (15 sessions each)
| Metric | 2.0.0 | main | Delta | Verdict |
@@ -227,6 +264,10 @@ media, so neither build's recorder decides the input:
analysed and deleted at once: it holds the webcam picture.
5. `node summarize-gui.mjs <outDir>`, `node summarize-record.mjs <scratch>/out/record`, then
`node doc-tables.mjs` on the three summaries prints these tables.
+6. Startup and editor open alone: `open-bench.mjs` (about 10 s a session) and
+ `summarize-open.mjs`. To see where the time goes, `profile-startup.mjs` profiles the main
+ process and `profile-hud.mjs` the HUD renderer from their first line; `attribute-hud.cjs` maps
+ the HUD profiles onto the sources of a `vite build --sourcemap` of the same commit.
Keep the desktop to the bench while it runs. A window a background process opens lands behind
the foreground one, which is why `topmost.ps1` pins the editor and the pattern player. A covered