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 route normaliser rows that flip with layout

rcourtman3d322ff3a8f3
diff --git a/internal/api/http_metrics_allocations_test.go b/internal/api/http_metrics_allocations_test.go
index 18fda99393..6cd27dbc46 100644
--- a/internal/api/http_metrics_allocations_test.go
+++ b/internal/api/http_metrics_allocations_test.go
@@ -32,7 +32,7 @@ func isolatedRouteAllocationCheck(t *testing.T, measure func()) {
func runRouteAllocationTest(t *testing.T, name string) {
t.Helper()
switch name {
- case "TestNormalizeSegment_DoesNotAllocate", "TestNormalizeRoute_RootFastPathDoesNotAllocate", "TestNormalizeRoute_BoundedAllocations":
+ case "TestNormalizeSegment_DoesNotAllocate", "TestNormalizeRoute_RootFastPathDoesNotAllocate", "TestNormalizeRoute_BoundedAllocations", "TestNormalizeRoute_LabelAllocationProfile":
default:
t.Fatalf("unknown route allocation measurement %q", name)
}
diff --git a/internal/api/http_metrics_bench_test.go b/internal/api/http_metrics_bench_test.go
index 7bb0c56670..b190fa07df 100644
--- a/internal/api/http_metrics_bench_test.go
+++ b/internal/api/http_metrics_bench_test.go
@@ -11,62 +11,17 @@ import (
// middleware (middleware.go:57–62). These functions must stay fast to
// avoid adding per-request overhead.
-// BenchmarkNormalizeRoute measures route normalization with representative
-// API paths of varying complexity.
-//
-// Neither the "/" fast path nor normalizeSegment is timed on its own. They
-// take 2-40ns, and paired CI runs measured each at one of two values a CPU
-// cycle or two apart, per call or per byte of the UUID scan.
-// Changes that never touched this code flipped them both ways by 13-50%.
-// Every path below runs its segments through normalizeSegment, so a real
-// slowdown in segment classification still shows here, most in with_uuid.
-// TestNormalizeRoute_RootFastPathDoesNotAllocate and
-// TestNormalizeSegment_DoesNotAllocate pin the allocation guarantees.
-func BenchmarkNormalizeRoute(b *testing.B) {
- paths := []struct {
- name string
- path string
- }{
- {"simple_api", "/api/resources"},
- {"with_numeric_id", "/api/users/12345"},
- {"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"},
- {"metrics_history", "/api/metrics-store/history"},
- }
-
- for _, p := range paths {
- b.Run(p.name, func(b *testing.B) {
- b.ReportAllocs()
- for i := 0; i < b.N; i++ {
- _ = normalizeRoute(p.path)
- }
- })
- }
-}
-
-// BenchmarkNormalizeRoute_Parallel measures normalizeRoute under concurrent
-// access matching the production middleware pattern (many goroutines calling
-// normalizeRoute simultaneously for different requests). Uses r.URL.Path
-// inputs (no query parameters) matching the real middleware call site.
-func BenchmarkNormalizeRoute_Parallel(b *testing.B) {
- paths := []string{
- "/api/resources",
- "/api/users/12345",
- "/api/metrics-store/history",
- "/api/nodes/550e8400-e29b-41d4-a716-446655440000/status",
- "/api/alerts/config",
- }
-
- b.ReportAllocs()
- b.RunParallel(func(pb *testing.PB) {
- i := 0
- for pb.Next() {
- _ = normalizeRoute(paths[i%len(paths)])
- i++
- }
- })
-}
+// normalizeRoute is not timed on its own. Since the bounded scanner it takes
+// 30-180ns a path on CI runners, and on pull requests that never touched
+// route code the BenchmarkNormalizeRoute rows and their parallel variant
+// failed 17 of 72 paired comparisons, 6 of those without with_uuid. Go aligns
+// functions to 32 bytes, so a change earlier in the binary moves the
+// scanner's loops by a cycle per iteration, as it did for the "/" fast path
+// and normalizeSegment. Every path shares those loops, so a mixed-path row
+// moves almost as far. TestNormalizeRoute_LabelAllocationProfile pins the
+// allocation columns, which never moved. BenchmarkFullMiddlewarePath still
+// times the normaliser inside the request path, and
+// BenchmarkNormalizeRoute_DiscardedSuffix the cost of a long discarded suffix.
// BenchmarkRecordAPIRequest measures the full Prometheus recording pipeline
// for a single HTTP request (histogram observe + counter inc + error counter
diff --git a/internal/api/http_metrics_route_test.go b/internal/api/http_metrics_route_test.go
index 41a100e2f3..be39917032 100644
--- a/internal/api/http_metrics_route_test.go
+++ b/internal/api/http_metrics_route_test.go
@@ -4,6 +4,7 @@ import (
"math/rand"
"net/http"
"net/http/httptest"
+ "runtime"
"strconv"
"strings"
"sync"
@@ -130,6 +131,63 @@ func TestNormalizeRoute_BoundedAllocations(t *testing.T) {
}
}
+// The retired BenchmarkNormalizeRoute rows flipped by a CPU cycle with code
+// layout, but their allocation columns never moved: one allocation per call,
+// sized to the label. That is the part a timing gate cannot judge reliably, so
+// it is pinned here per path. A label built from a buffer sized to the raw
+// path, or assembled through intermediates, fails even when it stays fast.
+func TestNormalizeRoute_LabelAllocationProfile(t *testing.T) {
+ if !measureAllocsInIsolation(t) {
+ return
+ }
+ for _, c := range []struct{ path, label string }{
+ {"/api/resources", "/api/resources"},
+ {"/api/users/12345", "/api/users/:id"},
+ {"/api/nodes/550e8400-e29b-41d4-a716-446655440000", "/api/nodes/:uuid"},
+ {"/api/nodes/550e8400-e29b-41d4-a716-446655440000/status", "/api/nodes/:uuid/status"},
+ {"/api/v1/orgs/123/resources/456/metrics", "/api/v1/orgs/:id/resources"},
+ {"/api/metrics-store/history?resourceType=vm&resourceId=pve1:node1:100&metric=cpu&range=1h", "/api/metrics-store/history"},
+ {"/api/metrics-store/history", "/api/metrics-store/history"},
+ } {
+ if got := normalizeRoute(c.path); got != c.label {
+ t.Fatalf("normalizeRoute(%q) = %q, want %q", c.path, got, c.label)
+ }
+ // A fresh copy of the label is the cost floor: one allocation in
+ // whatever size class the runtime gives that many bytes.
+ wantAllocs, wantBytes := allocationsPerCall(func() { normalizeRouteSink = strings.Clone(c.label) })
+ allocs, bytes := allocationsPerCall(func() { normalizeRouteSink = normalizeRoute(c.path) })
+ if wantAllocs != 1 {
+ t.Fatalf("control: cloning %q allocated %d times per call, want 1", c.label, wantAllocs)
+ }
+ if allocs != wantAllocs || bytes != wantBytes {
+ t.Errorf("normalizeRoute(%q) allocated %d times and %d bytes per call, want %d and %d (the label alone)",
+ c.path, allocs, bytes, wantAllocs, wantBytes)
+ }
+ }
+}
+
+// allocationsPerCall is testing.AllocsPerRun with the byte count kept. Callers
+// run in an isolated child, since MemStats are process-wide. A single runtime
+// allocation inside one window would add its whole size to the bytes, so each
+// figure is the lowest of a few windows: f's own cost recurs in every window.
+func allocationsPerCall(f func()) (allocs, bytes uint64) {
+ const runs, windows = 100, 3
+ defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
+ f()
+ allocs, bytes = ^uint64(0), ^uint64(0)
+ for range windows {
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ for range runs {
+ f()
+ }
+ runtime.ReadMemStats(&after)
+ allocs = min(allocs, (after.Mallocs-before.Mallocs)/runs)
+ bytes = min(bytes, (after.TotalAlloc-before.TotalAlloc)/runs)
+ }
+ return allocs, bytes
+}
+
func TestNormalizeRoute_ConcurrentAndRetainedLabels(t *testing.T) {
paths := routeLabelCorpus()[:20]
var workers sync.WaitGroup