// Measure Theseus launches. Runs N traced starts of the dev tree against a
// throwaway profile and prints one row per run, so a change can be checked
// against the previous numbers.
//
// node scripts/boot-trace/run.mjs [--runs 3] [--seconds 25] [--fresh] [--profile
]
//
// --fresh start from an empty profile (the first run then also measures
// first-launch work: seeding the bundled add-ons, filter builds)
// --profile profile dir to use (default: /theseus-boot-trace)
//
// The profile never touches the real one (THESEUS_USER_DATA), and update
// checks are off (THESEUS_NO_UPDATE_CHECK) so a dev tree that is older than
// the live release can't offer — let alone install — an update.
import { spawn } from "node:child_process";
import { mkdirSync, rmSync, readFileSync, openSync, closeSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
const here = path.dirname(fileURLToPath(import.meta.url));
const APP = path.resolve(here, "..", "..");
const args = process.argv.slice(2);
const opt = (k, d) => { const i = args.indexOf(k); return i >= 0 && args[i + 1] ? args[i + 1] : d; };
const RUNS = Number(opt("--runs", 3));
const SECONDS = Number(opt("--seconds", 25));
const PROFILE = path.resolve(opt("--profile", path.join(tmpdir(), "theseus-boot-trace")));
const ELECTRON = path.join(APP, "node_modules", "electron", "dist", process.platform === "win32" ? "electron.exe" : "electron");
if (!existsSync(ELECTRON)) { console.error("electron binary not found:", ELECTRON); process.exit(1); }
if (args.includes("--fresh")) rmSync(PROFILE, { recursive: true, force: true });
mkdirSync(PROFILE, { recursive: true });
const traceDir = path.join(PROFILE, "..", path.basename(PROFILE) + "-traces");
mkdirSync(traceDir, { recursive: true });
function runOnce(i) {
const out = path.join(traceDir, `run${i}.json`);
const log = openSync(path.join(traceDir, `run${i}.log`), "w");
return new Promise((resolve) => {
// Output to a file, not inherited: an Electron child holding the parent's
// pipes keeps a shell pipeline open after it exits.
const p = spawn(ELECTRON, [path.join(here, "trace-main.js")], {
cwd: APP, stdio: ["ignore", log, log], windowsHide: false,
env: { ...process.env, THESEUS_USER_DATA: PROFILE, THESEUS_NO_UPDATE_CHECK: "1", BOOT_TRACE_OUT: out, BOOT_TRACE_MS: String(SECONDS * 1000) },
});
const kill = setTimeout(() => { try { p.kill(); } catch {} }, SECONDS * 1000 + 30000);
p.on("exit", () => { clearTimeout(kill); closeSync(log); resolve(out); });
});
}
function summarise(file) {
const ev = JSON.parse(readFileSync(file, "utf8"));
const at = (re) => (ev.find((e) => re.test(e.label)) || {}).t ?? null;
const metric = (label) => ev.find((e) => e.kind === "metrics" && e.label === label) || {};
return {
ready: at(/^ready$/),
toolbar: at(/loaded #\d+ file:chrome\.html/),
firstPage: at(/loaded #\d+ file:home\.html/),
blocks6s: ev.filter((e) => e.kind === "block" && e.endedAt < 6000).reduce((a, e) => a + e.ms, 0),
addons: ev.filter((e) => e.kind === "addon").reduce((a, e) => a + e.ms, 0),
overlaysLoaded: ev.filter((e) => e.kind === "wc" && /loaded #\d+ file:(popover|engine-picker|downloads|address-picker|pw-fill|link-status|approval|js-dialog)\.html/.test(e.label)).length,
fetches6s: ev.filter((e) => e.kind === "fetch" && e.start < 6000).length,
proc15: metric("15s").processes ?? null,
mem15: metric("15s").privateMB ?? null,
failed: ev.filter((e) => e.kind === "fail").map((e) => e.label),
};
}
const rows = [];
for (let i = 1; i <= RUNS; i++) {
process.stdout.write(`run ${i}/${RUNS}… `);
const file = await runOnce(i);
if (!existsSync(file)) { console.log("no trace written (see " + file.replace(/json$/, "log") + ")"); continue; }
const r = summarise(file); rows.push(r);
console.log(r.failed.length ? `INVALID — ${r.failed.length} page load(s) failed: ${r.failed[0]}` : "done");
}
const cols = [["ready", "ready"], ["toolbar", "toolbar"], ["firstPage", "1st page"], ["blocks6s", "blocked<6s"], ["addons", "addons"], ["overlaysLoaded", "overlays"], ["fetches6s", "fetch<6s"], ["proc15", "procs@15s"], ["mem15", "MB@15s"]];
console.log("\n" + ["run", ...cols.map((c) => c[1])].map((h) => h.padStart(11)).join(""));
rows.forEach((r, i) => console.log((r.failed.length ? "!" : " ") + [String(i + 1), ...cols.map(([k]) => String(r[k] ?? "—"))].map((v) => v.padStart(11)).join("")));
console.log(`\ntimes in ms since process start; MB = private memory of all Theseus processes. Traces: ${traceDir}`);