]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add a benchmark for multi indexing
authorJonathan Amsterdam <jba@google.com>
Tue, 19 Sep 2023 21:23:58 +0000 (17:23 -0400)
committerJonathan Amsterdam <jba@google.com>
Tue, 19 Sep 2023 22:53:47 +0000 (22:53 +0000)
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>
src/net/http/pattern_test.go
src/net/http/routing_index_test.go

index e71cba863242de29c3af8c48d1347697eaade6f2..abda4d872d49d63d12120698734bc549a9d4223a 100644 (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
 }
index 404574a66a646c5075173b54f59186c844c6b1b7..1ffb9272c63a38ce713ab555fa6cacbed26e32fd 100644 (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)
+                       }
+               }
+       }
+}