| 1 | //! This crate provides histogram implementations that are conceptually similar |
| 2 | //! to HdrHistogram, with modifications to the bucket construction and indexing |
| 3 | //! algorithms that we believe provide a simpler implementation and more |
| 4 | //! efficient runtime compared to the reference implementation of HdrHistogram. |
| 5 | //! |
| 6 | //! # Types |
| 7 | //! |
| 8 | //! - [`Histogram`] — standard histogram with `u64` counters. Use for |
| 9 | //! single-threaded recording and percentile queries. |
| 10 | //! - [`Histogram32`] — like [`Histogram`] but with `u32` counters. |
| 11 | //! - [`AtomicHistogram`] — atomic histogram for concurrent recording. Take a |
| 12 | //! snapshot with [`AtomicHistogram::load`] or [`AtomicHistogram::drain`] to |
| 13 | //! query percentiles. |
| 14 | //! - [`AtomicHistogram32`] — like [`AtomicHistogram`] but with `u32` counters. |
| 15 | //! - [`SparseHistogram`] — compact representation storing only non-zero |
| 16 | //! buckets. Useful for serialization and storage. |
| 17 | //! - [`SparseHistogram32`] — like [`SparseHistogram`] but with `u32` counters. |
| 18 | //! - [`CumulativeROHistogram`] — read-only histogram with cumulative counts |
| 19 | //! for fast quantile queries via binary search. |
| 20 | //! - [`CumulativeROHistogram32`] — like [`CumulativeROHistogram`] but with |
| 21 | //! `u32` counters. |
| 22 | //! |
| 23 | //! # Example |
| 24 | //! |
| 25 | //! ``` |
| 26 | //! use histogram::{Histogram, Quantile}; |
| 27 | //! |
| 28 | //! let mut h = Histogram::new(7, 64).unwrap(); |
| 29 | //! |
| 30 | //! for value in 1..=100 { |
| 31 | //! h.increment(value).unwrap(); |
| 32 | //! } |
| 33 | //! |
| 34 | //! // Quantiles use the 0.0..=1.0 scale |
| 35 | //! let r50 = h.quantile(0.5).unwrap().unwrap(); |
| 36 | //! let r99 = h.quantile(0.99).unwrap().unwrap(); |
| 37 | //! // quantile() returns Result<Option<QuantilesResult>, Error> |
| 38 | //! // outer unwrap: quantile value is valid |
| 39 | //! // inner unwrap: histogram is non-empty |
| 40 | //! |
| 41 | //! let p50 = r50.get(&Quantile::new(0.5).unwrap()).unwrap(); |
| 42 | //! let p99 = r99.get(&Quantile::new(0.99).unwrap()).unwrap(); |
| 43 | //! println!("p50: {}-{}", p50.start(), p50.end()); |
| 44 | //! println!("p99: {}-{}", p99.start(), p99.end()); |
| 45 | //! ``` |
| 46 | //! |
| 47 | //! # Counter Width |
| 48 | //! |
| 49 | //! All four histogram types ship in two flavors: |
| 50 | //! |
| 51 | //! - u64-counter family ([`Histogram`], [`AtomicHistogram`], |
| 52 | //! [`SparseHistogram`], [`CumulativeROHistogram`]): the default. |
| 53 | //! - u32-counter siblings ([`Histogram32`], [`AtomicHistogram32`], |
| 54 | //! [`SparseHistogram32`], [`CumulativeROHistogram32`]): half the memory |
| 55 | //! and serialization size; counts up to 2^32 − 1 per bucket. |
| 56 | //! |
| 57 | //! Conversions: widening (`u32` → `u64`) is infallible (`From`); narrowing |
| 58 | //! (`u64` → `u32`) is fallible (`TryFrom`, returns [`Error::Overflow`]). |
| 59 | //! Direct cross-variant + narrowing paths support the snapshot pipeline. |
| 60 | //! |
| 61 | //! # Recommended Pipeline |
| 62 | //! |
| 63 | //! Pick the histogram type based on the *role* it plays: |
| 64 | //! |
| 65 | //! - **Recording — `AtomicHistogram` or `Histogram`.** Counts are unbounded |
| 66 | //! over the lifetime of the process; `u64` is the safe choice. |
| 67 | //! - **Snapshot delta — `Histogram`, then narrowed.** Compute the delta with |
| 68 | //! `checked_sub`, then `TryFrom` into the analytics type. |
| 69 | //! - **Read-only analytics — `CumulativeROHistogram32`.** Halved size, O(log n) |
| 70 | //! quantile queries, total-count check is cheaper than per-bucket. |
| 71 | //! |
| 72 | //! ``` |
| 73 | //! use histogram::{AtomicHistogram, CumulativeROHistogram32, Histogram}; |
| 74 | //! |
| 75 | //! let recorder = AtomicHistogram::new(7, 64).unwrap(); |
| 76 | //! # let snap_t0 = recorder.load(); |
| 77 | //! let snap_t1 = recorder.load(); |
| 78 | //! let delta = snap_t1.checked_sub(&snap_t0).unwrap(); |
| 79 | //! let analytic: CumulativeROHistogram32 = |
| 80 | //! CumulativeROHistogram32::try_from(&delta).unwrap(); |
| 81 | //! ``` |
| 82 | //! |
| 83 | //! # Background |
| 84 | //! Please see: <https://h2histogram.org> |
| 85 | |
| 86 | mod atomic; |
| 87 | mod bucket; |
| 88 | mod config; |
| 89 | mod conversions; |
| 90 | mod count; |
| 91 | mod cumulative; |
| 92 | mod errors; |
| 93 | mod quantile; |
| 94 | mod sparse; |
| 95 | mod standard; |
| 96 | |
| 97 | pub use atomic::{AtomicHistogram, AtomicHistogram32}; |
| 98 | pub use bucket::Bucket; |
| 99 | pub use config::Config; |
| 100 | pub use count::{AtomicCount, Count}; |
| 101 | pub use cumulative::{ |
| 102 | CumulativeROHistogram, CumulativeROHistogram32, CumulativeROHistogram32Ref, |
| 103 | CumulativeROHistogramRef, |
| 104 | }; |
| 105 | pub use errors::Error; |
| 106 | pub use quantile::{Quantile, QuantilesResult, SampleQuantiles}; |
| 107 | pub use sparse::{SparseHistogram, SparseHistogram32, SparseHistogram32Ref, SparseHistogramRef}; |
| 108 | pub use standard::{Histogram, Histogram32}; |