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
2 changes: 1 addition & 1 deletion ts/bench/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions ts/bench/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayBuffer>; task: Uint8Array<ArrayBuffer> }> {
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}`);
Expand All @@ -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<Uint8Array> {
async function bundle(entry: string, plugins: BunPlugin[] = []): Promise<Uint8Array<ArrayBuffer>> {
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<Uint8Array> {
async function read(path: string): Promise<Uint8Array<ArrayBuffer>> {
return new Uint8Array(await Bun.file(path).arrayBuffer());
}

Expand Down Expand Up @@ -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<ArrayBuffer>[] }) {
const sum = (f: (b: Uint8Array<ArrayBuffer>) => 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) };
}

Expand Down
12 changes: 6 additions & 6 deletions ts/bench/moq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<number, { version: number; encode: (dst: ArrayBuffer, v: number) => 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<number, { version: StreamVersion; encode: (dst: ArrayBuffer, v: number) => Uint8Array }> = {
0: { version: Lite.DRAFT_06, encode: Varint.encodeTo },
1: { version: Lite.DRAFT_07, encode: Varint.encodeLeadingOnesTo },
};
const LITE: Record<number, Version> = { 5: Lite.DRAFT_06, 6: Lite.DRAFT_07 };

Expand Down
17 changes: 17 additions & 0 deletions ts/bench/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
6 changes: 5 additions & 1 deletion ts/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down