Debian Codegraph @

src/lib.rs

raw symbols SCIP
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 atomicrust-analyzer cargo histogram 1.5.0 atomic/;
87 mod bucketrust-analyzer cargo histogram 1.5.0 bucket/;
88 mod configrust-analyzer cargo histogram 1.5.0 config/;
89 mod conversionsrust-analyzer cargo histogram 1.5.0 conversions/;
90 mod countrust-analyzer cargo histogram 1.5.0 count/;
91 mod cumulativerust-analyzer cargo histogram 1.5.0 cumulative/;
92 mod errorsrust-analyzer cargo histogram 1.5.0 errors/;
93 mod quantilerust-analyzer cargo histogram 1.5.0 quantile/;
94 mod sparserust-analyzer cargo histogram 1.5.0 sparse/;
95 mod standardrust-analyzer cargo histogram 1.5.0 standard/;
96
97 pub use atomicrust-analyzer cargo histogram 1.5.0 atomic/::{AtomicHistogramrust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram#, AtomicHistogram32rust-analyzer cargo histogram 1.5.0 atomic/AtomicHistogram32#};
98 pub use bucketrust-analyzer cargo histogram 1.5.0 bucket/::Bucketrust-analyzer cargo histogram 1.5.0 bucket/Bucket#;
99 pub use configrust-analyzer cargo histogram 1.5.0 config/::Configrust-analyzer cargo histogram 1.5.0 config/Config#;
100 pub use countrust-analyzer cargo histogram 1.5.0 count/::{AtomicCountrust-analyzer cargo histogram 1.5.0 count/AtomicCount#, Countrust-analyzer cargo histogram 1.5.0 count/Count#};
101 pub use cumulativerust-analyzer cargo histogram 1.5.0 cumulative/::{
102 CumulativeROHistogramrust-analyzer cargo histogram 1.5.0 cumulative/CumulativeROHistogram#, CumulativeROHistogram32rust-analyzer cargo histogram 1.5.0 cumulative/CumulativeROHistogram32#, CumulativeROHistogram32Refrust-analyzer cargo histogram 1.5.0 cumulative/CumulativeROHistogram32Ref#,
103 CumulativeROHistogramRefrust-analyzer cargo histogram 1.5.0 cumulative/CumulativeROHistogramRef#,
104 };
105 pub use errorsrust-analyzer cargo histogram 1.5.0 errors/::Errorrust-analyzer cargo histogram 1.5.0 errors/Error#;
106 pub use quantilerust-analyzer cargo histogram 1.5.0 quantile/::{Quantilerust-analyzer cargo histogram 1.5.0 quantile/Quantile#, QuantilesResultrust-analyzer cargo histogram 1.5.0 quantile/QuantilesResult#, SampleQuantilesrust-analyzer cargo histogram 1.5.0 quantile/SampleQuantiles#};
107 pub use sparserust-analyzer cargo histogram 1.5.0 sparse/::{SparseHistogramrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram#, SparseHistogram32rust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram32#, SparseHistogram32Refrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogram32Ref#, SparseHistogramRefrust-analyzer cargo histogram 1.5.0 sparse/SparseHistogramRef#};
108 pub use standardrust-analyzer cargo histogram 1.5.0 standard/::{Histogramrust-analyzer cargo histogram 1.5.0 standard/Histogram#, Histogram32rust-analyzer cargo histogram 1.5.0 standard/Histogram32#};