DEV Community

Anh Trần Tuấn
Anh Trần Tuấn

Posted on • Originally published at tuanh.net on

Statistics Behind Latency Metrics: Understanding P90, P95, and P99

1. What Are Latency Percentiles?

Image

Latency percentiles, such as P90, P95, and P99, are statistical measures that indicate how response times are distributed. They provide a clearer picture of system performance by showing the response times at different percentiles.

P90 (90th Percentile): This value indicates that 90% of the requests have a response time less than or equal to this value. It helps identify the performance experienced by the majority of users, excluding the top 10% of slowest responses.

P95 (95th Percentile): This metric shows that 95% of the requests have a response time less than or equal to this value. It provides a view of the performance experienced by almost all users, excluding the top 5% of slowest responses.

P99 (99th Percentile): The P99 metric means that 99% of the requests have a response time less than or equal to this value. It focuses on the slowest 1% of responses, highlighting the worst performance outliers.

Image

Average response times can be misleading, especially in systems with high variability. Latency percentiles provide a more accurate representation of user experience by accounting for the distribution of response times. This is crucial for identifying performance bottlenecks and ensuring that even the slowest responses are within acceptable limits.

2. Calculating Latency Percentiles

To calculate these percentiles, you need to collect response time data and sort it. Here’s a step-by-step guide and a sample code to calculate P90, P95, and P99.

2.1 Collecting and Sorting Data

Gather Response Times : Collect a list of response times for your system. For example, you might gather these times from logs or monitoring tools.

Sort Data : Arrange the response times in ascending order.

Calculate Percentiles : Use the following formula to find the percentile value:

Image

where 𝑃 is the percentile (e.g., 90 for P90), and 𝑁 N is the number of data points.

2.2 Sample Code for Calculation

Here’s a Java code snippet that demonstrates how to calculate these percentiles:

import java.util.Arrays;

public class LatencyMetrics {

    public static void main(String[] args) {
        // Example response times in milliseconds
        int[] responseTimes = {120, 150, 200, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000};

        // Sort the response times
        Arrays.sort(responseTimes);

        // Calculate percentiles
        System.out.println("P90: " + calculatePercentile(responseTimes, 90));
        System.out.println("P95: " + calculatePercentile(responseTimes, 95));
        System.out.println("P99: " + calculatePercentile(responseTimes, 99));
    }

    public static int calculatePercentile(int[] sortedData, int percentile) {
        double index = (percentile / 100.0) * (sortedData.length + 1);
        if (index == sortedData.length) {
            return sortedData[sortedData.length - 1];
        }
        int lowerIndex = (int) Math.floor(index) - 1;
        int upperIndex = (int) Math.ceil(index) - 1;
        if (lowerIndex == upperIndex) {
            return sortedData[lowerIndex];
        }
        double fraction = index - Math.floor(index);
        return (int) Math.round(sortedData[lowerIndex] + fraction * (sortedData[upperIndex] - sortedData[lowerIndex]));
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Interpreting Latency Metrics

Once you have calculated P90, P95, and P99, interpreting these values will help you understand user experience and performance.

P90 Value : This value should be used to gauge the typical user experience. If P90 is high, it means that a significant portion of users is experiencing delays.

P95 Value : This provides insight into the performance experienced by nearly all users. High P95 values can indicate systemic issues affecting most users.

P99 Value : This highlights the worst-case scenarios. High P99 values point to performance issues that need addressing to avoid affecting a small percentage of users severely.

4. Conclusion

Latency metrics such as P90, P95, and P99 are crucial for understanding and improving the performance of your system. By calculating and interpreting these percentiles, you can gain valuable insights into how your system performs under various conditions and identify areas for improvement.

If you have any questions or need further clarification, feel free to leave a comment below!

Read posts more at : Statistics Behind Latency Metrics: Understanding P90, P95, and P99

Top comments (0)