Lesson 1 · Unit 02
Percentiles
p50, p95 and p99 from samples, why averages mislead, and what histogram buckets cannot tell you.
What you practise
- P50, p95 and p99 computed from samples, why averages mislead, histogram buckets and what they cannot tell you
Try a question from this lesson
Pick one
Ten requests, latency in ms. The code prints the average, 200.0. How many of the ten users waited roughly 200 ms?
ms = [20, 22, 21, 23, 20,
24, 22, 21, 23, 1804]
print(sum(ms) / len(ms))Hint
Look for a value near 200 in the list.
Correct answer
None of them
Why
Nine users waited about 22 ms and one waited 1.8 seconds. The average, 200 ms, describes nobody: it is ten times too slow for nine users and nine times too kind to the tenth.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Percentiles by nearest rank
A percentile answers "how slow was the request that p% of requests beat or matched?".
The rule used here is nearest rank. Sort the samples. The rank is p / 100 x n, rounded up. The percentile is the value at that rank, counting from 1.
For ten samples: p50 is the 5th value, p90 the 9th, and p99 rounds 9.9 up to the 10th.
import math
ms = sorted([20, 22, 21, 23, 20,
24, 22, 21, 23, 1804])
def pct(p):
rank = math.ceil(p * len(ms) / 100)
return ms[rank - 1]
print(pct(50), pct(90), pct(99))Concept
Histogram buckets
A busy service cannot keep every sample. It keeps a histogram: a few counters, one per upper bound. Each counts the requests that took that long or less, so the counts are cumulative and the last bucket, with no bound, holds them all.
Percentiles are then estimated from the counts.
request duration, 1000 requests
at most (ms) count
50 500
100 900
250 980
1000 1000
no bound 100010 more exercises in the app
- 5× type the answer
- 1× order the lines
- 1× true or false
- 3× multiple choice
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.