import { fileURLToPath } from "node:url"; import { listRunningDevServers } from "./devServerMarker"; import { localRequest } from "./localhostHttp"; import { checkBudget, loadBudget, summarizeRecord } from "./perf-budget"; import { REPORT_ENDPOINT_PATH, type ReportRouteError } from "../packages/core/src/devtools/reportEndpoint"; import type { PerfReport } from "../packages/core/src/devtools/report"; /** * Read the report a game already open in a browser is producing, and gate it against * that game's budget. No browser launch, no CDP, no second process: the page * pushes to the dev server's route about once a second, and this fetches the newest. * * bun run perf live [--wait ms] [--no-budget] * * The record printed to stdout is exactly what `bun run perf ` prints. Notes * and the budget verdict go to stderr, so the JSON can be piped. */ const repoRoot = fileURLToPath(new URL("..", import.meta.url)); const DEFAULT_WAIT_MS = 1_500; export interface DebugArgs { game: string; waitMs: number; budget: boolean; } const USAGE = `usage: bun run perf live [--wait ms] [--no-budget] game id already open in a browser (for example harbor-heat) --wait how long to wait for a report when none has arrived (default ${DEFAULT_WAIT_MS} ms) --no-budget print the report without checking the game's budget`; export function parseDebugArgs(argv: readonly string[]): DebugArgs | "help" { if (argv.includes("--help") || argv.includes("-h")) return "help"; let game: string | undefined; let waitMs = DEFAULT_WAIT_MS; let budget = true; for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]!; if (arg === "--no-budget") { budget = false; continue; } if (arg === "--wait") { const value = argv[index + 1]; if (value === undefined) throw new Error("--wait needs a millisecond value"); index += 1; const parsed = Number(value); if (!Number.isFinite(parsed) || parsed < 0) throw new Error(`--wait must be 0 or more (got ${value})`); waitMs = parsed; continue; } if (arg.startsWith("-")) throw new Error(`unknown flag ${arg}`); if (game !== undefined) throw new Error(`one game per run (got ${game} and ${arg})`); game = arg; } if (game === undefined) throw new Error("a game id is required"); return { game, waitMs, budget }; } /** Fetch the newest in-page report for `game` from a running dev server. */ export async function fetchReport(base: string, game: string, waitMs: number): Promise { const url = new URL(REPORT_ENDPOINT_PATH, base); url.searchParams.set("game", game); if (waitMs > 0) url.searchParams.set("wait", String(waitMs)); const timeoutMs = Math.max(5_000, waitMs + 5_000); const response = await localRequest(url, { timeoutMs }); let body: unknown; try { body = JSON.parse(response.body) as unknown; } catch { return { error: `the report route answered ${response.status} with a body that is not JSON` }; } if (response.status < 200 || response.status >= 300) { const routeError = body as ReportRouteError; return { error: typeof routeError?.error === "string" ? routeError.error : `report route answered ${response.status}` }; } if (typeof body !== "object" || body === null || !("frame" in body) || !("render" in body)) { return { error: "the report route answered something that is not a perf record" }; } return body as PerfReport; } /** The URL a person opens to play `game` on a given dev server. */ export function gameUrl(server: { base: string; kind: "website" | "runner" }, game: string): string { return server.kind === "website" ? `${server.base}/games/${game}` : `${server.base}/?game=${game}`; } /** * Pick the server holding a report: the website dev server first, then the runner. * A wait only ever applies to one server, so a missing report fails in about the time * one `--wait` costs rather than doubling it per server. */ export async function findReport( servers: readonly { base: string; kind: "website" | "runner" }[], game: string, waitMs: number, ): Promise<{ server: (typeof servers)[number]; report: PerfReport } | { error: string }> { let lastError = `no report for "${game}" yet`; for (const server of servers) { const report = await fetchReport(server.base, game, 0); if (!("error" in report)) return { server, report }; lastError = report.error; } const first = servers[0]; if (first !== undefined && waitMs > 0) { const report = await fetchReport(first.base, game, waitMs); if (!("error" in report)) return { server: first, report }; lastError = report.error; } return { error: lastError }; } async function main(): Promise { const parsed = parseDebugArgs(process.argv.slice(2)); if (parsed === "help") { console.log(USAGE); return; } const { game, waitMs, budget } = parsed; const servers = await listRunningDevServers(repoRoot); if (servers.length === 0) { console.error( `debug: no dev server is running for this checkout — run \`bun dev\`, open http://localhost:3000/games/${game}, then retry`, ); process.exit(1); } const startedAt = Date.now(); const found = await findReport(servers, game, waitMs); const fetchMs = Date.now() - startedAt; if ("error" in found) { const open = servers.map((server) => gameUrl(server, game)).join(" or "); console.error(`debug: ${found.error}`); console.error(`debug: open ${open}, play for a second, then retry`); process.exit(1); } const { server, report } = found; console.log(JSON.stringify(report, null, 2)); const ageMs = Date.now() - report.at; console.error( `debug: ${game} report from ${server.base} in ${fetchMs} ms (report ${Math.max(0, Math.round(ageMs))} ms old, ` + `${report.frame.samples} frames over ${report.seconds}s, ${report.gpu})`, ); if (report.telemetry !== null && !report.telemetry.warmedUp) { console.error("debug: the sampler is still inside its warmup, so link/framebuffer counts are not yet meaningful"); } if (report.logs !== null && report.logs.severe.length > 0) { const latest = report.logs.severe[report.logs.severe.length - 1]!; console.error(`debug: ${report.logs.severe.length} warning/error line(s); latest [${latest.level}:${latest.system}] ${latest.message}`); } if (!budget) return; const limit = loadBudget(game, repoRoot); const violations = checkBudget(report, limit.budget); if (violations.length === 0) { console.error(`debug: ${summarizeRecord(report, limit.budget)} (budget ${limit.source})`); return; } console.error(`debug: ${game} is over budget ${limit.source}`); for (const violation of violations) console.error(` ${violation.message}`); process.exit(1); } if (import.meta.main) { await main(); }