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