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

Function BenchmarkLargeHostMatcher

modules/caddyhttp/matchers_test.go:1221–1246  ·  view source on GitHub ↗
(b *testing.B)

Source from the content-addressed store, hash-verified

1219}
1220
1221func BenchmarkLargeHostMatcher(b *testing.B) {
1222 // this benchmark simulates a large host matcher (thousands of entries) where each
1223 // value is an exact hostname (not a placeholder or wildcard) - compare the results
1224 // of this with and without the binary search (comment out the various fast path
1225 // sections in Match) to conduct experiments
1226
1227 const n = 10000
1228 lastHost := fmt.Sprintf("%d.example.com", n-1)
1229 req := &http.Request{Host: lastHost}
1230 repl := caddy.NewReplacer()
1231 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
1232 req = req.WithContext(ctx)
1233
1234 matcher := make(MatchHost, n)
1235 for i := 0; i < n; i++ {
1236 matcher[i] = fmt.Sprintf("%d.example.com", i)
1237 }
1238 err := matcher.Provision(caddy.Context{})
1239 if err != nil {
1240 b.Fatal(err)
1241 }
1242
1243 for b.Loop() {
1244 matcher.MatchWithError(req)
1245 }
1246}
1247
1248func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
1249 req := &http.Request{Host: "localhost"}

Callers

nothing calls this directly

Calls 3

WithValueMethod · 0.80
ProvisionMethod · 0.65
MatchWithErrorMethod · 0.65

Tested by

no test coverage detected