bench_diff/core/
summary_stats.rs

1use hdrhistogram::Histogram;
2
3/// Alias of [`Histogram<u64>`].
4pub(crate) type Timing = Histogram<u64>;
5
6/// Constructs a [`Timing`]. The arguments correspond to [Histogram::high] and [Histogram::sigfig].
7pub(crate) fn new_timing(hist_high: u64, hist_sigfig: u8) -> Timing {
8    let mut hist = Histogram::<u64>::new_with_max(hist_high, hist_sigfig)
9        .expect("should not happen given histogram construction");
10    hist.auto(true);
11    hist
12}
13
14/// Common summary statistics useful in latency testing/benchmarking.
15///
16/// Includes sample size, mean, standard deviation, median, several percentiles, min, and max.
17#[derive(Debug, Clone)]
18pub struct SummaryStats {
19    pub count: u64,
20    pub mean: f64,
21    pub stdev: f64,
22    pub min: u64,
23    pub p1: u64,
24    pub p5: u64,
25    pub p10: u64,
26    pub p25: u64,
27    pub median: u64,
28    pub p75: u64,
29    pub p90: u64,
30    pub p95: u64,
31    pub p99: u64,
32    pub max: u64,
33}
34
35impl SummaryStats {
36    /// Computes summary statistics from the given histogram.
37    fn new(hist: &Timing) -> Self {
38        Self {
39            count: hist.len(),
40            mean: hist.mean(),
41            stdev: hist.stdev(),
42            min: hist.min(),
43            p1: hist.value_at_quantile(0.01),
44            p5: hist.value_at_quantile(0.05),
45            p10: hist.value_at_quantile(0.10),
46            p25: hist.value_at_quantile(0.25),
47            median: hist.value_at_quantile(0.50),
48            p75: hist.value_at_quantile(0.75),
49            p90: hist.value_at_quantile(0.90),
50            p95: hist.value_at_quantile(0.95),
51            p99: hist.value_at_quantile(0.99),
52            max: hist.max(),
53        }
54    }
55}
56
57/// Computes a [`SummaryStats`] from a [`Histogram<u64>`].
58pub(crate) fn summary_stats(hist: &Histogram<u64>) -> SummaryStats {
59    SummaryStats::new(hist)
60}