How p95 can miss the slowest request
Sort twenty request times.
An API can have a low p95 while a few requests are much slower. Percentiles give you a position in a distribution. They do not give you the average wait or the time every caller experienced. To see the difference, build a batch of twenty requests and sort them by latency.
Using the nearest-rank convention, multiply the batch size by the percentile as a fraction and round up. That is a one-based rank, so subtract one to index a Python list. For twenty requests, p95 selects the nineteenth value, and p99 selects the twentieth. In this batch those are 20 ms and 800 ms.
Try changing one more request from 20 to 800 ms. Now the nineteenth value is 800 ms too. The same metric jumps because the count of slow requests crosses the cutoff. Small samples also make the high percentiles sensitive to just one or two events. Different tools use different interpolation rules, so compare results only after checking the convention.
A useful latency report records the number of requests, the time window, the endpoint, and how timeouts are counted. Look at the fraction above a threshold if the actual objective is that requests finish within a specified time. When comparing services or time windows, combine their underlying distributions when possible. Averaging reported percentiles can produce a number that has no such interpretation.
Run the example
from math import ceil
latencies = [20] * 19 + [800]
def nearest_rank(values, p):
ordered = sorted(values)
rank = ceil(p * len(ordered))
return ordered[rank - 1]
for p in (0.95, 0.99):
print(p, nearest_rank(latencies, p))
Expected output
0.95 20
0.99 800
Reference: https://www.itl.nist.gov/div898/handbook/prc/section2/prc262.htm
The team guide · My original notes on testing coding agents · Working with Lightbulb Partners