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
6 changes: 6 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ jobs:
if: ${{ !cancelled() }}
run: nix develop --command bun js/net/bench/frames.ts

# Fails if publishing a group costs more as the track retains more groups,
# which means the latency guard or the cache went back to scanning them all.
- name: JS track retention benchmark
if: ${{ !cancelled() }}
run: nix develop --command bun js/net/bench/track.ts

# Runs each moq-net Criterion routine once, so a bench that panics (an
# unparsable version, a shape that deadlocks) fails here instead of on the
# next manual run. Timing is not compared; nextest never runs a
Expand Down
108 changes: 108 additions & 0 deletions js/net/bench/track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/** Sweep retained groups and subscribers for one group published through a track and received. */
import { type Consumer as GroupConsumer, Producer as GroupProducer } from "../src/group.ts";
import { Milli, Timestamp } from "../src/time.ts";
import { Producer, type Subscriber } from "../src/track.ts";

const retainedCounts = [25, 100, 400, 1500];
const subscriberCounts = [1, 4, 16];
// Open groups each subscriber is still reading, so their latency guards re-evaluate on every arrival.
const heldCounts = [0, 16];
const groups = 100;
const reps = 7;
// Publishing a group must not cost more as the retained window grows, or a guard or cache pass
// has gone back to scanning every retained group. The sweep spans 60x, so a scan lands well past
// this; the margin is loose because the machine is noisy.
const maxSlope = 5;
const payload = new Uint8Array(80);
let checksum = 0;

// A clock that advances a millisecond per group, so a window of N ms retains N groups and every
// publish ages the oldest one out. Timing reads the real clock.
const now = performance.now.bind(performance);
let clock = 0;
performance.now = () => clock;

// Let every woken reader re-evaluate its guard before the next group.
const flush = () => new Promise<void>((resolve) => setImmediate(resolve));

function receive(subscribers: Subscriber[]): GroupConsumer[] {
const received: GroupConsumer[] = [];
for (const subscriber of subscribers) {
const group = subscriber.tryRecvGroup();
if (!group) throw new Error("group was not delivered");
const frame = group.tryReadFrame();
if (!frame) throw new Error("frame was not delivered");
checksum += frame.payload.byteLength;
received.push(group);
}
return received;
}

async function measure(retained: number, subscriberCount: number, held: number): Promise<number> {
// Held groups would age out too, so those rows keep everything instead.
const window = Milli(held > 0 ? 3_600_000 : retained);
const producer = new Producer("bench").accept({ maxAge: window });
const subscribers = Array.from({ length: subscriberCount }, () => producer.subscribe({ maxAge: window }));
let sequence = 0;
// Inserted by sequence, the way the wire hands a subscribed track its groups.
const publish = (close: boolean) => {
clock++;
const group = new GroupProducer(sequence);
producer.writeGroup(group);
group.writeFrame({ payload, timestamp: Timestamp.fromMillis(sequence++) });
if (close) group.close();
return group;
};

// The held groups stay open, each with a reader parked on its next frame.
const open: GroupProducer[] = [];
const reads: Promise<unknown>[] = [];
for (let index = 0; index < retained; index++) {
const isHeld = index < held;
const group = publish(!isHeld);
const received = receive(subscribers);
if (isHeld) {
open.push(group);
for (const consumer of received) reads.push(consumer.readFrame());
}
}
await flush();

const start = now();
for (let index = 0; index < groups; index++) {
publish(true);
receive(subscribers);
await flush();
}
const elapsed = now() - start;

for (const group of open) group.close();
await Promise.all(reads);
producer.close();
return (elapsed * 1000) / groups;
}

// Warm the JIT so the first row isn't the slowest.
await measure(100, 1, 0);

console.log("retained,subscribers,held,publish_us");
for (const held of heldCounts) {
for (const subscriberCount of subscriberCounts) {
let baseline: number | undefined;
for (const retained of retainedCounts) {
const samples: number[] = [];
for (let rep = 0; rep < reps; rep++) samples.push(await measure(retained, subscriberCount, held));
// The fastest run is the one least disturbed by the rest of the machine.
const us = Math.min(...samples);
console.log(`${retained},${subscriberCount},${held},${us.toFixed(1)}`);

baseline ??= us;
if (us > baseline * maxSlope) {
throw new Error(
`${retained} retained groups cost ${us.toFixed(1)} us/group, over ${maxSlope}x ${baseline.toFixed(1)}`,
);
}
}
}
}
if (checksum === 0) throw new Error("benchmark did no work");
53 changes: 53 additions & 0 deletions js/net/src/track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,29 @@ test("an unstamped immediate successor leaves reach unbounded", async () => {
expect((await track.recvGroup())?.sequence).toBe(2);
});

// Groups can arrive out of sequence order; the immediate successor bounds reach even when it
// arrived last.
test("a late-arriving successor bounds a group's reach", async () => {
const producer = new TrackProducer("test").accept({ maxAge: Milli(5000) });
const track = producer.subscribe({ maxAge: Milli(500) });

for (const [sequence, ms] of [
[0, 0],
[2, 3000],
[1, 200],
]) {
const group = new GroupProducer(sequence);
group.writeFrame({ payload: enc.encode(`${ms}`), timestamp: Timestamp.fromMillis(ms) });
group.close();
producer.writeGroup(group);
}

// Group 0 reaches at most 200ms, where group 1 starts, so it is past the budget.
// Group 1 reaches 3000ms, the edge itself.
expect((await track.recvGroup())?.sequence).toBe(1);
expect((await track.recvGroup())?.sequence).toBe(2);
});

// The ordered frame helpers ride the same cursor, so they see the same budget: a
// backlog inside it is drained in full, and what is past it is skipped.
test("ordered frame reads follow the budget", async () => {
Expand Down Expand Up @@ -963,6 +986,36 @@ test("retention reclaims a group the publisher abandoned open", async () => {
}
});

test("an idle live edge ages out once a newer group arrives", async () => {
const clock = mockMonotonicTime(10_000);
try {
const producer = new TrackProducer("test").accept({ maxAge: Milli(100) });
const track = producer.subscribe({ maxAge: Milli(100) });
const edge = producer.appendGroup();
edge.writeString("first");

const group = await track.recvGroup();
if (!group) throw new Error("missing group");
expect(await group.readString()).toBe("first");

// A prune while it is still the live edge keeps it, and finds nothing else to age out.
clock.set(10_200);
producer.subscribe({ maxAge: Milli(100) });

// A successor ends the exemption, and the group is long past the window, so the
// write evicts it rather than a later wakeup.
clock.set(10_300);
producer.appendGroup();
const read = group.readFrame().then(
() => "clean end",
(err: unknown) => err,
);
expect(await Promise.race([read, settle().then(() => "still parked")])).toBeInstanceOf(TooFarBehind);
} finally {
clock.restore();
}
});

test("an abandoned open group ages out with no further write", async () => {
// Real time, since this is about the wakeup: nothing writes to the track again, so
// without a timer the read below parks forever.
Expand Down
Loading
Loading