summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/matchers_test.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-12-02 13:26:28 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2020-12-02 13:26:28 -0700
commit9157051f45b8c1354b6c8432457ca4930ba90d9e (patch)
treeb92f1a6bcd1057be76ca173417976a4687010811 /modules/caddyhttp/matchers_test.go
parent4cff36d731390915649261f0e9c088be0eeafcf1 (diff)
caddyhttp: Optimize large host matchers
Diffstat (limited to 'modules/caddyhttp/matchers_test.go')
-rw-r--r--modules/caddyhttp/matchers_test.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go
index 51db67d..ab4487d 100644
--- a/modules/caddyhttp/matchers_test.go
+++ b/modules/caddyhttp/matchers_test.go
@@ -1018,6 +1018,33 @@ func TestNotMatcher(t *testing.T) {
}
}
}
+func BenchmarkLargeHostMatcher(b *testing.B) {
+ // this benchmark simulates a large host matcher (thousands of entries) where each
+ // value is an exact hostname (not a placeholder or wildcard) - compare the results
+ // of this with and without the binary search (comment out the various fast path
+ // sections in Match) to conduct experiments
+
+ const n = 10000
+ lastHost := fmt.Sprintf("%d.example.com", n-1)
+ req := &http.Request{Host: lastHost}
+ repl := caddy.NewReplacer()
+ ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
+ req = req.WithContext(ctx)
+
+ matcher := make(MatchHost, n)
+ for i := 0; i < n; i++ {
+ matcher[i] = fmt.Sprintf("%d.example.com", i)
+ }
+ err := matcher.Provision(caddy.Context{})
+ if err != nil {
+ b.Fatal(err)
+ }
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ matcher.Match(req)
+ }
+}
func BenchmarkHostMatcherWithoutPlaceholder(b *testing.B) {
req := &http.Request{Host: "localhost"}