aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2023-09-19 17:23:58 -0400
committerJonathan Amsterdam <jba@google.com>2023-09-19 22:53:47 +0000
commit3857a89e7eb872fa22d569e70b7e076bec74ebbb (patch)
treec22ecfb429597325b022e825bdd94b46e6ad29b1 /src/net
parent4d700a719bacb4286032c66e4e68d74024f8e748 (diff)
downloadgo-3857a89e7eb872fa22d569e70b7e076bec74ebbb.tar.xz
net/http: add a benchmark for multi indexing
We don't index multis, so a corpus full of them will take quadratic time to check for conflicts. How slow is that going to be in practice? This benchmark indexes and checks a thousand multi patterns, all disjoint. It runs in about 35ms. Change-Id: Id27940ab19ad003627bd5c43c53466e01456b796 Reviewed-on: https://go-review.googlesource.com/c/go/+/529477 Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net')
-rw-r--r--src/net/http/pattern_test.go6
-rw-r--r--src/net/http/routing_index_test.go26
2 files changed, 29 insertions, 3 deletions
diff --git a/src/net/http/pattern_test.go b/src/net/http/pattern_test.go
index e71cba8632..abda4d872d 100644
--- a/src/net/http/pattern_test.go
+++ b/src/net/http/pattern_test.go
@@ -159,11 +159,11 @@ func TestIsValidHTTPToken(t *testing.T) {
}
}
-func mustParsePattern(t *testing.T, s string) *pattern {
- t.Helper()
+func mustParsePattern(tb testing.TB, s string) *pattern {
+ tb.Helper()
p, err := parsePattern(s)
if err != nil {
- t.Fatal(err)
+ tb.Fatal(err)
}
return p
}
diff --git a/src/net/http/routing_index_test.go b/src/net/http/routing_index_test.go
index 404574a66a..1ffb9272c6 100644
--- a/src/net/http/routing_index_test.go
+++ b/src/net/http/routing_index_test.go
@@ -151,3 +151,29 @@ func genStar(max int, g generator) generator {
}
}
}
+
+func BenchmarkMultiConflicts(b *testing.B) {
+ // How fast is indexing if the corpus is all multis?
+ const nMultis = 1000
+ var pats []*pattern
+ for i := 0; i < nMultis; i++ {
+ pats = append(pats, mustParsePattern(b, fmt.Sprintf("/a/b/{x}/d%d/", i)))
+ }
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ var idx routingIndex
+ for _, p := range pats {
+ got := indexConflicts(p, &idx)
+ if len(got) != 0 {
+ b.Fatalf("got %d conflicts, want 0", len(got))
+ }
+ idx.addPattern(p)
+ }
+ if i == 0 {
+ // Confirm that all the multis ended up where they belong.
+ if g, w := len(idx.multis), nMultis; g != w {
+ b.Fatalf("got %d multis, want %d", g, w)
+ }
+ }
+ }
+}