diff --git a/src/Prometheus/Math.php b/src/Prometheus/Math.php index 3b3029d6..f9ae146f 100644 --- a/src/Prometheus/Math.php +++ b/src/Prometheus/Math.php @@ -21,14 +21,11 @@ public function quantile(array $arr, float $q): float return 0; } - $allindex = ($count - 1) * $q; - $intvalindex = (int) $allindex; - $floatval = $allindex - $intvalindex; - if ($count > $intvalindex + 1) { - $result = $floatval * ($arr[$intvalindex + 1] - $arr[$intvalindex]) + $arr[$intvalindex]; - } else { - $result = $arr[$intvalindex]; + $j = floor($count * $q); + $r = $count * $q - $j; + if (0.0 === $r) { + return $arr[$j - 1]; } - return $result; + return $arr[$j]; } } diff --git a/tests/Test/BlackBoxTest.php b/tests/Test/BlackBoxTest.php index ab6c82e3..fd451792 100644 --- a/tests/Test/BlackBoxTest.php +++ b/tests/Test/BlackBoxTest.php @@ -176,11 +176,11 @@ public function summariesShouldIncrementAtomically(): void $body = (string)$metricsResult->getBody(); self::assertThat($body, self::stringContains(<< 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.1], - 'value' => 1.9, + 'value' => 1, ], [ 'name' => 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.5], - 'value' => 5.5, + 'value' => 5, ], [ 'name' => 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.9], - 'value' => 9.1, + 'value' => 9, ], [ 'name' => 'test_some_metric_count', @@ -446,19 +446,19 @@ public function itShouldIgnoreSerieItemsOlderThanMaxAgeSeconds(): void 'name' => 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.1], - 'value' => 1.9, + 'value' => 1, ], [ 'name' => 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.5], - 'value' => 5.5, + 'value' => 5, ], [ 'name' => 'test_some_metric', 'labelNames' => ['quantile'], 'labelValues' => [0.9], - 'value' => 9.1, + 'value' => 9, ], [ 'name' => 'test_some_metric_count', diff --git a/tests/Test/Prometheus/MathTest.php b/tests/Test/Prometheus/MathTest.php index 1f35ae22..0980845e 100644 --- a/tests/Test/Prometheus/MathTest.php +++ b/tests/Test/Prometheus/MathTest.php @@ -30,9 +30,9 @@ public function providerQuantileSuccess(): array { return [ 'Even serie' => [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5], - 'Odd serie' => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5.5], + 'Odd serie' => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5], 'Even float serie' => [[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10], 0.5, 0.5], - 'Odd float serie' => [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10], 0.5, 0.55], + 'Odd float serie' => [[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.10], 0.5, 0.5], 'Empty serie' => [[], 0.5, 0], ]; }