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/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ blocks. The quests that gated m0 lines moved under them.
- [Papercuts](/quest/m1/papercuts.md) - JS refuses to serve a broadcast it did not produce, and a uring test stops sleeping
- [Front parking](/quest/m1/origin-front-parks.md) - an unroutable request waits on a front instead of re-asking on every route-table move
- [Route wakes](/quest/m1/route-wakes.md) - a route change wakes only the fronts it can move, so pool churn stops scaling with served paths
- [Copy-walk bench](/quest/m1/admission-bench.md) - a front's walk over its copies on a route change is benchmarked over tracks and copies
- [Publish channel count](/quest/m1/publish-audio-channel-count.md) - forcing a channel count on an Audio.Capture stops costing the subscriber gaps of silence
- [JS request deadline](/quest/m1/js-request-deadline.md) - each JS request, PUBLISH_NAMESPACE included, has one fatal 10 s timer from create to answer and fails fast without stream credit, so nothing queues or retries
- [Rust request credit](/quest/m1/rs-request-credit.md) - a moq-net request fails at once without stream credit instead of waiting
Expand Down
31 changes: 0 additions & 31 deletions quest/m1/admission-bench.md

This file was deleted.

159 changes: 156 additions & 3 deletions rs/moq-net/benches/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
//! An equal-cost pool is swept the same way, over its members and the paths it
//! already serves.
//!
//! A route swap on one front is swept over its tracks and the copies each track
//! still holds from earlier routes.
//!
//! Run with `cargo bench -p moq-net --bench origin`.
use std::task::Poll;
use std::time::Duration;

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use futures::FutureExt;
use moq_net::{Hop, Hops, Pattern, Patterns, Timestamp, announce, broadcast, kio, origin};
use moq_net::{Epoch, Hop, Hops, Pattern, Patterns, Timestamp, announce, broadcast, kio, origin, track};

/// `(publishers, subscribers)` shapes for the fan-out benchmarks.
const SHAPES: [(usize, usize); 3] = [(100, 10), (1_000, 100), (1_000, 1_000)];
Expand Down Expand Up @@ -684,6 +687,155 @@ fn bench_parked(c: &mut Criterion) {
group.finish();
}

/// `(tracks, copies)` for [`bench_copy_walk`]: tracks one front is serving, and
/// route copies each of those tracks still holds when the route swaps.
///
/// The single-copy points from 32 to 128 tracks show the per-track slope alone.
const COPY_WALK: [(usize, usize); 8] = [(1, 1), (1, 32), (32, 1), (64, 1), (128, 1), (8, 32), (32, 8), (64, 16)];

const COPY_PATH: &str = "room/live";

/// How long a splice may take before the bench treats it as stuck.
const COPY_WAIT: Duration = Duration::from_secs(5);

/// One generation at [`COPY_PATH`], held so its copies stay subscribed.
struct Generation {
broadcast: broadcast::Producer,
tracks: Vec<track::Producer>,
}

impl Generation {
/// Create a hidden generation and leave a finished group on every track, so a
/// reader that does look has something to resume.
fn build(producer: &origin::Producer, tracks: usize) -> Self {
let broadcast = producer.create_broadcast(COPY_PATH).unwrap();
let mut held = Vec::with_capacity(tracks);
for i in 0..tracks {
let track = broadcast.create_track(format!("{i}"), None).unwrap();
let mut written = track.append_group().unwrap();
written.write_frame(Timestamp::ZERO, b"f".as_ref()).unwrap();
written.finish().unwrap();
held.push(track);
}
Self {
broadcast,
tracks: held,
}
}

/// Route the front to this generation without waiting.
fn route(&self, epoch: &Epoch) {
self.broadcast
.announce(origin::Route::default().with_epoch(epoch.clone()))
.unwrap();
}

/// Route the front to this generation and wait until every track is spliced.
///
/// The wait ends on demand, which flips when the front subscribes the new
/// copy, before its splice. That bounds the splice only on a current-thread
/// runtime, where every local query is ready within the front poll that
/// splices it.
async fn announce(&self, epoch: &Epoch) {
self.route(epoch);
for track in &self.tracks {
tokio::time::timeout(COPY_WAIT, track.demand().used())
.await
.expect("route swap did not splice")
.expect("replacement track closed");
}
}
}

/// A front serving [`COPY_PATH`], with `copies` route copies on each of its tracks.
struct CopyWalk {
epoch: Epoch,
_producer: origin::Producer,
_subscribers: Vec<track::Subscriber>,
generations: Vec<Generation>,
/// The generation the timed swap announces, built during setup.
next: Option<Generation>,
}

/// Build a front with `copies` already spliced onto each track.
///
/// Subscribers are never polled. A poll drops a replaced copy once its groups
/// are delivered, and this measures the walk over the copies a swap still has.
/// The same is why every generation is kept: dropping one ends its copy.
async fn copy_walk(tracks: usize, copies: usize) -> CopyWalk {
assert!(copies >= 1, "a front holds at least the serving copy");
let (producer, driver) = origin::Producer::new(origin::Config::default());
tokio::spawn(moq_net::time::run(driver));
let epoch = Epoch::mint();
let first = Generation::build(&producer, tracks);
first.route(&epoch);
let resolved = tokio::time::timeout(COPY_WAIT, producer.consume().request_broadcast(COPY_PATH))
.await
.expect("front did not resolve")
.expect("front refused");
let mut subscribers = Vec::with_capacity(tracks);
for i in 0..tracks {
let track = resolved.track(&format!("{i}")).unwrap();
let subscriber = tokio::time::timeout(COPY_WAIT, track.subscribe(None))
.await
.expect("track did not splice")
.expect("subscribe failed");
subscribers.push(subscriber);
}
let mut generations = Vec::with_capacity(copies + 1);
generations.push(first);
for _ in 1..copies {
let generation = Generation::build(&producer, tracks);
generation.announce(&epoch).await;
generations.push(generation);
}
CopyWalk {
next: Some(Generation::build(&producer, tracks)),
epoch,
_producer: producer,
_subscribers: subscribers,
generations,
}
}

impl CopyWalk {
/// Swap in the prebuilt newer route at the same epoch.
///
/// A local track's info is already known, so the front subscribes, walks
/// every track, and each reader walks its copies in one driver turn.
async fn swap(&mut self) {
let generation = self.next.take().expect("one swap per setup");
generation.announce(&self.epoch).await;
self.generations.push(generation);
}
}

/// A route swap on one front. Setup holds `copies` per track and builds the
/// next generation, untimed; the announce and splice are timed. Warm-up sizes
/// the iteration count from wall time, which includes that setup, so a large
/// front does not multiply it by a fast routine.
///
/// The runtime must stay current-thread; see [`Generation::announce`].
fn bench_copy_walk(c: &mut Criterion) {
let mut group = c.benchmark_group("origin/copy_walk");
group.sample_size(10);
for (tracks, copies) in COPY_WALK {
group.throughput(Throughput::Elements(tracks as u64));
group.bench_function(BenchmarkId::from_parameter(format!("{tracks}t_{copies}c")), |b| {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
b.iter_batched_ref(
|| runtime.block_on(copy_walk(tracks, copies)),
|rig| runtime.block_on(rig.swap()),
BatchSize::PerIteration,
);
});
}
group.finish();
}

criterion_group!(
benches,
bench_announce,
Expand All @@ -698,6 +850,7 @@ criterion_group!(
bench_pool_churn,
bench_handoff,
bench_relay,
bench_parked
bench_parked,
bench_copy_walk
);
criterion_main!(benches);
Loading