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
5 changes: 5 additions & 0 deletions js/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ catalog-bench:
bun install --frozen-lockfile
bun publish/bench/catalog.ts

# Sweep QMux record decoding over record size x frames per record.
qmux-record-bench:
bun install --frozen-lockfile
bun qmux/bench/record.ts

# Sweep CMAF fragment decode over mdat bytes x declared sample count, including a hostile count.
cmaf-bench:
bun install --frozen-lockfile
Expand Down
51 changes: 51 additions & 0 deletions js/qmux/bench/record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Sweep QMux record decoding over record size x frames per record. Each frame is a STREAM frame
* whose payload fills its share of the record, an empty STREAM frame, or a MAX_DATA control frame.
* Decoding must cost what the record holds, so time per record stays flat as frames multiply.
*/

import * as Frame from "../src/frame.ts";
import * as Stream from "../src/stream.ts";
import { VarInt } from "../src/varint.ts";

const runs = 200;
const id = new Stream.Id(VarInt.from(4n));

/** `count` frames that `make` builds, concatenated into one record body. */
function record(count: number, make: () => Frame.Any): Uint8Array {
const parts = Array.from({ length: count }, () => Frame.encode(make(), "qmux-01"));
const out = new Uint8Array(parts.reduce((sum, part) => sum + part.byteLength, 0));
let offset = 0;
for (const part of parts) {
out.set(part, offset);
offset += part.byteLength;
}
return out;
}

function time(body: Uint8Array, count: number): number {
const start = performance.now();
for (let run = 0; run < runs; run++) {
const frames = Frame.decodeRecord(body);
if (frames.length !== count) throw new Error(`decoded ${frames.length} of ${count} frames`);
}
return ((performance.now() - start) * 1000) / runs;
}

console.log("record_bytes,kind,frames,us_per_record");
for (const size of [1_024, 16_380]) {
for (const count of [1, 16, 256]) {
// Four bytes of STREAM header (type, id, two-byte length) per frame.
const payload = new Uint8Array(Math.max(0, Math.floor(size / count) - 4));
const body = record(count, () => ({ type: "stream", id, data: payload, fin: false }));
console.log(`${body.byteLength},stream,${count},${time(body, count).toFixed(1)}`);
}
for (const [kind, make] of [
["empty", (): Frame.Any => ({ type: "stream", id, data: new Uint8Array(0), fin: false })],
["max_data", (): Frame.Any => ({ type: "max_data", max: 1n << 20n })],
] as const) {
const one = Frame.encode(make(), "qmux-01").byteLength;
const count = Math.floor(size / one);
console.log(`${size},${kind},${count},${time(record(count, make), count).toFixed(1)}`);
}
}
54 changes: 54 additions & 0 deletions js/qmux/src/frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,57 @@ describe("maxStreamPayload", () => {
// stream ID come out of the budget.
});
});

describe("record decoding cost", () => {
/** The bytes `Uint8Array.prototype.slice` copies while `run` runs. */
function copied(run: () => void): number {
const slice = Uint8Array.prototype.slice;
let bytes = 0;
Uint8Array.prototype.slice = function (this: Uint8Array, ...args: [number?, number?]) {
const out = slice.apply(this, args);
bytes += out.byteLength;
return out;
} as typeof slice;
try {
run();
} finally {
Uint8Array.prototype.slice = slice;
}
return bytes;
}

/** A record of `count` copies of one encoded frame. */
function record(frame: Frame.Any, count: number): Uint8Array {
const one = Frame.encode(frame, "qmux-01");
const out = new Uint8Array(one.byteLength * count);
for (let i = 0; i < count; i++) out.set(one, i * one.byteLength);
return out;
}

const id = new Stream.Id(VarInt.from(4n));

test("decoding copies each payload once, however many frames follow it", () => {
// The largest default record of empty STREAM frames: each frame's remainder used to be
// copied, about 45 MB for this one record.
const empty = record({ type: "stream", id, data: new Uint8Array(0), fin: false }, 5_460);
let frames: Frame.Any[] = [];
expect(copied(() => (frames = Frame.decodeRecord(empty)))).toBe(0);
expect(frames.length).toBe(5_460);

const payloads = record({ type: "stream", id, data: new Uint8Array(32).fill(7), fin: false }, 400);
expect(copied(() => Frame.decodeRecord(payloads))).toBe(400 * 32);
});

test("a retained payload does not alias its record", () => {
const body = record({ type: "stream", id, data: new Uint8Array([1]), fin: false }, 2);
const [first] = Frame.decodeRecord(body);
if (first.type !== "stream") throw new Error("expected a stream frame");
expect(first.data.buffer).not.toBe(body.buffer);
expect(first.data.buffer.byteLength).toBe(1);
});

test("a truncated length is still refused", () => {
const one = Frame.encode({ type: "stream", id, data: new Uint8Array(8), fin: false }, "qmux-01");
expect(() => Frame.decodeRecord(one.subarray(0, one.byteLength - 1))).toThrow("frame truncated");
});
});
Loading
Loading