From c941e02c0cf9f14728a1fc91c9edb9a0cc70f3b9 Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Fri, 23 Oct 2026 23:06:52 -0400 Subject: [PATCH] fix(usage): use nearest-rank for latency percentiles --- internal/runtime/usage_aggregate.go | 5 ++++- .../runtime/usage_percentile_rank_test.go | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 internal/runtime/usage_percentile_rank_test.go diff --git a/internal/runtime/usage_aggregate.go b/internal/runtime/usage_aggregate.go index 35e797165..beb4986a7 100644 --- a/internal/runtime/usage_aggregate.go +++ b/internal/runtime/usage_aggregate.go @@ -1,6 +1,7 @@ package runtime import ( + "math" "sort" "strings" "sync" @@ -119,7 +120,9 @@ func (t *ToolUsage) Percentile(p float64) (ms int64, exceeds bool) { if total == 0 { return 0, false } - target := int64(float64(total) * p) + // Nearest-rank: truncating would land one rank low for small totals and + // hide the slow tail (9 fast calls + 1 slow one would report p95 as fast). + target := int64(math.Ceil(float64(total) * p)) if target < 1 { target = 1 } diff --git a/internal/runtime/usage_percentile_rank_test.go b/internal/runtime/usage_percentile_rank_test.go new file mode 100644 index 000000000..430a6c075 --- /dev/null +++ b/internal/runtime/usage_percentile_rank_test.go @@ -0,0 +1,20 @@ +package runtime + +import "testing" + +// Nearest-rank p95 of 10 calls is the 10th fastest (ceil(9.5)); truncating to +// rank 9 reported nine fast calls plus one 3 s call as a ~30 ms p95. +func TestToolUsagePercentile_SmallTotalsUseNearestRank(t *testing.T) { + tu := &ToolUsage{LatencyBuckets: make([]int64, len(latencyBucketBoundsMs)+1)} + for i := 0; i < 9; i++ { + tu.LatencyBuckets[latencyBucketIndex(30)]++ + } + tu.LatencyBuckets[latencyBucketIndex(3000)]++ + + if got, _ := tu.Percentile(0.95); got != 5000 { + t.Errorf("p95 = %d ms, want 5000 (bucket holding the 3000 ms call)", got) + } + if got, _ := tu.Percentile(0.50); got != 50 { + t.Errorf("p50 = %d ms, want 50", got) + } +}