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>
This commit is contained in:
Jonathan Amsterdam 2023-09-19 17:23:58 -04:00
parent 4d700a719b
commit 3857a89e7e
2 changed files with 29 additions and 3 deletions

View File

@ -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
}

View File

@ -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)
}
}
}
}