rust 37 lines · 6 steps

Computing latency percentiles in Rust

Sort a batch of duration samples once, then interpolate any percentile from the ranked list.

Explained by highlit
1use std::time::Duration;
2 
3#[derive(Debug, Clone, Copy)]
4pub struct LatencyStats {
5 pub p50: Duration,
6 pub p90: Duration,
7 pub p95: Duration,
8 pub p99: Duration,
9 pub max: Duration,
10}
11 
12pub fn compute_latency_stats(mut samples: Vec<Duration>) -> Option<LatencyStats> {
13 if samples.is_empty() {
14 return None;
15 }
16 
17 samples.sort_unstable();
18 
19 let percentile = |p: f64| -> Duration {
20 let rank = p / 100.0 * (samples.len() - 1) as f64;
21 let lower = rank.floor() as usize;
22 let upper = rank.ceil() as usize;
23 let weight = rank - lower as f64;
24 
25 let lo = samples[lower].as_secs_f64();
26 let hi = samples[upper].as_secs_f64();
27 Duration::from_secs_f64(lo + (hi - lo) * weight)
28 };
29 
30 Some(LatencyStats {
31 p50: percentile(50.0),
32 p90: percentile(90.0),
33 p95: percentile(95.0),
34 p99: percentile(99.0),
35 max: *samples.last().unwrap(),
36 })
37}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Sorting the samples once up front lets every percentile be a cheap index lookup instead of a rescan.
  2. 2Linear interpolation between neighboring ranks gives smooth estimates even when a percentile falls between two samples.
  3. 3Returning Option forces callers to handle the empty-input case instead of hitting a panic.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Computing latency percentiles in Rust — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code