pub struct StatsCollector { /* private fields */ }Expand description
Stateful statistics collector for irtt-client events.
A collector maintains cumulative statistics and, when configured, rolling windows. Rolling snapshots are recomputed from retained normalized events.
§Example
Feed a short stream of irtt-client events to a finite-mode collector and
read back a cumulative snapshot:
use irtt_stats::{StatsCollector, StatsConfig};
let mut collector = StatsCollector::new(StatsConfig::finite());
for (seq, rtt_ms) in [10_u64, 30, 20].into_iter().enumerate() {
collector.process(&reply(seq as u32, rtt_ms));
}
let snapshot = collector.snapshot();
assert_eq!(snapshot.events.echo_replies, 3);
assert_eq!(snapshot.rtt.primary.count, 3);
// Finite mode retains exact samples, so the round-trip metric reports an
// exact median: the middle of the sorted 10, 20, 30 ms samples.
assert_eq!(snapshot.rtt.primary.median_ns, Some(20_000_000.0));Implementations§
Source§impl StatsCollector
impl StatsCollector
Sourcepub fn new(config: StatsConfig) -> Self
pub fn new(config: StatsConfig) -> Self
Creates a collector with the supplied configuration.
Sourcepub fn process(&mut self, event: &ClientEvent) -> EventStatsUpdate
pub fn process(&mut self, event: &ClientEvent) -> EventStatsUpdate
Processes one client event and returns the per-event statistics update.
Updates currently report whether the event contributed a unique reply timing sample and any adjacent-sequence IPDV pairs completed by this event.
Sourcepub fn snapshot(&self) -> Snapshot
pub fn snapshot(&self) -> Snapshot
Returns a snapshot of all events processed by this collector.
This is the snapshot StatsConfig::samples applies to, so under
SampleMode::Exact every timing metric here reports a median once it
has samples.
Sourcepub fn rolling_count(&self) -> Option<Snapshot>
pub fn rolling_count(&self) -> Option<Snapshot>
Returns the configured count-based rolling snapshot, if enabled.
The snapshot is recomputed from the retained rolling events using
running statistics only, whatever StatsConfig::samples is set to,
so its timing metrics report no median. A rolling window is a bounded
recent view rather than the run’s retained history.
§Example
use irtt_stats::{StatsCollector, StatsConfig};
let mut collector = StatsCollector::new(StatsConfig {
rolling_count: Some(2),
..StatsConfig::finite()
});
collector.process(&sent(0, 0, 10));
collector.process(&sent(1, 10, 10));
collector.process(&sent(2, 20, 10));
// The window keeps only the last two normalized events...
let rolling = collector.rolling_count().unwrap();
assert_eq!(rolling.packets.packets_sent, 2);
// ...and is recomputed with running statistics, so it reports no median
// even though the collector retains exact samples for its cumulative
// snapshot.
assert_eq!(rolling.send_call.median_ns, None);
// The cumulative snapshot still sees every event.
let cumulative = collector.snapshot();
assert_eq!(cumulative.packets.packets_sent, 3);
assert_eq!(cumulative.send_call.median_ns, Some(10_000.0));Sourcepub fn rolling_time(&self) -> Option<Snapshot>
pub fn rolling_time(&self) -> Option<Snapshot>
Returns the configured time-based rolling snapshot, if enabled.
Like StatsCollector::rolling_count, this is recomputed with running
statistics only and reports no medians.
§Example
use std::time::Duration;
use irtt_stats::{StatsCollector, StatsConfig};
let mut collector = StatsCollector::new(StatsConfig {
rolling_time: Some(Duration::from_millis(15)),
..StatsConfig::finite()
});
collector.process(&sent(0, 0, 10));
collector.process(&sent(1, 10, 10));
collector.process(&sent(2, 30, 10));
// A 15 ms window anchored at the latest event (30 ms) retains only the
// events from the last 15 ms — here just the final send.
let rolling = collector.rolling_time().unwrap();
assert_eq!(rolling.packets.packets_sent, 1);