Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Stop timing the one-cycle NormalizeRoute root fast path

rcourtmanf86711413fa4
diff --git a/internal/api/http_metrics_bench_test.go b/internal/api/http_metrics_bench_test.go
index d11f00a32b..5d2e3fc813 100644
--- a/internal/api/http_metrics_bench_test.go
+++ b/internal/api/http_metrics_bench_test.go
@@ -13,6 +13,12 @@ import (
// BenchmarkNormalizeRoute measures route normalization with representative
// API paths of varying complexity.
+//
+// The "/" fast path is deliberately not timed here. Paired CI runs measured it
+// at either 2.18ns or 2.50ns, about one CPU cycle apart, and changes that never
+// touched this code flipped it both ways by 13-30%.
+// TestNormalizeRoute_RootFastPathDoesNotAllocate pins what matters about the
+// path instead.
func BenchmarkNormalizeRoute(b *testing.B) {
paths := []struct {
name string
@@ -23,7 +29,6 @@ func BenchmarkNormalizeRoute(b *testing.B) {
{"with_uuid", "/api/nodes/550e8400-e29b-41d4-a716-446655440000"},
{"deep_path_truncated", "/api/v1/orgs/123/resources/456/metrics"},
{"with_query_params", "/api/metrics-store/history?resourceType=vm&resourceId=pve1:node1:100&metric=cpu&range=1h"},
- {"root", "/"},
{"metrics_history", "/api/metrics-store/history"},
}
diff --git a/internal/api/http_metrics_test.go b/internal/api/http_metrics_test.go
index 6370cc2838..3e6e98ab8a 100644
--- a/internal/api/http_metrics_test.go
+++ b/internal/api/http_metrics_test.go
@@ -246,6 +246,16 @@ func TestNormalizeRoute(t *testing.T) {
}
}
+// The root and empty paths must return before splitting. Without the fast
+// path, "/" still normalizes to "/" but allocates a split slice on every call.
+func TestNormalizeRoute_RootFastPathDoesNotAllocate(t *testing.T) {
+ for _, path := range []string{"/", ""} {
+ if allocs := testing.AllocsPerRun(100, func() { _ = normalizeRoute(path) }); allocs != 0 {
+ t.Errorf("normalizeRoute(%q) allocated %v times per call, want 0", path, allocs)
+ }
+ }
+}
+
// ---------------------------------------------------------------------------
// recordAPIRequest integration tests — verify that the core recording
// function correctly populates all three Prometheus metric vectors.