Expand description
This crate provides histogram implementations that are conceptually similar to HdrHistogram, with modifications to the bucket construction and indexing algorithms that we believe provide a simpler implementation and more efficient runtime compared to the reference implementation of HdrHistogram.
§Types
Histogram— standard histogram withu64counters. Use for single-threaded recording and percentile queries.Histogram32— likeHistogrambut withu32counters.AtomicHistogram— atomic histogram for concurrent recording. Take a snapshot withAtomicHistogram::loadorAtomicHistogram::drainto query percentiles.AtomicHistogram32— likeAtomicHistogrambut withu32counters.SparseHistogram— compact representation storing only non-zero buckets. Useful for serialization and storage.SparseHistogram32— likeSparseHistogrambut withu32counters.CumulativeROHistogram— read-only histogram with cumulative counts for fast quantile queries via binary search.CumulativeROHistogram32— likeCumulativeROHistogrambut withu32counters.
§Example
use histogram::{Histogram, Quantile};
let mut h = Histogram::new(7, 64).unwrap();
for value in 1..=100 {
h.increment(value).unwrap();
}
// Quantiles use the 0.0..=1.0 scale
let r50 = h.quantile(0.5).unwrap().unwrap();
let r99 = h.quantile(0.99).unwrap().unwrap();
// quantile() returns Result<Option<QuantilesResult>, Error>
// outer unwrap: quantile value is valid
// inner unwrap: histogram is non-empty
let p50 = r50.get(&Quantile::new(0.5).unwrap()).unwrap();
let p99 = r99.get(&Quantile::new(0.99).unwrap()).unwrap();
println!("p50: {}-{}", p50.start(), p50.end());
println!("p99: {}-{}", p99.start(), p99.end());§Counter Width
All four histogram types ship in two flavors:
- u64-counter family (
Histogram,AtomicHistogram,SparseHistogram,CumulativeROHistogram): the default. - u32-counter siblings (
Histogram32,AtomicHistogram32,SparseHistogram32,CumulativeROHistogram32): half the memory and serialization size; counts up to 2^32 − 1 per bucket.
Conversions: widening (u32 → u64) is infallible (From); narrowing
(u64 → u32) is fallible (TryFrom, returns Error::Overflow).
Direct cross-variant + narrowing paths support the snapshot pipeline.
§Recommended Pipeline
Pick the histogram type based on the role it plays:
- Recording —
AtomicHistogramorHistogram. Counts are unbounded over the lifetime of the process;u64is the safe choice. - Snapshot delta —
Histogram, then narrowed. Compute the delta withchecked_sub, thenTryFrominto the analytics type. - Read-only analytics —
CumulativeROHistogram32. Halved size, O(log n) quantile queries, total-count check is cheaper than per-bucket.
use histogram::{AtomicHistogram, CumulativeROHistogram32, Histogram};
let recorder = AtomicHistogram::new(7, 64).unwrap();
let snap_t1 = recorder.load();
let delta = snap_t1.checked_sub(&snap_t0).unwrap();
let analytic: CumulativeROHistogram32 =
CumulativeROHistogram32::try_from(&delta).unwrap();§Background
Please see: https://h2histogram.org
Structs§
- Atomic
Histogram - A histogram that uses atomic counters for each bucket.
- Atomic
Histogram32 - A histogram that uses atomic counters for each bucket.
- Bucket
- A bucket represents a quantized range of values and a count of observations that fall into that range.
- Config
- The configuration of a histogram which determines the bucketing strategy and therefore the relative error and memory utilization of a histogram.
- CumulativeRO
Histogram - A read-only, cumulative histogram for fast quantile queries.
- CumulativeRO
Histogram32 - A read-only, cumulative histogram for fast quantile queries.
- CumulativeRO
Histogram32 Ref - A borrowed view over cumulative histogram storage.
- CumulativeRO
Histogram Ref - A borrowed view over cumulative histogram storage.
- Histogram
- A histogram that uses plain counters for each bucket.
- Histogram32
- A histogram that uses plain counters for each bucket.
- Quantile
- A validated quantile value in the inclusive range
0.0..=1.0. - Quantiles
Result - The result of a quantile query on a histogram.
- Sparse
Histogram - A sparse, columnar representation of a histogram.
- Sparse
Histogram32 - A sparse, columnar representation of a histogram.
- Sparse
Histogram32 Ref - A borrowed view over sparse histogram storage.
- Sparse
Histogram Ref - A borrowed view over sparse histogram storage.
Enums§
- Error
- Errors returned for histogram construction and operations.
Traits§
- Atomic
Count - Atomic counterpart of a
Counttype. - Count
- A counter type usable for histogram bucket counts.
- Sample
Quantiles - Trait for computing quantiles from histogram data.