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
impl Histogram
Sourcepub const OVERFLOW_SENTINEL: u64 = u64::MAX
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.
Sourcepub fn new() -> Self
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);Sourcepub fn record(&mut self, val: u64)
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);Sourcepub fn is_overflowing(&self) -> bool
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());Sourcepub fn count(&self) -> u64
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);Sourcepub fn percentile(&self, p: f64) -> u64
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));Sourcepub fn mean(&self) -> f64
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);Sourcepub fn max(&self) -> u64
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);Sourcepub fn reset(&mut self)
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);Sourcepub fn merge(&mut self, other: &Self)
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);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Histogram
impl RefUnwindSafe for Histogram
impl Send for Histogram
impl Sync for Histogram
impl Unpin for Histogram
impl UnsafeUnpin for Histogram
impl UnwindSafe for Histogram
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.