diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index f692005c3dc..6860fbf8f0c 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -148,6 +148,17 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command cargo bench --locked -p moq-net --features fuzz --bench '*' -- --test + # One relay's stats tick (drain plus plain and compressed encode), swept + # over held paths, tiers, and how many of those paths changed, plus the + # decode table. `--test` runs each tick routine once (decode ignores it + # and runs its whole table), so a panic (a plain + # frame past the cache cap, a drain that drops a held path) fails here + # instead of on the next manual run. Timing is not compared. `bench` + # exposes the hidden producer hook; without it the tick target is skipped. + - name: moq-stats producer tick and decode benchmarks + if: ${{ !cancelled() }} + run: nix develop --command just rs bench-stats --test + # The catalog bench asserts the estimate rate limit's publish counts, so one pass is # the regression check. - name: moq-mux catalog estimate fanout benchmark diff --git a/Cargo.lock b/Cargo.lock index fae29eda186..38d9468df42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4904,6 +4904,7 @@ dependencies = [ name = "moq-stats" version = "0.2.12" dependencies = [ + "criterion", "futures", "moq-json", "moq-net", diff --git a/quest/m2/README.md b/quest/m2/README.md index 06af5ba204d..8620daa4ee2 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -85,7 +85,6 @@ waiting on an upstream release waits in [m4](/quest/m4/README.md). - [Capture multi-plane PipeWire cameras](/quest/m2/pipewire-camera-planes.md) - I420 and NV12 cameras that deliver one memory block per plane - [#2819](/quest/m2/2819-moq-video-carry-pipewire-dma-bufs-safely-into-the-vulkan.md) - moq-video: validate PipeWire DMA-BUFs into the Vulkan renderer on hardware - [vcpkg registry](/quest/m2/cpp-vcpkg.md) - a registry we own serves the prebuilt package to `vcpkg` manifests -- [Benchmark the moq-stats producer](/quest/m2/stats-producer-bench.md) - a Criterion target for one relay's per-tick stats drain and encode, swept over held paths - [Binary delta stats](/quest/m2/stats-delta.md) - an on-demand varint delta flavor of every stats track, if relay encode CPU still matters after the JSON fixes - [#3115](/quest/m2/3115-moqsink-the-publication-has-no-generation-so-a-flush.md) - moqsink: a flushing restart after EOS opens a new publication generation - [QUIC I/O boundary](/quest/m2/quic-io-boundary.md) - moq-uring receives from the buffer ring and transmits into registered buffers with no copy, once a profile says where diff --git a/quest/m2/bench-coverage.md b/quest/m2/bench-coverage.md index 7fa14549dba..faa6f92885a 100644 --- a/quest/m2/bench-coverage.md +++ b/quest/m2/bench-coverage.md @@ -10,9 +10,8 @@ update, and `moq-auth` token verification. Deferred to m2 in the 2026-09-30 audit. The `moq-pattern` benchmark is the one worth doing first, and could return to m1 as its own quest. The -`moq-stats` producer benchmark split out into -[its own quest](/quest/m2/stats-producer-bench.md) in the 2026-10-06 audit, -since [Binary delta stats](/quest/m2/stats-delta.md) gates on it alone. +`moq-stats` producer tick lives in `rs/moq-stats/benches/producer_tick.rs`; +[Binary delta stats](/quest/m2/stats-delta.md) gates on that profile. - `moq-pattern`: matching swept over pattern count and path depth. Patterns shipped as `rs/moq-pattern` and `js/pattern`, so the matcher gets one bench @@ -34,4 +33,3 @@ Public API: none. Wire: none. ## Related - [Benchmark regressions in CI](/quest/m1/bench-ci.md) - tracks these targets on PRs and nightly -- [Benchmark the moq-stats producer](/quest/m2/stats-producer-bench.md) - the stats producer target, split out of this quest diff --git a/quest/m2/stats-delta.md b/quest/m2/stats-delta.md index 1d4967ae650..26f279bb98c 100644 --- a/quest/m2/stats-delta.md +++ b/quest/m2/stats-delta.md @@ -20,9 +20,10 @@ stats subscriber in the worst scenario below), so bytes alone do not justify a new format. The motivation is relay encode CPU at 10k+ broadcasts and aggregator fan-in. #4019 already cut `.json.z` decode allocations about 42x (time 2.5-4x) by dropping per-key path tracking; `rs/moq-stats/benches/decode.rs` -measures it. Proceed only if CPU still matters after a profile of the -moq-json snapshot encoder (about 100 ms per tick at 10k broadcasts) has fixed -or ruled out its cost. Re-run the benchmark against that baseline; if JSON is +measures it. `rs/moq-stats/benches/producer_tick.rs` profiles the encode side: +a full rewrite at 10k held paths is about 22 ms per tick for one tier and +91 ms for four. Proceed only if CPU still matters after that profile has fixed +or ruled out the moq-json snapshot encoder's cost. Re-run the benchmark against that baseline; if JSON is close enough, abandon this quest with the numbers. **Evidence.** A prototype benchmark on top of #3955's commit `d86496be2` @@ -106,10 +107,6 @@ maintainer's call; ask before writing one. Public API impact: additive on moq-stats unless the helpers change. Wire impact: new on-demand tracks; existing tracks unchanged. -## Required - -- [Benchmark the moq-stats producer](/quest/m2/stats-producer-bench.md) - the moq-json snapshot encoder profile the gate needs - ## Related - [Stats format page](/doc/concept/stats.md) - where the new flavor is documented diff --git a/quest/m2/stats-producer-bench.md b/quest/m2/stats-producer-bench.md deleted file mode 100644 index 0b136476b8d..00000000000 --- a/quest/m2/stats-producer-bench.md +++ /dev/null @@ -1,37 +0,0 @@ -# [S] Benchmark the moq-stats producer - -## Goal - -A Criterion target measures what one relay pays per stats tick to drain its -registry and encode the traffic tracks, swept over held paths and tiers, and -runs in the nightly benchmark smoke. It is the moq-json snapshot encoder -profile [Binary delta stats](/quest/m2/stats-delta.md) gates on. - -## Plan - -Split out of [Bench coverage](/quest/m2/bench-coverage.md) in the 2026-10-06 -audit, so the stats-delta gate waits on this benchmark alone rather than on -four unrelated ones. - -- #4299 keeps an idle path in every frame while the registry holds its - counters (about 430 B per idle path per plain frame), and nothing measures - that or the per-tick drain as held paths grow. Sweep held paths (for - example 100 to 50k) against tiers and the share of idle versus changed - paths. -- Report time and allocations per tick, and plain and compressed bytes per - frame the way `rs/moq-stats/benches/decode.rs` reports its table, - including the point where a plain frame nears its size cap. -- Drive the real producer path (`process_slot`, the snapshot encoders) over - a synthetic `Registry`; a bench hook is fine if it stays out of the public - API. -- `decode.rs` is not in CI either; run both targets in the nightly benchmark - smoke, as `moq-net`'s are. - -Name the target after what it measures, so the CI comment reads without -opening the file. - -Public API: none. Wire: none. - -## Related - -- [Benchmark regressions in CI](/quest/m1/bench-ci.md) - tracks this target on PRs and nightly diff --git a/rs/justfile b/rs/justfile index 7ea132a915b..953a7c32c24 100644 --- a/rs/justfile +++ b/rs/justfile @@ -347,3 +347,8 @@ bench-playback: [positional-arguments] bench-rtmp *args: cargo bench --locked -p moq-rtmp --bench chunk_interleaving -- "$@" + +# One relay's stats tick, plus the decode table. `--test` runs each tick once; decode ignores it and prints its whole table. +[positional-arguments] +bench-stats *args: + cargo bench --locked -p moq-stats --features bench --bench producer_tick --bench decode -- "$@" diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 9fda8cae618..cc7bc76480c 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -224,6 +224,19 @@ pub fn excluding(origin: crate::origin::Consumer, peer: crate::Hop) -> crate::or origin.excluding(peer) } +/// Add `n` publisher-side payload bytes on `(path, tier)`, creating the entry +/// when the path is tracked. The stats producer bench marks a share of held +/// paths changed this way, without a media frame. A later [`crate::stats::Registry::report`] +/// drops the entry unless some guard still holds it. Not a public API. +pub fn bump_publisher_bytes( + registry: &crate::stats::Registry, + path: impl crate::AsPath, + tier: &crate::stats::Tier, + n: u64, +) { + registry.bump_publisher_bytes(path, tier, n); +} + /// Encode `announced` as a fresh [`AnnounceWriter`] stream. pub fn encode_announces(announced: &[Announced], compress: bool) -> Vec { let mut writer = AnnounceWriter::new(compress); @@ -1163,6 +1176,34 @@ mod tests { use std::collections::BTreeSet; + /// The bump lands on the publisher side and survives a second drain while + /// an announce guard still holds the entry. + #[test] + fn bump_publisher_bytes_stays_while_announced() { + use futures::FutureExt; + + let registry = crate::stats::Registry::new(crate::stats::Config::new()); + let (origin, driver) = crate::origin::Producer::new(crate::origin::Config::default()); + let _driver = driver; + let tier = crate::stats::Tier::new("t"); + let session = registry.tier(tier.clone()).session("root"); + let _broadcast = origin.publish("room/1", crate::origin::Route::default()).unwrap(); + let mut cursor = origin.consume().with_stats(session).announced(); + while cursor.next().now_or_never().flatten().is_some() {} + + bump_publisher_bytes(®istry, "room/1", &tier, 5); + let mut report = crate::stats::Report::default(); + registry.report(&mut report); + registry.report(&mut report); + let entry = report + .traffic + .iter() + .find(|entry| entry.path.as_str() == "room/1") + .expect("announced path"); + assert_eq!(entry.publisher.bytes, 5); + assert_eq!(entry.tier, tier); + } + fn target(name: &str) -> Target { TARGETS .iter() diff --git a/rs/moq-net/src/stats.rs b/rs/moq-net/src/stats.rs index 92095b2972e..fa2b7273d59 100644 --- a/rs/moq-net/src/stats.rs +++ b/rs/moq-net/src/stats.rs @@ -883,6 +883,17 @@ impl Registry { } } + /// Add `n` publisher-side payload bytes on `(path, tier)`. The hidden `fuzz` + /// module exposes this to the stats producer bench. A report drops the entry + /// unless a guard still holds it. + #[cfg(any(test, feature = "fuzz"))] + pub(crate) fn bump_publisher_bytes(&self, path: impl AsPath, tier: &Tier, n: u64) { + let Some(entry) = self.entry(path) else { + return; + }; + entry.tier(tier).publisher.bytes.fetch_add(n, Ordering::Relaxed); + } + fn entry(&self, path: impl AsPath) -> Option> { // A disabled registry never allocates state. let shared = self.shared.as_ref()?; diff --git a/rs/moq-stats/Cargo.toml b/rs/moq-stats/Cargo.toml index dd8eb203d23..cd8f91d0477 100644 --- a/rs/moq-stats/Cargo.toml +++ b/rs/moq-stats/Cargo.toml @@ -12,6 +12,12 @@ rust-version.workspace = true keywords = ["quic", "http3", "webtransport", "stats", "live"] categories = ["multimedia", "network-programming", "web-programming"] +[features] +# Exposes one producer tick to the producer_tick bench, through the hidden +# `produce::bench` module, and moq-net's hidden counter bump. Off by default +# and hidden from the docs. Not a supported API. +bench = ["moq-net/fuzz"] + [dependencies] moq-json = { workspace = true } moq-net = { workspace = true } @@ -22,6 +28,7 @@ tracing = { workspace = true } web-async = { workspace = true } [dev-dependencies] +criterion = { workspace = true } futures = { workspace = true } tokio = { workspace = true, features = ["macros", "rt", "test-util", "time"] } @@ -32,3 +39,9 @@ harness = false [[bench]] name = "aggregate" harness = false + +# Reaches the producer drain through the hidden `bench` module. +[[bench]] +name = "producer_tick" +harness = false +required-features = ["bench"] diff --git a/rs/moq-stats/benches/producer_tick.rs b/rs/moq-stats/benches/producer_tick.rs new file mode 100644 index 00000000000..43ffffccec9 --- /dev/null +++ b/rs/moq-stats/benches/producer_tick.rs @@ -0,0 +1,245 @@ +//! What one relay pays per stats tick to drain its registry and encode the +//! traffic tracks, swept over held paths, tiers, and the share of those paths +//! whose counters changed since the last tick. +//! +//! Idle paths stay in the plain snapshot for as long as the registry holds +//! them, so `heldB` grows with the table and is what nears the frame cap +//! (`cap%`). +//! `plainB` and `zB` are what this tick wrote (zero when nothing changed). +//! The plain side is a full snapshot; the compressed side is a merge-patch +//! delta. Time is the Criterion target `stats/tick`. The table is one warm-up +//! plus a few ticks, allocations included. +//! +//! Run `cargo bench -p moq-stats --features bench --bench producer_tick`. + +use std::alloc::{GlobalAlloc, Layout, System}; +use std::hint::black_box; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use futures::FutureExt; +use moq_net::{announce, broadcast, origin, stats}; +use moq_stats::produce::bench::{Driver, FrameBytes}; + +struct Counter; + +static COUNTING: AtomicBool = AtomicBool::new(false); +static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0); + +#[global_allocator] +static ALLOCATOR: Counter = Counter; + +unsafe impl GlobalAlloc for Counter { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + } + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + } + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + if COUNTING.load(Ordering::Relaxed) { + ALLOCATIONS.fetch_add(1, Ordering::Relaxed); + } + unsafe { System.realloc(ptr, layout, new_size) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) } + } +} + +/// Ticks averaged into one table row, after a warm-up drain. +const TABLE_TICKS: u32 = 4; + +/// Held paths. The last count is sized so its plain snapshot nears the cache cap; `cap%` shows how close. +const PATHS: [usize; 4] = [100, 1_000, 10_000, 50_000]; + +const TIERS: [usize; 2] = [1, 4]; + +/// Share of paths whose payload counter moves each tick. Zero stays idle. +const CHANGED: [usize; 3] = [0, 10, 100]; + +struct Relay { + registry: stats::Registry, + tiers: Vec, + paths: Vec, + /// Bump every nth path. Zero bumps nothing. + step: usize, + driver: Driver, + _origin: origin::Producer, + _origin_driver: origin::Driver, + _publishers: Vec, + _cursors: Vec, + _sessions: Vec, +} + +impl Relay { + fn build(paths: usize, tiers: usize, changed_percent: usize) -> Self { + let registry = stats::Registry::new( + stats::Config::new().with_exclude(moq_net::Pattern::subtree(".stats").expect("literal prefix")), + ); + let (origin, origin_driver) = origin::Producer::new(origin::Config::default()); + let path_names: Vec = (0..paths).map(|index| format!("room/{index:06}")).collect(); + let publishers: Vec<_> = path_names + .iter() + .map(|path| { + origin + .publish(path.as_str(), origin::Route::default()) + .expect("publish path") + }) + .collect(); + let mut cursors = Vec::with_capacity(tiers); + let mut sessions = Vec::with_capacity(tiers); + let tier_labels: Vec<_> = (0..tiers) + .map(|tier| { + let tier = stats::Tier::new(format!("t{tier}")); + let session = registry.tier(tier.clone()).session("root"); + let mut cursor = origin.consume().with_stats(session.clone()).announced(); + while cursor.next().now_or_never().flatten().is_some() {} + cursors.push(cursor); + sessions.push(session); + tier + }) + .collect(); + + let mut report = stats::Report::default(); + registry.report(&mut report); + assert_eq!(report.traffic.len(), paths * tiers, "one held entry per path and tier"); + + let driver = Driver::new(registry.clone(), origin.clone()).expect("stats broadcast"); + Self { + registry, + tiers: tier_labels, + paths: path_names, + step: 100usize.checked_div(changed_percent).unwrap_or(0), + driver, + _origin: origin, + _origin_driver: origin_driver, + _publishers: publishers, + _cursors: cursors, + _sessions: sessions, + } + } + + /// Move the changed share's publisher byte counter, outside the timed tick. + fn touch(&self) { + if self.step == 0 { + return; + } + for path in self.paths.iter().step_by(self.step) { + for tier in &self.tiers { + moq_net::fuzz::bump_publisher_bytes(&self.registry, path.as_str(), tier, 1); + } + } + } +} + +struct Row { + allocs: usize, + elapsed: Duration, + bytes: FrameBytes, +} + +/// Warm up once, then average a few ticks. The warm-up pays for track creation. +fn sample(relay: &mut Relay) -> Row { + black_box(relay.driver.tick()); + let mut allocs = 0; + let mut elapsed = Duration::ZERO; + let mut bytes = FrameBytes::default(); + for _ in 0..TABLE_TICKS { + relay.touch(); + ALLOCATIONS.store(0, Ordering::Relaxed); + COUNTING.store(true, Ordering::Relaxed); + let start = Instant::now(); + bytes = black_box(relay.driver.tick()); + elapsed += start.elapsed(); + COUNTING.store(false, Ordering::Relaxed); + allocs += ALLOCATIONS.load(Ordering::Relaxed); + } + Row { + allocs: allocs / TABLE_TICKS as usize, + elapsed: elapsed / TABLE_TICKS, + bytes, + } +} + +fn check_row(paths: usize, changed: usize, row: &Row) { + let cap = moq_net::group::MAX_CACHE_BYTES; + assert!(row.bytes.held > 0, "publisher snapshot missing"); + assert!( + row.bytes.held as u64 <= cap, + "plain frame exceeds the cache cap: {} > {cap}", + row.bytes.held + ); + // The snapshot keeps every held path, so a few paths cannot be a couple of braces. + assert!(row.bytes.held > paths, "plain snapshot did not grow with held paths"); + if changed > 0 { + assert!(row.bytes.plain > 0, "changed paths did not publish a plain frame"); + assert!( + row.bytes.compressed > 0, + "changed paths did not publish a compressed frame" + ); + } +} + +fn bench(c: &mut Criterion) { + let cap = moq_net::group::MAX_CACHE_BYTES; + println!("plain frame cap: {cap} bytes"); + println!("paths tiers changed% allocs/tick us/tick heldB cap% plainB zB"); + + let mut group = c.benchmark_group("stats/tick"); + group.sample_size(10); + group.warm_up_time(Duration::from_millis(200)); + group.measurement_time(Duration::from_secs(1)); + for &changed in &CHANGED { + for &tiers in &TIERS { + for &paths in &PATHS { + let id = BenchmarkId::from_parameter(format!("{paths}p_{tiers}t_{changed}pct")); + group.throughput(Throughput::Elements((paths * tiers) as u64)); + // Criterion re-enters the closure per sample, so build and print the row + // once, and only when this id runs (not under `--list` or a filter). + let mut relay = None; + group.bench_function(id, |b| { + let relay = relay.get_or_insert_with(|| { + let mut relay = Relay::build(paths, tiers, changed); + let row = sample(&mut relay); + let micros = row.elapsed.as_secs_f64() * 1e6; + println!( + "{paths:>5} {tiers:>5} {changed:>8} {allocs:>11} {micros:>8.1} {held:>8} {ratio:>4.0} {plain:>8} {z:>8}", + allocs = row.allocs, + held = row.bytes.held, + ratio = row.bytes.held as f64 * 100.0 / cap as f64, + plain = row.bytes.plain, + z = row.bytes.compressed, + ); + check_row(paths, changed, &row); + relay + }); + b.iter_custom(|iters| { + let mut total = Duration::ZERO; + for _ in 0..iters { + relay.touch(); + let start = Instant::now(); + black_box(relay.driver.tick()); + total += start.elapsed(); + } + total + }); + }); + } + } + } + group.finish(); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/rs/moq-stats/src/produce.rs b/rs/moq-stats/src/produce.rs index f6475e103f8..3b5ab03db93 100644 --- a/rs/moq-stats/src/produce.rs +++ b/rs/moq-stats/src/produce.rs @@ -386,8 +386,40 @@ impl Drain { group.finish(); } } + + /// Largest publisher-track payload across tiers: the snapshot still held, + /// and what this tick actually wrote. Idle paths stay in the snapshot, so + /// `held` is the plain frame that nears the cache cap. + #[cfg(feature = "bench")] + fn publisher_frame_bytes(&self) -> bench::FrameBytes { + let mut held = 0; + let mut plain = 0; + let mut compressed = 0; + for group in self.groups.values() { + for (name, pair) in &group.traffic.tracks { + if !name.ends_with("publisher.json") { + continue; + } + held = held.max(pair.held); + plain = plain.max(pair.wrote_plain); + compressed = compressed.max(pair.wrote_compressed); + } + } + bench::FrameBytes { + held, + plain, + compressed, + } + } } +/// Drives one producer tick for the benchmark. Not a public API: compiled only +/// with the `bench` feature, which nothing enables by default. +#[cfg(feature = "bench")] +#[doc(hidden)] +#[path = "produce_bench.rs"] +pub mod bench; + /// One track's frame, rebuilt every drain in a buffer kept across drains. /// Serializes as a JSON object keyed by path, byte-identical to /// [`TrafficFrame`](crate::TrafficFrame) / [`SessionsFrame`](crate::SessionsFrame) once sorted. @@ -434,11 +466,14 @@ impl Snapshot { self.track.demand() } - fn update(&mut self, value: &Frame) -> moq_json::Result<()> { + /// Write one frame. `Some(len)` is the payload written; `None` means the + /// value was unchanged and the encoder emitted nothing. + fn update(&mut self, value: &Frame) -> moq_json::Result> { let Some(frame) = self.encoder.update(value)? else { - return Ok(()); + return Ok(None); }; - if frame.payload.len() as u64 > moq_net::group::MAX_CACHE_BYTES { + let len = frame.payload.len(); + if len as u64 > moq_net::group::MAX_CACHE_BYTES { return Err(moq_net::Error::FrameTooLarge.into()); } if frame.keyframe { @@ -458,7 +493,7 @@ impl Snapshot { self.group.take().unwrap().finish()?; } frame.commit(); - Ok(()) + Ok(Some(len)) } fn finish(&mut self) -> moq_json::Result<()> { @@ -478,6 +513,16 @@ struct TrackPair { compressed: Snapshot, /// This drain's entries, published and cleared by [`Self::publish`]. frame: Frame, + /// Last plain payload written, kept so a later unchanged tick can still + /// report the snapshot size. Bench builds only. + #[cfg(feature = "bench")] + held: usize, + /// Plain and compressed bytes this drain actually wrote. Zero when the + /// encoder skipped an unchanged value. Bench builds only. + #[cfg(feature = "bench")] + wrote_plain: usize, + #[cfg(feature = "bench")] + wrote_compressed: usize, } impl TrackPair { @@ -496,6 +541,12 @@ impl TrackPair { plain: Snapshot::new(plain_track, plain_config, sequence.clone()), compressed: Snapshot::new(compressed_track, compressed_config, sequence), frame: Frame::default(), + #[cfg(feature = "bench")] + held: 0, + #[cfg(feature = "bench")] + wrote_plain: 0, + #[cfg(feature = "bench")] + wrote_compressed: 0, } } @@ -508,15 +559,38 @@ impl TrackPair { /// and clear them for the next drain; moq-json skips unchanged values. fn publish(&mut self, name: &str) { self.frame.entries.sort_unstable_by(|a, b| a.0.cmp(&b.0)); - if let Err(err) = self.plain.update(&self.frame) { - tracing::debug!(?err, name, "stats: failed to write frame"); - } - if let Err(err) = self.compressed.update(&self.frame) { - tracing::debug!(?err, name, "stats: failed to write compressed frame"); - } + let plain = match self.plain.update(&self.frame) { + Ok(len) => len, + Err(err) => { + tracing::debug!(?err, name, "stats: failed to write frame"); + None + } + }; + let compressed = match self.compressed.update(&self.frame) { + Ok(len) => len, + Err(err) => { + tracing::debug!(?err, name, "stats: failed to write compressed frame"); + None + } + }; + self.record_bench(plain, compressed); self.frame.entries.clear(); } + #[cfg(feature = "bench")] + fn record_bench(&mut self, plain: Option, compressed: Option) { + self.wrote_plain = plain.unwrap_or(0); + self.wrote_compressed = compressed.unwrap_or(0); + if let Some(len) = plain { + self.held = len; + } + } + + #[cfg(not(feature = "bench"))] + fn record_bench(&mut self, plain: Option, compressed: Option) { + let _ = (plain, compressed); + } + /// Finish both flavors, so dropping the pair is a deliberate end instead of /// a dropped-without-finish warning. An error means the track already /// ended; there is nothing left to close. diff --git a/rs/moq-stats/src/produce_bench.rs b/rs/moq-stats/src/produce_bench.rs new file mode 100644 index 00000000000..14767c39a4d --- /dev/null +++ b/rs/moq-stats/src/produce_bench.rs @@ -0,0 +1,60 @@ +//! One producer tick, without the publish interval. The benchmark drives this +//! over a registry it filled itself. Not a public API. + +use std::sync::Arc; +use std::sync::atomic::AtomicU64; +use std::time::Duration; + +use moq_net::{PathOwned, origin, stats::Registry}; +use web_async::time::Instant; + +use super::{Drain, Task}; + +/// Plain and compressed publisher-frame sizes from one tick, in bytes. +/// +/// `held` is the largest plain snapshot any tier is still holding (idle paths +/// included). `plain` and `compressed` are what this tick wrote, or zero when +/// the encoder skipped an unchanged value. Each is the max across tiers, so a +/// single frame can be compared with the cache cap. +#[derive(Clone, Copy, Debug, Default)] +pub struct FrameBytes { + /// Largest plain publisher snapshot still held. + pub held: usize, + /// Largest plain publisher payload written this tick. + pub plain: usize, + /// Largest compressed publisher payload written this tick. + pub compressed: usize, +} + +/// A depth-0 stats publisher, driven one drain at a time. +pub struct Driver { + drain: Drain, +} + +impl Driver { + /// Publish `.stats/node` on `origin` and drain `registry`, the way + /// [`crate::Producer::new`] does at depth 0. `None` if the origin refuses + /// the broadcast. + pub fn new(registry: Registry, origin: origin::Producer) -> Option { + let task = Task { + registry, + origin, + prefix: PathOwned::from(".stats"), + node: None, + depth: 0, + linger: Duration::from_secs(300), + interval: Duration::from_secs(1), + sequence: Arc::new(AtomicU64::new(0)), + }; + Some(Self { + drain: Drain::new(task)?, + }) + } + + /// Drain the registry and encode every track once. + pub fn tick(&mut self) -> FrameBytes { + self.drain.collect(); + self.drain.publish(Instant::now()); + self.drain.publisher_frame_bytes() + } +}