MCPcopy
hub / github.com/caddyserver/caddy / BenchmarkSingleRouteMetrics

Function BenchmarkSingleRouteMetrics

modules/caddyhttp/metrics_test.go:602–631  ·  view source on GitHub ↗

BenchmarkSingleRouteMetrics simulates the new behavior where metrics are collected once for the entire route.

(b *testing.B)

Source from the content-addressed store, hash-verified

600// BenchmarkSingleRouteMetrics simulates the new behavior where metrics
601// are collected once for the entire route.
602func BenchmarkSingleRouteMetrics(b *testing.B) {
603 ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
604 m := &Metrics{
605 init: sync.Once{},
606 httpMetrics: &httpMetrics{},
607 }
608
609 // Build a chain of 5 plain middleware handlers (no per-handler metrics)
610 var next Handler = HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
611 return nil
612 })
613 for i := 0; i < 5; i++ {
614 capturedNext := next
615 next = HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
616 return capturedNext.ServeHTTP(w, r)
617 })
618 }
619
620 // Wrap the entire chain with a single route-level metrics handler
621 ih := newMetricsInstrumentedRoute(ctx, "handler", next, m)
622
623 r := httptest.NewRequest("GET", "/", nil)
624 w := httptest.NewRecorder()
625
626 b.ResetTimer()
627 b.ReportAllocs()
628 for i := 0; i < b.N; i++ {
629 ih.ServeHTTP(w, r)
630 }
631}

Callers

nothing calls this directly

Calls 3

HandlerFuncFuncType · 0.85
ServeHTTPMethod · 0.65

Tested by

no test coverage detected