Loading problem...
In production systems, understanding response time distributions is crucial for maintaining service quality and meeting Service Level Objectives (SLOs). Unlike average latency, which can be skewed by outliers, percentile metrics provide a more accurate picture of user experience and system health.
Percentiles divide a sorted dataset into 100 equal parts. The P-th percentile indicates the value below which P% of the observations fall. In the context of system monitoring:
Percentile Calculation with Linear Interpolation:
For a sorted dataset of n values and a target percentile p (expressed as a fraction between 0 and 1):
$$percentile = data[lower] + (rank - lower) \times (data[upper] - data[lower])$$
Your Task: Write a Python function that computes the P50, P95, and P99 latency percentiles from a collection of observed response times. The function should:
{'P50': 0.0, 'P95': 0.0, 'P99': 0.0} if the input list is emptylatencies = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]{'P50': 55.0, 'P95': 95.5, 'P99': 99.1}With 10 uniformly distributed latency measurements (10ms to 100ms in 10ms intervals):
• P50 Calculation: Position = 0.50 × 9 = 4.5. This falls between indices 4 (value 50) and 5 (value 60). Interpolating: 50 + 0.5 × (60 - 50) = 55.0ms
• P95 Calculation: Position = 0.95 × 9 = 8.55. This falls between indices 8 (value 90) and 9 (value 100). Interpolating: 90 + 0.55 × (100 - 90) = 95.5ms
• P99 Calculation: Position = 0.99 × 9 = 8.91. This falls between indices 8 (value 90) and 9 (value 100). Interpolating: 90 + 0.91 × (100 - 90) = 99.1ms
This indicates that 50% of requests complete within 55ms, 95% within 95.5ms, and 99% within 99.1ms.
latencies = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]{'P50': 10.5, 'P95': 19.05, 'P99': 19.81}A 20-sample dataset spanning 1ms to 20ms:
• P50: Position = 0.50 × 19 = 9.5 → Interpolate between indices 9 and 10 (values 10 and 11): 10 + 0.5 × 1 = 10.5ms
• P95: Position = 0.95 × 19 = 18.05 → Interpolate between indices 18 and 19 (values 19 and 20): 19 + 0.05 × 1 = 19.05ms
• P99: Position = 0.99 × 19 = 18.81 → Interpolate between indices 18 and 19: 19 + 0.81 × 1 = 19.81ms
latencies = [50, 10, 90, 30, 70, 20, 80, 40, 60, 100]{'P50': 55.0, 'P95': 95.5, 'P99': 99.1}The input latencies are unsorted, simulating real-world data collection. The function must first sort the values: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. After sorting, the calculations proceed identically to Example 1, demonstrating that percentiles are order-invariant—they depend only on the distribution of values, not their arrival sequence.
Constraints