Skip to main content

Histogram

Struct Histogram 

Source
pub struct Histogram { /* private fields */ }
Expand description

A fixed-bucket histogram for tracking latencies and payload sizes.

All operations are O(BUCKET_COUNT) and allocation free.

Implementations§

Source§

impl Histogram

Source

pub const OVERFLOW_SENTINEL: u64 = u64::MAX

Sentinel value returned by Histogram::percentile, Histogram::mean, and Histogram::max when the overflow bucket is non-empty.

Source

pub fn new() -> Self

Construct a fresh histogram with all bucket counts set to zero.

§Examples
use dynomite::stats::Histogram;
let h = Histogram::new();
assert_eq!(h.count(), 0);
Source

pub fn record(&mut self, val: u64)

Record a single observation, placing it in the appropriate bucket.

Values larger than the largest bucket offset land in the final bucket and signal histogram overflow. Once the overflow bucket is non-empty, Histogram::percentile, Histogram::mean, and Histogram::max all return Histogram::OVERFLOW_SENTINEL.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(42);
assert_eq!(h.count(), 1);
assert_eq!(h.max(), 42);
Source

pub fn is_overflowing(&self) -> bool

Returns true when the final (overflow) bucket is non-empty.

The reference implementation logs an error and refuses to publish quantiles in this case; the Rust port surfaces the same signal through this method and through the Histogram::OVERFLOW_SENTINEL returned from quantile accessors.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(10);
assert!(!h.is_overflowing());
Source

pub fn count(&self) -> u64

Total number of observations recorded.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(1);
h.record(2);
assert_eq!(h.count(), 2);
Source

pub fn percentile(&self, p: f64) -> u64

Approximate percentile, where p is in the closed interval [0.0, 1.0]. Inputs outside the range or NaN return 0.

The result is the offset of the first bucket whose cumulative count meets or exceeds floor(count * p). Returns Histogram::OVERFLOW_SENTINEL if the histogram is Histogram::is_overflowing.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
for v in 0..1_000 { h.record(v); }
assert!(h.percentile(0.95) >= h.percentile(0.5));
Source

pub fn mean(&self) -> f64

Arithmetic mean of all observations using bucket offsets as representative values. Returns 0.0 for an empty histogram and f64::INFINITY when the histogram is Histogram::is_overflowing.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(10);
assert!(h.mean() > 0.0);
Source

pub fn max(&self) -> u64

Maximum observation seen since the last Histogram::reset. Returns Histogram::OVERFLOW_SENTINEL when the histogram is Histogram::is_overflowing.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(99);
assert_eq!(h.max(), 99);
Source

pub fn reset(&mut self)

Reset all bucket counts and the recorded maximum to zero.

§Examples
use dynomite::stats::Histogram;
let mut h = Histogram::new();
h.record(7);
h.reset();
assert_eq!(h.count(), 0);
Source

pub fn merge(&mut self, other: &Self)

Merge bucket counts from another histogram into this one.

The maximum is updated to the larger of the two recorded maxima.

§Examples
use dynomite::stats::Histogram;
let mut a = Histogram::new();
let mut b = Histogram::new();
a.record(10);
b.record(20);
a.merge(&b);
assert_eq!(a.count(), 2);
assert_eq!(a.max(), 20);
Source

pub fn buckets(&self) -> &[u64; 94]

Borrow the raw bucket counts.

§Examples
use dynomite::stats::{Histogram, BUCKET_COUNT};
let h = Histogram::new();
assert_eq!(h.buckets().len(), BUCKET_COUNT);

Trait Implementations§

Source§

impl Clone for Histogram

Source§

fn clone(&self) -> Histogram

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Histogram

Source§

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

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

impl Default for Histogram

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,