pub struct PeerLatencyTracker { /* private fields */ }Expand description
Tracks per-peer RTT measurements with histogram, percentile, and ranking support.
Each peer maintains a fixed-size ring buffer of raw latency samples. Statistics (min, max, sum, count) are tracked cumulatively so that mean computation remains accurate even after samples are evicted.
Implementations§
Source§impl PeerLatencyTracker
impl PeerLatencyTracker
Sourcepub fn new(max_samples_per_peer: usize) -> Self
pub fn new(max_samples_per_peer: usize) -> Self
Create a new tracker with the specified ring buffer size per peer.
Sourcepub fn record(&mut self, peer_id: &str, latency_us: u64)
pub fn record(&mut self, peer_id: &str, latency_us: u64)
Record a latency sample for the given peer.
A PeerLatency entry is automatically created if one does not already
exist. The ring buffer evicts the oldest sample when full.
Sourcepub fn percentile(&self, peer_id: &str, p: f64) -> Option<u64>
pub fn percentile(&self, peer_id: &str, p: f64) -> Option<u64>
Compute a percentile value for a peer using linear interpolation.
p must be in [0.0, 1.0] (e.g., 0.99 for p99).
Returns None if the peer is unknown or has no samples.
Sourcepub fn mean(&self, peer_id: &str) -> Option<f64>
pub fn mean(&self, peer_id: &str) -> Option<f64>
Compute the mean latency for a peer.
Uses cumulative sum/count for accuracy, not just buffered samples.
Returns None if the peer is unknown or has no samples.
Sourcepub fn median(&self, peer_id: &str) -> Option<u64>
pub fn median(&self, peer_id: &str) -> Option<u64>
Compute the median (p50) latency for a peer.
Returns None if the peer is unknown or has no samples.
Sourcepub fn histogram(
&self,
peer_id: &str,
bucket_count: usize,
) -> Option<Vec<LatencyBucket>>
pub fn histogram( &self, peer_id: &str, bucket_count: usize, ) -> Option<Vec<LatencyBucket>>
Generate a histogram of latency samples for a peer.
The range [min, max] is divided into bucket_count equal-width buckets.
Returns None if the peer is unknown, has no samples, or bucket_count is 0.
Sourcepub fn fastest_peers(&self, n: usize) -> Vec<(String, f64)>
pub fn fastest_peers(&self, n: usize) -> Vec<(String, f64)>
Return the n peers with the lowest mean latency.
Results are sorted ascending by mean latency.
Sourcepub fn slowest_peers(&self, n: usize) -> Vec<(String, f64)>
pub fn slowest_peers(&self, n: usize) -> Vec<(String, f64)>
Return the n peers with the highest mean latency.
Results are sorted descending by mean latency.
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> bool
pub fn remove_peer(&mut self, peer_id: &str) -> bool
Remove all tracking state for peer_id.
Returns true if the peer existed and was removed.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the number of peers currently being tracked.
Sourcepub fn global_mean(&self) -> Option<f64>
pub fn global_mean(&self) -> Option<f64>
Compute the global mean latency across all peers.
Returns None when no samples have been recorded.
Sourcepub fn stats(&self) -> LatencyTrackerStats
pub fn stats(&self) -> LatencyTrackerStats
Return aggregate statistics for the tracker.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerLatencyTracker
impl RefUnwindSafe for PeerLatencyTracker
impl Send for PeerLatencyTracker
impl Sync for PeerLatencyTracker
impl Unpin for PeerLatencyTracker
impl UnsafeUnpin for PeerLatencyTracker
impl UnwindSafe for PeerLatencyTracker
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more