synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / src / stats.rs
| 1 | //! Latency percentiles (nearest-rank) and across-repetition spreads. |
| 2 | |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | /// Minimum sample count for reporting a p95 value (smaller samples report `null`). |
| 6 | pub const MIN_SAMPLES_P95: usize = 20; |
| 7 | /// Minimum sample count for reporting a p99 value (smaller samples report `null`). |
| 8 | pub const MIN_SAMPLES_P99: usize = 100; |
| 9 | |
| 10 | /// Per-cell latency summary in milliseconds (three decimals, microsecond resolution). |
| 11 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 12 | pub struct LatencySummary { |
| 13 | pub n: usize, |
| 14 | pub min_ms: f64, |
| 15 | pub p50_ms: f64, |
| 16 | /// `None` when `n < MIN_SAMPLES_P95`. |
| 17 | pub p95_ms: Option<f64>, |
| 18 | /// `None` when `n < MIN_SAMPLES_P99`. |
| 19 | pub p99_ms: Option<f64>, |
| 20 | pub max_ms: f64, |
| 21 | pub mean_ms: f64, |
| 22 | } |
| 23 | |
| 24 | /// Nearest-rank percentile over an ascending slice: the value at 1-based rank |
| 25 | /// `ceil(q * n)`. Returns `None` for an empty slice or `q` outside `(0, 1]`. |
| 26 | pub fn nearest_rank(sorted: &[u64], q: f64) -> Option<u64> { |
| 27 | if sorted.is_empty() || !(q > 0.0 && q <= 1.0) { |
| 28 | return None; |
| 29 | } |
| 30 | let rank = (q * sorted.len() as f64).ceil() as usize; |
| 31 | Some(sorted[rank.clamp(1, sorted.len()) - 1]) |
| 32 | } |
| 33 | |
| 34 | /// Round to three decimals. |
| 35 | pub fn round3(x: f64) -> f64 { |
| 36 | (x * 1000.0).round() / 1000.0 |
| 37 | } |
| 38 | |
| 39 | fn us_to_ms(us: u64) -> f64 { |
| 40 | round3(us as f64 / 1000.0) |
| 41 | } |
| 42 | |
| 43 | /// Summarize microsecond samples. Returns `None` when there are no samples. |
| 44 | pub fn summarize_us(samples_us: &[u64]) -> Option<LatencySummary> { |
| 45 | if samples_us.is_empty() { |
| 46 | return None; |
| 47 | } |
| 48 | let mut s = samples_us.to_vec(); |
| 49 | s.sort_unstable(); |
| 50 | let n = s.len(); |
| 51 | let sum: u128 = s.iter().map(|&v| u128::from(v)).sum(); |
| 52 | Some(LatencySummary { |
| 53 | n, |
| 54 | min_ms: us_to_ms(s[0]), |
| 55 | p50_ms: us_to_ms(nearest_rank(&s, 0.50)?), |
| 56 | p95_ms: if n >= MIN_SAMPLES_P95 { |
| 57 | nearest_rank(&s, 0.95).map(us_to_ms) |
| 58 | } else { |
| 59 | None |
| 60 | }, |
| 61 | p99_ms: if n >= MIN_SAMPLES_P99 { |
| 62 | nearest_rank(&s, 0.99).map(us_to_ms) |
| 63 | } else { |
| 64 | None |
| 65 | }, |
| 66 | max_ms: us_to_ms(s[n - 1]), |
| 67 | mean_ms: round3(sum as f64 / n as f64 / 1000.0), |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | /// Median, minimum, and maximum of one metric across repetitions. |
| 72 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 73 | pub struct Spread { |
| 74 | pub median: f64, |
| 75 | pub min: f64, |
| 76 | pub max: f64, |
| 77 | /// Number of repetitions that contributed a value. |
| 78 | pub reps: usize, |
| 79 | } |
| 80 | |
| 81 | /// Spread over finite values (median of an even count is the mean of the two middle values). |
| 82 | pub fn spread(values: &[f64]) -> Option<Spread> { |
| 83 | let mut v: Vec<f64> = values.iter().copied().filter(|x| x.is_finite()).collect(); |
| 84 | if v.is_empty() { |
| 85 | return None; |
| 86 | } |
| 87 | v.sort_by(|a, b| a.total_cmp(b)); |
| 88 | let n = v.len(); |
| 89 | let median = if n % 2 == 1 { |
| 90 | v[n / 2] |
| 91 | } else { |
| 92 | (v[n / 2 - 1] + v[n / 2]) / 2.0 |
| 93 | }; |
| 94 | Some(Spread { |
| 95 | median: round3(median), |
| 96 | min: round3(v[0]), |
| 97 | max: round3(v[n - 1]), |
| 98 | reps: n, |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |
| 104 | use super::*; |
| 105 | |
| 106 | #[test] |
| 107 | fn nearest_rank_matches_definition() { |
| 108 | let s: Vec<u64> = (1..=100).collect(); |
| 109 | assert_eq!(nearest_rank(&s, 0.50), Some(50)); |
| 110 | assert_eq!(nearest_rank(&s, 0.95), Some(95)); |
| 111 | assert_eq!(nearest_rank(&s, 0.99), Some(99)); |
| 112 | assert_eq!(nearest_rank(&s, 1.0), Some(100)); |
| 113 | assert_eq!(nearest_rank(&[7], 0.5), Some(7)); |
| 114 | assert_eq!(nearest_rank(&[], 0.5), None); |
| 115 | assert_eq!(nearest_rank(&s, 0.0), None); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn summary_withholds_tail_percentiles_for_small_samples() { |
| 120 | let small: Vec<u64> = (1..=19).map(|v| v * 1000).collect(); |
| 121 | let sum = summarize_us(&small).unwrap(); |
| 122 | assert_eq!(sum.n, 19); |
| 123 | assert_eq!(sum.p95_ms, None); |
| 124 | assert_eq!(sum.p99_ms, None); |
| 125 | assert_eq!(sum.p50_ms, 10.0); |
| 126 | |
| 127 | let big: Vec<u64> = (1..=100).map(|v| v * 1000).collect(); |
| 128 | let sum = summarize_us(&big).unwrap(); |
| 129 | assert_eq!(sum.p95_ms, Some(95.0)); |
| 130 | assert_eq!(sum.p99_ms, Some(99.0)); |
| 131 | assert_eq!(sum.mean_ms, 50.5); |
| 132 | assert!(summarize_us(&[]).is_none()); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn spread_reports_median_min_max() { |
| 137 | let s = spread(&[3.0, 1.0, 2.0]).unwrap(); |
| 138 | assert_eq!((s.median, s.min, s.max, s.reps), (2.0, 1.0, 3.0, 3)); |
| 139 | let s = spread(&[4.0, 1.0]).unwrap(); |
| 140 | assert_eq!(s.median, 2.5); |
| 141 | assert!(spread(&[f64::NAN]).is_none()); |
| 142 | } |
| 143 | } |