diff --git a/quest/m2/README.md b/quest/m2/README.md index abe1b534c..826e81689 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -19,7 +19,6 @@ is not a goal, and types must be solid at the API boundary ([design](/ts/README. ## Required - [MIR lowering spike](/quest/m2/ts-mir-spike.md) - the whole lowering ported to MIR on two fixtures decides whether the translator moves -- [Fail the TS benchmark when Chrome hangs](/quest/m2/ts-bench-chrome-timeout.md) - a hung renderer fails the run loudly - [Stable names](/quest/m2/ts-stable-names.md) - generic instances and trait-impl methods always carry their suffix, so callers' names never shift - [Diverging calls through closures and generics](/quest/m2/ts-never-edges.md) - closures returning `!` and generics at `!` pass `tsc` - [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split diff --git a/quest/m2/ts-bench-chrome-timeout.md b/quest/m2/ts-bench-chrome-timeout.md deleted file mode 100644 index e9becc089..000000000 --- a/quest/m2/ts-bench-chrome-timeout.md +++ /dev/null @@ -1,15 +0,0 @@ -# [XS] Fail the TS benchmark when Chrome hangs - -## Goal - -A hung Chromium renderer fails `just ts bench` loudly, naming the engine and what -it was running, instead of stalling the run until the job times out. - -## Plan - -Seen twice overnight, first in [#15](https://github.com/kixelated/rust2/pull/15): -the renderer sat idle and never posted a result, while a rerun passed. The limit -only turns a hang into a failure; it never retries, and if the hang's cause shows -up, fix it at the source. - -Public API: none. diff --git a/ts/README.md b/ts/README.md index 5c8be3395..d5cd21984 100644 --- a/ts/README.md +++ b/ts/README.md @@ -249,7 +249,8 @@ native Rust's first. The JS routes run in Node, Bun, and Chrome, with the WASM r copies across the boundary timed apart from its compute, and an A/A copy of the translation to show the noise. The report records the host and engine versions, and each route's bundle size raw and gzipped. It sets no threshold; the nightly workflow -runs it and posts the report to its summary. +runs it and posts the report to its summary. Chrome's page reports each step it starts, +and one that runs 30 s fails the run, naming it. The task fixture's `bench::run` times an async handoff: a value sent to a task parked on its channel, read through 1, 4, or 16 async functions. Its translation runs polled at diff --git a/ts/bench/entry.ts b/ts/bench/entry.ts index a0253c351..34f982e88 100644 --- a/ts/bench/entry.ts +++ b/ts/bench/entry.ts @@ -19,7 +19,12 @@ export interface Config { const browser = typeof window !== "undefined"; +// Tells Chrome's `main.ts`, which fails the run when it stops hearing, what runs next. A +// beacon leaves no response for the page to handle between samples. +const progress = browser ? (step: string) => void navigator.sendBeacon("/progress", step) : () => {}; + async function load(name: string): Promise { + progress(`loading ${name}`); const url = new URL(name, import.meta.url); if (url.protocol === "file:") { const fs = await import("node:fs/promises"); @@ -30,6 +35,7 @@ async function load(name: string): Promise { /** A bundled route, whose `run` (or `exported`) returns its output as `view` reads it. */ async function route(name: string, file: string, view: (out: T) => Uint8Array, exported = "run"): Promise { + progress(`loading ${file}`); const run = (await import(new URL(file, import.meta.url).href))[exported]; return { name, run, output: async (input) => view(await run(input)) }; } @@ -58,7 +64,7 @@ async function main() { await route("A/A", "translated-aa.js", translated), ]; if (config.route) routes.push(await route(config.route, "route.js", translated)); - const timings = await measure(wasm, routes, workloads, config); + const timings = await measure(wasm, routes, workloads, { ...config, progress }); // The async handoff: the translation polled at once by a one-task executor, as the // native and WASM routes are, and on the runtime's microtask executor. @@ -71,7 +77,7 @@ async function main() { ]; if (config.route) handoffRoutes.push(await route(config.route, "task-route.js", bytes)); const taskWasm = new Wasm(await WebAssembly.compile(await load("task.wasm"))); - timings.push(...(await measure(taskWasm, handoffRoutes, parse(config.handoffs), config))); + timings.push(...(await measure(taskWasm, handoffRoutes, parse(config.handoffs), { ...config, progress }))); return { engine: engine(), timings }; } diff --git a/ts/bench/main.ts b/ts/bench/main.ts index cf0d1401b..391fe7e57 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -214,8 +214,18 @@ async function script(command: string): Promise { return JSON.parse(result); } +// The page's longest step is a timing round, one or two samples of each of a few hundred +// bodies: a few seconds at the default 10 ms, so this long a silence is a hang. +const stallMs = Math.max(30_000, 1000 * sampleMs); + async function chrome(): Promise { const { promise, resolve, reject } = Promise.withResolvers(); + let step = "loading the page"; + let stall: Timer | undefined; + const progress = () => { + clearTimeout(stall); + stall = setTimeout(() => fail(`stalled for ${stallMs / 1000} s while ${step}`), stallMs); + }; const server = Bun.serve({ hostname: "127.0.0.1", port: 0, @@ -223,7 +233,10 @@ async function chrome(): Promise { const path = new URL(req.url).pathname; if (req.method === "POST") { const body = await req.text(); - if (path === "/result") resolve(JSON.parse(body)); + if (path === "/progress") { + step = body; + progress(); + } else if (path === "/result") resolve(JSON.parse(body)); else reject(new Error(`Chrome: ${body}`)); return new Response(); } @@ -235,17 +248,26 @@ async function chrome(): Promise { }, }); // No sandbox: the page is our own, and CI runners refuse the user namespaces it needs. + // Its stderr goes to a file, which a child left behind cannot hold open as it can a pipe. + const log = join(out, "chrome.log"); const proc = Bun.spawn( ["chromium", "--headless", "--no-sandbox", "--no-first-run", `--user-data-dir=${join(out, "chrome")}`, server.url.href], - { stdout: "ignore", stderr: "pipe" }, + { stdout: "ignore", stderr: Bun.file(log) }, ); - proc.exited.then(async (code) => reject(new Error(`Chrome exited with ${code}:\n${await new Response(proc.stderr).text()}`))); + const fail = async (why: string) => { + proc.kill("SIGKILL"); + reject(new Error(`Chrome ${why}:\n${await Bun.file(log).text()}`)); + }; + proc.exited.then((code) => fail(`exited with ${code}`)); + progress(); try { // The user agent reduces the version to its major number. return { ...(await promise), engine: text(["chromium", "--version"]) }; } finally { proc.kill(); + // Closing the server first drops any progress still in flight. await server.stop(true); + clearTimeout(stall); } } diff --git a/ts/bench/time.ts b/ts/bench/time.ts index 4d8638355..138711da0 100644 --- a/ts/bench/time.ts +++ b/ts/bench/time.ts @@ -18,6 +18,8 @@ export interface Options { sampleMs: number; /** Samples per timed body, interleaved with every other body's. */ rounds: number; + /** Told what runs next, between timed regions. */ + progress(step: string): void; } export interface Timing { @@ -49,12 +51,14 @@ export function hex(bytes: Uint8Array): string { } export async function measure(wasm: Wasm, routes: Route[], workloads: Workload[], options: Options): Promise { + const { progress } = options; const bodies: Body[] = []; for (const workload of workloads) { const { input } = workload; const want = wasm.call(input); if (want === "panic") throw new Error(`${workload.name} ${workload.op} panics in native Rust`); for (const route of routes) { + progress(`checking ${route.name} on ${workload.name} ${workload.op}`); const got = hex(await route.output(input)); if (got !== hex(want)) { throw new Error(`${route.name} differs from native Rust on ${workload.name} ${workload.op}:\nnative ${hex(want)}\ngot ${got}`); @@ -75,6 +79,7 @@ export async function measure(wasm: Wasm, routes: Route[], workloads: Workload[] // Find each body's calls per sample, which also warms it up. for (const body of bodies) { + progress(`warming up ${body.route} on ${body.workload.name} ${body.workload.op}`); body.setup?.(); const first = body.call(); body.async = first instanceof Promise; @@ -83,8 +88,11 @@ export async function measure(wasm: Wasm, routes: Route[], workloads: Workload[] } // Interleave the bodies in each round, starting each round one later, so drift and - // position spread over all of them. + // position spread over all of them. Progress is per round: a report per sample slows + // Chrome's samples by about 10%. + const span = `${workloads[0].name} through ${workloads[workloads.length - 1].name}`; for (let round = 0; round < options.rounds; round++) { + progress(`timing round ${round + 1} of ${options.rounds} on ${span}`); for (let i = 0; i < bodies.length; i++) { const body = bodies[(i + round) % bodies.length]; const ms = await sample(body);