Skip to main content

LatencyHistogram

Struct LatencyHistogram 

Source
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

Source

pub fn record(&self, ms: u64)

Record a latency sample in milliseconds.

Source

pub fn mean_ms(&self) -> f64

Return the mean latency in ms, or 0.0 if no samples.

Source

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.

Source

pub fn count(&self) -> u64

Return the total sample count.

Source

pub fn has_data(&self) -> bool

Return true if at least one sample has been recorded.

Source

pub fn is_empty(&self) -> bool

Return true when no samples have been recorded yet.

Source

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.

Source

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.

Source

pub fn buckets(&self) -> Vec<(u64, u64)>

Return bucket counts as (upper_bound_ms, count) pairs.

Source

pub fn min_ms(&self) -> Option<u64>

Return the minimum recorded latency in ms, or None if no samples.

Source

pub fn max_ms(&self) -> Option<u64>

Return the maximum recorded latency in ms, or None if no samples.

Source

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.

Source

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).

Source

pub fn p50(&self) -> u64

Return the 50th-percentile (median) latency in milliseconds.

Source

pub fn p95(&self) -> u64

Return the 95th-percentile latency in milliseconds.

Source

pub fn p99(&self) -> u64

Return the 99th-percentile latency in milliseconds.

Source

pub fn p25(&self) -> u64

Return the 25th-percentile latency in milliseconds.

Source

pub fn p75(&self) -> u64

Return the 75th-percentile latency in milliseconds.

Source

pub fn p90(&self) -> u64

Return the 90th-percentile latency in milliseconds.

Source

pub fn p10(&self) -> u64

Return the 10th-percentile latency in milliseconds.

Useful for assessing the “best case” tail of the distribution.

Source

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.

Source

pub fn reset(&self)

Reset all histogram counters to zero.

Source

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.

Source

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.

Source

pub fn sample_count(&self) -> u64

Return the total number of samples recorded in this histogram.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn occupied_bucket_count(&self) -> usize

Return the number of buckets that have at least one recorded sample.

Source

pub fn is_skewed(&self) -> bool

Return true if the latency distribution is skewed (p99 > 2 × p50).

Returns false for empty histograms.

Source

pub fn is_uniform(&self) -> bool

Return true if all recorded samples fall into exactly one bucket.

An empty histogram is considered uniform.

Source

pub fn clear(&self)

Reset all histogram counters to zero.

Alias for reset using more conventional naming.

Source

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.

Source

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).

Trait Implementations§

Source§

impl Debug for LatencyHistogram

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LatencyHistogram

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more