mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-14 01:42:30 +00:00
fix: calculate conservative SLO percentiles
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package observability
|
package observability
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -52,7 +53,13 @@ func percentile(values []time.Duration, p float64) time.Duration {
|
|||||||
}
|
}
|
||||||
ordered := append([]time.Duration(nil), values...)
|
ordered := append([]time.Duration(nil), values...)
|
||||||
sort.Slice(ordered, func(i, j int) bool { return ordered[i] < ordered[j] })
|
sort.Slice(ordered, func(i, j int) bool { return ordered[i] < ordered[j] })
|
||||||
index := int(float64(len(ordered)-1) * p)
|
index := int(math.Ceil(p*float64(len(ordered)))) - 1
|
||||||
|
if index < 0 {
|
||||||
|
index = 0
|
||||||
|
}
|
||||||
|
if index >= len(ordered) {
|
||||||
|
index = len(ordered) - 1
|
||||||
|
}
|
||||||
return ordered[index]
|
return ordered[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,3 +25,9 @@ func TestEvaluateSLODoesNotInventFailureForEmptyOptionalWindows(t *testing.T) {
|
|||||||
t.Fatalf("empty window violations = %+v", violations)
|
t.Fatalf("empty window violations = %+v", violations)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPercentileUsesConservativeNearestRankForSmallWindows(t *testing.T) {
|
||||||
|
if got := percentile([]time.Duration{time.Millisecond, 101 * time.Millisecond}, .95); got != 101*time.Millisecond {
|
||||||
|
t.Fatalf("p95 underreported small window: %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user