Class PercentileCalculator

java.lang.Object
com.nmontytskyi.monitoring.detector.PercentileCalculator

public final class PercentileCalculator extends Object
Utility class for calculating response time percentiles.

Percentile P95 = 340ms means that 95% of requests completed faster than 340ms, while 5% were slower. This is a more accurate indicator than the average, which hides "tail" latencies that affect real user experience.

Implements the Nearest Rank Method:

   index  = ceil(p / 100 * n) - 1
   result = sortedValues[index]
 

This is a utility class (static methods only) and cannot be instantiated.

  • Method Details

    • calculate

      public static long calculate(List<Long> values, int percentile)
      Calculates the given percentile for a list of response time values.
      Parameters:
      values - list of measurements in milliseconds (does not need to be sorted)
      percentile - target percentile from 0 to 100 inclusive
      Returns:
      the percentile value; 0 if the list is empty
      Throws:
      IllegalArgumentException - if percentile is outside the range [0, 100]
    • p50

      public static long p50(List<Long> values)
      Calculates the median (P50) — 50% of requests completed faster than this value.
      Parameters:
      values - list of measurements in milliseconds
      Returns:
      median or 0 if the list is empty
    • p95

      public static long p95(List<Long> values)
      Calculates P95 — 95% of requests completed faster than this value. Standard indicator for API performance evaluation.
      Parameters:
      values - list of measurements in milliseconds
      Returns:
      P95 or 0 if the list is empty
    • p99

      public static long p99(List<Long> values)
      Calculates P99 — 99% of requests completed faster than this value. Represents "tail latency" — the experience of the slowest 1% of requests.
      Parameters:
      values - list of measurements in milliseconds
      Returns:
      P99 or 0 if the list is empty
    • calculateAll

      public static PercentileCalculator.PercentileStats calculateAll(List<Long> values)
      Calculates P50, P95, and P99 in a single sorting pass. Recommended method for populating SlaReport.
      Parameters:
      values - list of measurements in milliseconds
      Returns:
      object containing all three percentiles; all zeros if the list is empty