mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 01:08:56 +01:00
The advisory Benchmarks check failed #2884 on NormalizeRoute/with_uuid
alone, +12.25% (p=0.000), though that PR changed only AI handler code.
It was not the only one. Since the bounded route scanner landed on
9 October (01788459f1), normalizeRoute takes 30-180ns a path on CI runners. In 72
paired CI comparisons that measured internal/api from then until 13
October, the BenchmarkNormalizeRoute rows and their parallel variant
failed the gate 17 times. None of those PRs touched route code, and 6
of the 17 failures did not involve with_uuid: deep_path_truncated,
with_query_params, metrics_history, with_numeric_id and Parallel each
crossed 10% too. with_uuid moved past 10% in 21 of the 72 comparisons,
in both directions.
This is the same layout effect that retired the "/" fast path (#2255)
and NormalizeSegment (#2258). Go aligns functions to 32 bytes, so code
added earlier in the package moves normalizeRoute between the halves of
a 64-byte block. That changes its loops by a cycle per iteration. Every
path shares those loops, so retiring with_uuid alone would leave the
other rows failing. Summed over all six paths, a mixed-path row would
still have moved up to 10%.
BenchmarkNormalizeRoute and BenchmarkNormalizeRoute_Parallel are
removed. Their allocation columns never varied, and they are now pinned
by TestNormalizeRoute_LabelAllocationProfile. For each former path it
requires exactly one allocation, of the same size as a fresh copy of the
label. It runs in the existing isolated child process and takes the
lowest of three windows, so one stray runtime allocation cannot fail it.
BenchmarkFullMiddlewarePath still times the normaliser inside the
request path, with a CI maximum of +8.4% over those 72 runs, and
BenchmarkNormalizeRoute_DiscardedSuffix still times a long discarded
suffix. Gross classification slowdowns are left to those rows: a
doubled UUID scan has no deterministic signal, and the retired row
could not separate one from layout.
Proofs, on pulse-dev with the CI runner narrowed to ./internal/api/ at
30 samples. The probe is an unrelated init-time function in a file
sorted before http_metrics.go. It moves normalizeRoute from 32 to 0 mod
64 in the test binary without changing its size:
- old tree vs old tree plus probe: with_uuid -14.70% (p=0.001). Read the
other way, as a PR that removes the probe, the gate exits 1 on
NormalizeRoute/with_uuid alone, +17.23% (p=0.001). That is #2884's
shape.
- new tree vs new tree plus probe: gate exits 0 in both directions. On
a first run under load 9-15, three millisecond-scale rows that run no
route code flagged at p=0.025-0.046 with spreads of 15-38%. The
repeat came back clean in both directions.
- mutation controls on the final test: sizing the label buffer at twice
the label fails only the new test. The existing at-most-one-allocation
test passes it. Joining the label through an intermediate fails both.
- the route tests pass 20 times plain and 10 times under -race.
119 lines
4.0 KiB
Go
119 lines
4.0 KiB
Go
package api
|
||
|
||
import (
|
||
"fmt"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// Benchmarks for the HTTP metrics hot path — normalizeRoute and
|
||
// recordAPIRequest run on every incoming HTTP request via ErrorHandler
|
||
// middleware (middleware.go:57–62). These functions must stay fast to
|
||
// avoid adding per-request overhead.
|
||
|
||
// 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
|
||
// conditional). This is the complete per-request cost.
|
||
func BenchmarkRecordAPIRequest(b *testing.B) {
|
||
// Ensure metrics are initialized before timing.
|
||
httpMetricsOnce.Do(initHTTPMetrics)
|
||
|
||
cases := []struct {
|
||
name string
|
||
method string
|
||
route string
|
||
status int
|
||
}{
|
||
{"success_200", "GET", "/api/resources", 200},
|
||
{"not_found_404", "GET", "/api/missing", 404},
|
||
{"server_error_500", "POST", "/api/settings", 500},
|
||
{"created_201", "POST", "/api/alerts", 201},
|
||
}
|
||
|
||
for _, c := range cases {
|
||
b.Run(c.name, func(b *testing.B) {
|
||
elapsed := 5 * time.Millisecond
|
||
// Use unique route per sub-benchmark to avoid label contention
|
||
// affecting other benchmarks in the same process.
|
||
route := fmt.Sprintf("%s/bench/%s", c.route, c.name)
|
||
// Warmup: pre-create Prometheus label series so the benchmark
|
||
// measures steady-state cost, not first-touch allocation.
|
||
recordAPIRequest(c.method, route, c.status, elapsed)
|
||
|
||
b.ReportAllocs()
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
recordAPIRequest(c.method, route, c.status, elapsed)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// BenchmarkRecordAPIRequest_Parallel measures recordAPIRequest under
|
||
// concurrent goroutine load, matching the real middleware hot path where
|
||
// many HTTP handlers record metrics simultaneously.
|
||
func BenchmarkRecordAPIRequest_Parallel(b *testing.B) {
|
||
httpMetricsOnce.Do(initHTTPMetrics)
|
||
|
||
elapsed := 5 * time.Millisecond
|
||
routes := []string{
|
||
"/api/resources/bench/parallel/0",
|
||
"/api/metrics-store/history/bench/parallel/1",
|
||
"/api/alerts/bench/parallel/2",
|
||
"/api/settings/bench/parallel/3",
|
||
}
|
||
statuses := []int{200, 200, 404, 200}
|
||
|
||
// Warmup: pre-create all Prometheus label series.
|
||
for i, route := range routes {
|
||
recordAPIRequest("GET", route, statuses[i], elapsed)
|
||
}
|
||
|
||
b.ReportAllocs()
|
||
b.ResetTimer()
|
||
b.RunParallel(func(pb *testing.PB) {
|
||
i := 0
|
||
for pb.Next() {
|
||
idx := i % len(routes)
|
||
recordAPIRequest("GET", routes[idx], statuses[idx], elapsed)
|
||
i++
|
||
}
|
||
})
|
||
}
|
||
|
||
// BenchmarkFullMiddlewarePath simulates the complete per-request overhead:
|
||
// normalizeRoute + recordAPIRequest, as executed by the ErrorHandler middleware.
|
||
// Uses r.URL.Path (no query params) matching the real middleware call site.
|
||
func BenchmarkFullMiddlewarePath(b *testing.B) {
|
||
httpMetricsOnce.Do(initHTTPMetrics)
|
||
|
||
// In the real middleware (middleware.go:57), normalizeRoute receives
|
||
// r.URL.Path which never contains query parameters.
|
||
path := "/api/metrics-store/history"
|
||
elapsed := 5 * time.Millisecond
|
||
|
||
// Warmup: ensure Prometheus label series are pre-created so the
|
||
// benchmark measures steady-state cost, not first-touch allocation.
|
||
route := normalizeRoute(path)
|
||
recordAPIRequest("GET", route, 200, elapsed)
|
||
|
||
b.ReportAllocs()
|
||
b.ResetTimer()
|
||
for i := 0; i < b.N; i++ {
|
||
route := normalizeRoute(path)
|
||
recordAPIRequest("GET", route, 200, elapsed)
|
||
}
|
||
}
|