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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Sorting the samples once up front lets every percentile be a cheap index lookup instead of a rescan.
- 2Linear interpolation between neighboring ranks gives smooth estimates even when a percentile falls between two samples.
- 3Returning Option forces callers to handle the empty-input case instead of hitting a panic.
Related explainers
rust
use std::time::Duration; #[derive(Debug, PartialEq)] pub enum ParseDurationError {
Parsing duration strings safely in Rust
parsing
error-handling
checked-arithmetic
Intermediate
8 steps
rust
use std::time::{Duration, Instant}; pub struct Ewma { alpha: f64,
A time-decayed moving average in Rust
exponential-smoothing
time-decay
state-machine
Intermediate
8 steps
typescript
type Countdown = { days: number; hours: number; minutes: number;
Building a self-stopping countdown timer
date-math
closures
timers
Intermediate
9 steps
rust
use std::fs::File; use std::io::{Read, Seek, SeekFrom}; #[derive(Debug)]
Parsing an HTTP Range header in Rust
http-range
parsing
file-io
Intermediate
10 steps
rust
use rand::distributions::{Alphanumeric, DistString}; use rand::rngs::OsRng; #[derive(Debug, Clone, PartialEq, Eq)]
A newtype wrapper for API tokens in Rust
newtype-pattern
randomness
encapsulation
Intermediate
6 steps
rust
pub fn normalize_path(input: &str) -> String { let is_absolute = input.starts_with('/'); let has_trailing_slash = input.len() > 1 && input.ends_with('/'); let mut stack: Vec<&str> = Vec::new();
Normalizing filesystem paths in Rust
string-processing
stack
path-manipulation
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/computing-latency-percentiles-in-rust-explained-rust-ea18/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.