← All Exercises

Report Latency Percentiles from Raw Samples

basicjavascript~20min

Background

Averages understate tail pain. Percentiles answer "how bad is it for the slowest users" and are the values peers and SLOs are written against.

Problem

Compute p50, p95, p99 and the mean from latency samples using nearest-rank, so tail latency is reported without hiding outliers in an average.

Input

JSON object: { latencies: number[] } (milliseconds)

Output

JSON object: { p50, p95, p99, mean }

Sample tests

Sample 1

Input: {"latencies":[10,20,30,40,50,60,70,80,90,100]}
Expected: {"p50":50,"p95":100,"p99":100,"mean":55}

Sample 2

Input: {"latencies":[5,15,25,35,45,55,65,75,85,95,105,115,125,135,145,155,165,175,185,195]}
Expected: {"p50":95,"p95":185,"p99":195,"mean":100}

Your solution

Learning objective

Report tail latency with a method that matches how SLOs are written.