Skip to content

Commit 92fee3d

Browse files
dunglasclaude
andcommitted
test: add benchmark suite for pattern compilation and matching
Covers New(), Test(), and Exec() across simple, wildcard, named, regex, and full URL patterns to enable future performance comparisons. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fc2a960 commit 92fee3d

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

benchmark_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package urlpattern_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dunglas/go-urlpattern"
7+
)
8+
9+
var benchmarkPatterns = []struct {
10+
name, pattern, baseURL string
11+
}{
12+
{"simple", "https://example.com/foo/bar", ""},
13+
{"wildcard", "https://*.example.com/*", ""},
14+
{"named", "https://example.com/users/:id/posts/:postId", ""},
15+
{"regex", "https://example.com/items/(\\d+)", ""},
16+
{"full", "https://user:pw@example.com:8080/path/:id?q=:v#:frag", ""},
17+
}
18+
19+
var benchmarkMatches = []struct {
20+
name, pattern, input string
21+
}{
22+
{"simple", "https://example.com/foo/bar", "https://example.com/foo/bar"},
23+
{"wildcard", "https://*.example.com/*", "https://api.example.com/users/42"},
24+
{"named", "https://example.com/users/:id/posts/:postId", "https://example.com/users/42/posts/7"},
25+
{"regex", "https://example.com/items/(\\d+)", "https://example.com/items/12345"},
26+
{"miss", "https://example.com/foo", "https://example.com/bar"},
27+
}
28+
29+
// Package-level sinks: keep return values live so the compiler cannot
30+
// dead-code-eliminate the benchmarked calls.
31+
var (
32+
benchPatternSink *urlpattern.URLPattern
33+
benchBoolSink bool
34+
benchResultSink *urlpattern.URLPatternResult
35+
)
36+
37+
func BenchmarkNew(b *testing.B) {
38+
for _, bc := range benchmarkPatterns {
39+
b.Run(bc.name, func(b *testing.B) {
40+
b.ReportAllocs()
41+
var p *urlpattern.URLPattern
42+
var err error
43+
for i := 0; i < b.N; i++ {
44+
p, err = urlpattern.New(bc.pattern, bc.baseURL, nil)
45+
if err != nil {
46+
b.Fatal(err)
47+
}
48+
}
49+
benchPatternSink = p
50+
})
51+
}
52+
}
53+
54+
func BenchmarkTest(b *testing.B) {
55+
for _, bc := range benchmarkMatches {
56+
p, err := urlpattern.New(bc.pattern, "", nil)
57+
if err != nil {
58+
b.Fatal(err)
59+
}
60+
b.Run(bc.name, func(b *testing.B) {
61+
b.ReportAllocs()
62+
var ok bool
63+
for i := 0; i < b.N; i++ {
64+
ok = p.Test(bc.input, "")
65+
}
66+
benchBoolSink = ok
67+
})
68+
}
69+
}
70+
71+
func BenchmarkExec(b *testing.B) {
72+
for _, bc := range benchmarkMatches {
73+
p, err := urlpattern.New(bc.pattern, "", nil)
74+
if err != nil {
75+
b.Fatal(err)
76+
}
77+
b.Run(bc.name, func(b *testing.B) {
78+
b.ReportAllocs()
79+
var r *urlpattern.URLPatternResult
80+
for i := 0; i < b.N; i++ {
81+
r = p.Exec(bc.input, "")
82+
}
83+
benchResultSink = r
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)