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 serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 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.