Skip to content

Commit cb7ee99

Browse files
authored
change quantile calculation implementation (#81)
Signed-off-by: Matthias Perret <moussadedijon@gmail.com>
1 parent 7c998b3 commit cb7ee99

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

src/Prometheus/Math.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ public function quantile(array $arr, float $q): float
2121
return 0;
2222
}
2323

24-
$allindex = ($count - 1) * $q;
25-
$intvalindex = (int) $allindex;
26-
$floatval = $allindex - $intvalindex;
27-
if ($count > $intvalindex + 1) {
28-
$result = $floatval * ($arr[$intvalindex + 1] - $arr[$intvalindex]) + $arr[$intvalindex];
29-
} else {
30-
$result = $arr[$intvalindex];
24+
$j = floor($count * $q);
25+
$r = $count * $q - $j;
26+
if (0.0 === $r) {
27+
return $arr[$j - 1];
3128
}
32-
return $result;
29+
return $arr[$j];
3330
}
3431
}

tests/Test/BlackBoxTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ public function summariesShouldIncrementAtomically(): void
176176
$body = (string)$metricsResult->getBody();
177177

178178
self::assertThat($body, self::stringContains(<<<EOF
179-
test_some_summary{type="blue",quantile="0.01"} 1.09
180-
test_some_summary{type="blue",quantile="0.05"} 1.45
181-
test_some_summary{type="blue",quantile="0.5"} 5.5
182-
test_some_summary{type="blue",quantile="0.95"} 9.55
183-
test_some_summary{type="blue",quantile="0.99"} 9.91
179+
test_some_summary{type="blue",quantile="0.01"} 1
180+
test_some_summary{type="blue",quantile="0.05"} 1
181+
test_some_summary{type="blue",quantile="0.5"} 5
182+
test_some_summary{type="blue",quantile="0.95"} 10
183+
test_some_summary{type="blue",quantile="0.99"} 10
184184
test_some_summary_count{type="blue"} 10
185185
test_some_summary_sum{type="blue"} 55
186186
EOF

tests/Test/Prometheus/AbstractSummaryTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,19 @@ public function itShouldObserveComputeMedianCorrectlyWithEvenSerieLength(): void
277277
'name' => 'test_some_metric',
278278
'labelNames' => ['quantile'],
279279
'labelValues' => [0.1],
280-
'value' => 1.9,
280+
'value' => 1,
281281
],
282282
[
283283
'name' => 'test_some_metric',
284284
'labelNames' => ['quantile'],
285285
'labelValues' => [0.5],
286-
'value' => 5.5,
286+
'value' => 5,
287287
],
288288
[
289289
'name' => 'test_some_metric',
290290
'labelNames' => ['quantile'],
291291
'labelValues' => [0.9],
292-
'value' => 9.1,
292+
'value' => 9,
293293
],
294294
[
295295
'name' => 'test_some_metric_count',
@@ -446,19 +446,19 @@ public function itShouldIgnoreSerieItemsOlderThanMaxAgeSeconds(): void
446446
'name' => 'test_some_metric',
447447
'labelNames' => ['quantile'],
448448
'labelValues' => [0.1],
449-
'value' => 1.9,
449+
'value' => 1,
450450
],
451451
[
452452
'name' => 'test_some_metric',
453453
'labelNames' => ['quantile'],
454454
'labelValues' => [0.5],
455-
'value' => 5.5,
455+
'value' => 5,
456456
],
457457
[
458458
'name' => 'test_some_metric',
459459
'labelNames' => ['quantile'],
460460
'labelValues' => [0.9],
461-
'value' => 9.1,
461+
'value' => 9,
462462
],
463463
[
464464
'name' => 'test_some_metric_count',

tests/Test/Prometheus/MathTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function providerQuantileSuccess(): array
3030
{
3131
return [
3232
'Even serie' => [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5],
33-
'Odd serie' => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5.5],
33+
'Odd serie' => [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 0.5, 5],
3434
'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],
35-
'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],
35+
'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],
3636
'Empty serie' => [[], 0.5, 0],
3737
];
3838
}

0 commit comments

Comments
 (0)