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
31 changes: 30 additions & 1 deletion rs/moq-net/benches/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
//! - lagging: a relay hears the origin on two streams, the second some one-claim
//! batches behind the first, and each hears its next batch. Swept over the claims
//! the origin holds and how far the second lags: neither axis is expected to grow.
//! - confirmed: a relay holds back every claim from the streams it answers until it
//! confirms serving one, all of them at one instance and epoch. One claim restarts at
//! another epoch, then back at the shared one, each confirmed. Swept over the claims
//! held and the streams answered: the streams axis is expected to grow, since each is
//! sent the claim; the claims axis is not.
//!
//! The route layer is crate-private, so this reaches it through the `fuzz` module.
//!
Expand All @@ -25,6 +30,9 @@ const SHAPES: [(usize, usize); 4] = [(10, 10), (10_000, 10), (10, 1_000), (10_00
/// `(claims, changes)` shapes for a relay hearing a batch.
const RECEIVED: [(usize, usize); 4] = [(100, 1), (10_000, 1), (100, 100), (10_000, 100)];

/// `(claims, streams)` shapes for a relay holding back claims.
const HELD: [(usize, usize); 4] = [(10, 10), (10_000, 10), (10, 100), (10_000, 100)];

/// `(claims, lag)` shapes for a relay hearing a stream that lags another.
const LAGGING: [(usize, usize); 4] = [(100, 1), (10_000, 1), (100, 100), (10_000, 10_000)];

Expand Down Expand Up @@ -89,5 +97,26 @@ fn bench_lagging(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, bench_watched, bench_unwatched, bench_received, bench_lagging);
fn bench_confirmed(c: &mut Criterion) {
let mut group = c.benchmark_group("routes/confirmed");
for (claims, streams) in HELD {
let id = BenchmarkId::from_parameter(format!("{claims}c_{streams}s"));
let mut fixture = None;
group.bench_function(id, |b| {
let relay = fixture.get_or_insert_with(|| fuzz::RouteRelay::held(claims, streams));
// Each change's serving change, then one batch per stream answered.
b.iter(|| assert_eq!(relay.restart(), 2 * (1 + streams)));
});
}
group.finish();
}

criterion_group!(
benches,
bench_watched,
bench_unwatched,
bench_received,
bench_lagging,
bench_confirmed
);
criterion_main!(benches);
50 changes: 41 additions & 9 deletions rs/moq-net/src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ impl RouteNode {
}

/// A relay's lite-07 route node hearing one origin's claims on streams it asked for,
/// and forwarding them on a stream it answers, for the routes bench.
/// and forwarding them on streams it answers, for the routes bench.
pub struct RouteRelay {
// Dropped first: a stream disconnects from its node on drop.
receive: Vec<crate::route::node::Stream>,
answer: crate::route::node::Stream,
answer: Vec<crate::route::node::Stream>,
#[allow(dead_code, reason = "held so the streams keep a node to talk to")]
node: crate::route::Node,
instance: crate::Instance,
Expand All @@ -663,11 +663,13 @@ impl RouteRelay {
/// The origin's identity as the relay hears it.
const ORIGIN: u64 = 1;

/// A relay that already heard and forwarded the origin's `claims` claims under
/// `live/`, on each of `streams` streams.
fn new(claims: usize, streams: usize) -> Self {
/// A relay that already heard the origin's `claims` claims under `live/` on each of
/// `streams` streams, and forwarded them on each of `answers` streams.
fn new(claims: usize, streams: usize, answers: usize) -> Self {
let node = crate::route::Node::new(2, 2);
let answer = node.answer(crate::route::Link::default(), Path::empty()).unwrap();
let answer = (0..answers)
.map(|_| node.answer(crate::route::Link::default(), Path::empty()).unwrap())
.collect();
let instance = crate::Instance::random();
let rev = claims as u64;
let advert = crate::route::Advert {
Expand Down Expand Up @@ -703,13 +705,20 @@ impl RouteRelay {

/// A relay hearing the origin's `claims` claims on one stream.
pub fn heard(claims: usize) -> Self {
Self::new(claims, 1)
Self::new(claims, 1, 1)
}

/// A relay hearing the origin's `claims` claims on one stream and answering
/// `answers` streams, holding every claim back from them since none is confirmed
/// served.
pub fn held(claims: usize, answers: usize) -> Self {
Self::new(claims, 1, answers)
}

/// A relay hearing the origin's `claims` claims on two streams, the second `lag`
/// one-claim batches behind the first.
pub fn lagging(claims: usize, lag: usize) -> Self {
let mut relay = Self::new(claims, 2);
let mut relay = Self::new(claims, 2, 1);
for _ in 0..lag {
relay.advance();
}
Expand Down Expand Up @@ -742,6 +751,29 @@ impl RouteRelay {
self.drain()
}

/// The first claim restarts at another epoch, then back at the one every other
/// claim shares, the relay confirming each, returning how many outputs it queued.
pub fn restart(&mut self) -> usize {
self.confirm(1) + self.confirm(0)
}

/// The first claim changes to `epoch`, and the relay confirms serving it.
fn confirm(&mut self, epoch: u64) -> usize {
self.rev += 1;
let claim = crate::route::node::Claim {
epoch,
..Self::claim(&self.instance, 0)
};
self.receive[0].batch(Self::ORIGIN, vec![(Self::path(0), Some(claim))], Some(self.rev));
let serves: Vec<_> = std::iter::from_fn(|| self.receive[0].try_output()).collect();
for serve in &serves {
if let crate::route::node::Output::Serve { id, claim, served, .. } = serve {
self.receive[0].serving(*id, claim, &served.claim);
}
}
serves.len() + self.drain()
}

/// The first stream hears the origin's next revision, which changes one claim.
fn advance(&mut self) {
self.rev += 1;
Expand All @@ -762,7 +794,7 @@ impl RouteRelay {
}

fn drain(&self) -> usize {
(self.receive.iter().chain([&self.answer]))
(self.receive.iter().chain(&self.answer))
.map(|stream| std::iter::from_fn(|| stream.try_output()).count())
.sum()
}
Expand Down
4 changes: 2 additions & 2 deletions rs/moq-net/src/lite/subscriber/routed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ impl Routed {
if same {
entry.named = named;
entry.route.update(route);
self.stream.serving(id, &key.1);
self.stream.serving(id, &key.1, &served.claim);
return;
}
}
Expand All @@ -493,7 +493,7 @@ impl Routed {
let entry = AnnouncedRoute::new(route, dynamic, wake);
entry.waker.wake_by_ref();
self.served.insert(key.clone(), ServedClaim { named, route: entry });
self.stream.serving(id, &key.1);
self.stream.serving(id, &key.1, &served.claim);
}
Err(_) => {
// Outside our origin's scope.
Expand Down
Loading