Skip to content

Commit

Permalink
change quantile calculation implementation (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Perret <[email protected]>
  • Loading branch information
mp3000mp authored Dec 17, 2021
1 parent 7c998b3 commit cb7ee99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
13 changes: 5 additions & 8 deletions src/Prometheus/Math.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
10 changes: 5 additions & 5 deletions tests/Test/BlackBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public function summariesShouldIncrementAtomically(): void
$body = (string)$metricsResult->getBody();

self::assertThat($body, self::stringContains(<<<EOF
test_some_summary{type="blue",quantile="0.01"} 1.09
test_some_summary{type="blue",quantile="0.05"} 1.45
test_some_summary{type="blue",quantile="0.5"} 5.5
test_some_summary{type="blue",quantile="0.95"} 9.55
test_some_summary{type="blue",quantile="0.99"} 9.91
test_some_summary{type="blue",quantile="0.01"} 1
test_some_summary{type="blue",quantile="0.05"} 1
test_some_summary{type="blue",quantile="0.5"} 5
test_some_summary{type="blue",quantile="0.95"} 10
test_some_summary{type="blue",quantile="0.99"} 10
test_some_summary_count{type="blue"} 10
test_some_summary_sum{type="blue"} 55
EOF
Expand Down
12 changes: 6 additions & 6 deletions tests/Test/Prometheus/AbstractSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ public function itShouldObserveComputeMedianCorrectlyWithEvenSerieLength(): 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',
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions tests/Test/Prometheus/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}
Expand Down

0 comments on commit cb7ee99

Please sign in to comment.