pub struct LatencyHistogram { /* private fields */ }Expand description
A simple fixed-bucket latency histogram.
Bucket upper bounds are inclusive (i.e., a sample of exactly 1 ms falls into bucket 0). Bucket index mapping:
- 0: ≤ 1 ms
- 1: 2 – 5 ms
- 2: 6 – 10 ms
- 3: 11 – 50 ms
- 4: 51 – 100 ms
- 5: 101 – 500 ms
- 6: > 500 ms
Implementations§
Source§impl LatencyHistogram
impl LatencyHistogram
Sourcepub fn std_dev_ms(&self) -> f64
pub fn std_dev_ms(&self) -> f64
Return a bucket-midpoint approximation of the standard deviation in ms.
Uses the midpoint of each histogram bucket to estimate the second moment,
then applies √(E[X²] − E[X]²). Returns 0.0 when fewer than two
samples have been recorded.
§Accuracy
The result is an estimate; its accuracy improves as the sample count increases and degrades near the boundaries of wide buckets.
Sourcepub fn percentile(&self, p: f64) -> u64
pub fn percentile(&self, p: f64) -> u64
Estimate the p-th percentile latency in milliseconds from the histogram.
p must be in [0.0, 1.0]. Returns the upper bound of the first
bucket that contains the p-th percentile. Returns 0 if no samples
have been recorded.
§Accuracy
This is a bucket-boundary estimate, not an exact value. The error is bounded by the bucket width at that percentile.
Sourcepub fn mode_bucket_ms(&self) -> Option<u64>
pub fn mode_bucket_ms(&self) -> Option<u64>
Return the upper-bound of the bucket with the highest sample count (the mode).
Returns None if no samples have been recorded. When multiple buckets
tie for the maximum, the lowest-latency bucket is returned.
Sourcepub fn buckets(&self) -> Vec<(u64, u64)>
pub fn buckets(&self) -> Vec<(u64, u64)>
Return bucket counts as (upper_bound_ms, count) pairs.
Sourcepub fn min_ms(&self) -> Option<u64>
pub fn min_ms(&self) -> Option<u64>
Return the minimum recorded latency in ms, or None if no samples.
Sourcepub fn max_ms(&self) -> Option<u64>
pub fn max_ms(&self) -> Option<u64>
Return the maximum recorded latency in ms, or None if no samples.
Sourcepub fn range_ms(&self) -> Option<u64>
pub fn range_ms(&self) -> Option<u64>
Return the spread (max − min) of recorded latencies in milliseconds.
Returns None if no samples have been recorded. A narrow range
indicates consistent latency; a wide range suggests outliers.
Sourcepub fn interquartile_range_ms(&self) -> u64
pub fn interquartile_range_ms(&self) -> u64
Return the interquartile range (p75 − p25) in milliseconds.
A measure of dispersion that is less sensitive to outliers than
range_ms. Returns 0 when fewer than two samples have been
recorded (p25 == p75 == 0).
Sourcepub fn p10(&self) -> u64
pub fn p10(&self) -> u64
Return the 10th-percentile latency in milliseconds.
Useful for assessing the “best case” tail of the distribution.
Sourcepub fn median_ms(&self) -> u64
pub fn median_ms(&self) -> u64
Return the median (50th-percentile) step latency in milliseconds.
Convenience alias for p50; useful when callers want an explicit
“median” name without importing percentile constants.
Sourcepub fn sum_ms(&self) -> u64
pub fn sum_ms(&self) -> u64
Return the total sum of all recorded latency samples in milliseconds.
Equivalent to mean_ms() * count() but avoids floating-point arithmetic.
Sourcepub fn coefficient_of_variation(&self) -> f64
pub fn coefficient_of_variation(&self) -> f64
Return the coefficient of variation: std_dev_ms / mean_ms.
A value of 0.0 means no variation; higher values indicate more
spread in latency. Returns 0.0 when mean_ms is zero (empty
histogram or all-zero samples) to avoid division by zero.
Sourcepub fn sample_count(&self) -> u64
pub fn sample_count(&self) -> u64
Return the total number of samples recorded in this histogram.
Sourcepub fn percentile_spread(&self) -> u64
pub fn percentile_spread(&self) -> u64
Return the difference between the p99 and p50 latency buckets in milliseconds.
A larger spread indicates a long-tail latency distribution.
Returns 0 when no samples have been recorded.
Sourcepub fn bucket_counts(&self) -> [u64; 7]
pub fn bucket_counts(&self) -> [u64; 7]
Return the count for each bucket as an array, in order from the fastest (≤1ms) to the slowest (>500ms) bucket.
Sourcepub fn min_occupied_ms(&self) -> Option<u64>
pub fn min_occupied_ms(&self) -> Option<u64>
Return the upper bound (ms) of the lowest bucket that has at least one
sample, or None if no samples have been recorded.
Sourcepub fn max_occupied_ms(&self) -> Option<u64>
pub fn max_occupied_ms(&self) -> Option<u64>
Return the upper-bound of the largest bucket with at least one recorded sample.
Returns None if the histogram is empty.
Sourcepub fn occupied_bucket_count(&self) -> usize
pub fn occupied_bucket_count(&self) -> usize
Return the number of buckets that have at least one recorded sample.
Sourcepub fn is_skewed(&self) -> bool
pub fn is_skewed(&self) -> bool
Return true if the latency distribution is skewed (p99 > 2 × p50).
Returns false for empty histograms.
Sourcepub fn is_uniform(&self) -> bool
pub fn is_uniform(&self) -> bool
Return true if all recorded samples fall into exactly one bucket.
An empty histogram is considered uniform.
Sourcepub fn clear(&self)
pub fn clear(&self)
Reset all histogram counters to zero.
Alias for reset using more conventional naming.
Sourcepub fn is_above_p99(&self, latency_ms: u64) -> bool
pub fn is_above_p99(&self, latency_ms: u64) -> bool
Return true if latency_ms is strictly greater than the current p99.
Useful for detecting outlier requests at call sites without storing
the p99 value separately. Returns false when the histogram is empty.
Sourcepub fn is_below_p99(&self, threshold_ms: u64) -> bool
pub fn is_below_p99(&self, threshold_ms: u64) -> bool
Return true if the p99 latency is strictly below threshold_ms.
Useful for SLO checks. Returns true when no samples have been
recorded (p99 == 0).