diff --git a/ts/bench/entry.ts b/ts/bench/entry.ts index d443b1c60..604367bce 100644 --- a/ts/bench/entry.ts +++ b/ts/bench/entry.ts @@ -20,7 +20,7 @@ export interface Config { } // A page has a `window` and its workers a `WorkerGlobalScope`; Node and Bun have neither. -const browser = typeof window !== "undefined" || typeof WorkerGlobalScope !== "undefined"; +const browser = typeof window !== "undefined" || "WorkerGlobalScope" in globalThis; const threads = browser ? undefined : await import("node:worker_threads"); const worker = browser ? typeof window === "undefined" : !threads?.isMainThread; diff --git a/ts/bench/main.ts b/ts/bench/main.ts index 8aaace254..bffa9378c 100644 --- a/ts/bench/main.ts +++ b/ts/bench/main.ts @@ -60,7 +60,7 @@ const handoffRoutes = ["native", "wasm", "hand-written", "translated", "spawned" const extra = args.route === undefined ? undefined : await extraRoute(args.route); /** `--route`'s name, and the bundles of the translations in its directory, which must both be there. */ -async function extraRoute(arg: string): Promise<{ name: string; codec: Uint8Array; task: Uint8Array }> { +async function extraRoute(arg: string): Promise<{ name: string; codec: Uint8Array; task: Uint8Array }> { const at = arg.indexOf("="); const name = arg.slice(0, at); if (at < 1 || at === arg.length - 1) throw new Error(`--route takes name=dir, not ${arg}`); @@ -83,13 +83,13 @@ const moq: BunPlugin = { }; /** `entry` bundled and minified, as a consumer ships it: each route is timed and sized as this. */ -async function bundle(entry: string, plugins: BunPlugin[] = []): Promise { +async function bundle(entry: string, plugins: BunPlugin[] = []): Promise> { const result = await Bun.build({ entrypoints: [entry], target: "browser", external: ["node:*"], minify: true, plugins }); if (result.outputs.length !== 1) throw new Error(`bundling ${entry} made ${result.outputs.length} files`); return new Uint8Array(await result.outputs[0].arrayBuffer()); } -async function read(path: string): Promise { +async function read(path: string): Promise> { return new Uint8Array(await Bun.file(path).arrayBuffer()); } @@ -177,8 +177,8 @@ const taskSizes = [ ...(extra ? [{ route: extra.name, files: [extra.task] }] : []), ].map(measureSize); -function measureSize({ route, files }: { route: string; files: Uint8Array[] }) { - const sum = (f: (b: Uint8Array) => number) => files.reduce((n, b) => n + f(b), 0); +function measureSize({ route, files }: { route: string; files: Uint8Array[] }) { + const sum = (f: (b: Uint8Array) => number) => files.reduce((n, b) => n + f(b), 0); return { route, raw: sum((b) => b.byteLength), gzip: sum((b) => Bun.gzipSync(b, { level: 9 }).byteLength) }; } diff --git a/ts/bench/moq.ts b/ts/bench/moq.ts index b55bbea54..0e8d3caac 100644 --- a/ts/bench/moq.ts +++ b/ts/bench/moq.ts @@ -7,10 +7,9 @@ // // `moq/` imports resolve to the moq checkout when `main.ts` bundles this file. -import { Version as Ietf } from "moq/js/net/src/ietf/version.ts"; import { Group } from "moq/js/net/src/lite/group.ts"; import { Version as Lite } from "moq/js/net/src/lite/version.ts"; -import { Cursor, Reader, Writer } from "moq/js/net/src/stream.ts"; +import { Cursor, Reader, type StreamVersion, Writer } from "moq/js/net/src/stream.ts"; import * as Varint from "moq/js/net/src/varint.ts"; import { iterate } from "./iter.ts"; @@ -21,10 +20,11 @@ import { interleave, stamp } from "./timestamp.ts"; type Version = (typeof Lite)[keyof typeof Lite]; // bench.rs reads a selector through the harness's `form` and `lite_version`; these are the -// ones the workloads use. -const FORMS: Record Uint8Array }> = { - 0: { version: Ietf.DRAFT_16, encode: Varint.encodeTo }, - 1: { version: Ietf.DRAFT_17, encode: Varint.encodeLeadingOnesTo }, +// ones the workloads use. A form is the varint encoding of a version moq still speaks: +// Lite 06 is the last with QUIC varints, and Lite 07 the first with leading ones. +const FORMS: Record Uint8Array }> = { + 0: { version: Lite.DRAFT_06, encode: Varint.encodeTo }, + 1: { version: Lite.DRAFT_07, encode: Varint.encodeLeadingOnesTo }, }; const LITE: Record = { 5: Lite.DRAFT_06, 6: Lite.DRAFT_07 }; diff --git a/ts/bench/tsconfig.json b/ts/bench/tsconfig.json new file mode 100644 index 000000000..0085b37b5 --- /dev/null +++ b/ts/bench/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "strict": true, + "noEmit": true, + "target": "esnext", + "lib": ["esnext", "dom"], + "module": "esnext", + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "skipLibCheck": true, + "paths": { "moq/*": ["../target/moq/*"] }, + "typeRoots": ["../target/moq/node_modules/@types"], + "types": ["bun"] + }, + "include": ["*.ts"] +} diff --git a/ts/justfile b/ts/justfile index 76b087b14..6eb851515 100644 --- a/ts/justfile +++ b/ts/justfile @@ -14,13 +14,17 @@ moq_rev := "30f25c04e6d0288d87893dc609ba582518be2cf6" default: @just --list -# Lint and format-check the driver and its fixtures. +# Lint and format-check the driver, its fixtures, and the bench. check: cargo clippy --locked --all-targets -- -D warnings cargo fmt --all --check cargo shear cargo sort --workspace --check --no-format tsc -p runtime + # The bench against moq's TypeScript at the pin, so a pin that drops what it uses fails here. + just moq-pin + cd target/moq && bun install --frozen-lockfile --ignore-scripts --filter @moq/net --silent + tsc -p bench # Run the snapshot and differential tests. test *args: