Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions quest/m2/ts-bench-chrome-timeout.md

This file was deleted.

3 changes: 2 additions & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions ts/bench/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> {
progress(`loading ${name}`);
const url = new URL(name, import.meta.url);
if (url.protocol === "file:") {
const fs = await import("node:fs/promises");
Expand All @@ -30,6 +35,7 @@ async function load(name: string): Promise<Uint8Array> {

/** A bundled route, whose `run` (or `exported`) returns its output as `view` reads it. */
async function route<T>(name: string, file: string, view: (out: T) => Uint8Array, exported = "run"): Promise<Route> {
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)) };
}
Expand Down Expand Up @@ -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.
Expand All @@ -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 };
}

Expand Down
28 changes: 25 additions & 3 deletions ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,29 @@ async function script(command: string): Promise<Engine> {
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<Engine> {
const { promise, resolve, reject } = Promise.withResolvers<Engine>();
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,
async fetch(req) {
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();
}
Expand All @@ -235,17 +248,26 @@ async function chrome(): Promise<Engine> {
},
});
// 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);
}
}

Expand Down
10 changes: 9 additions & 1 deletion ts/bench/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,12 +51,14 @@ export function hex(bytes: Uint8Array): string {
}

export async function measure(wasm: Wasm, routes: Route[], workloads: Workload[], options: Options): Promise<Timing[]> {
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}`);
Expand All @@ -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;
Expand All @@ -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);
Expand Down