-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfastpath_bench_test.go
125 lines (116 loc) · 2.9 KB
/
fastpath_bench_test.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package fastpath
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/ucarion/urlpath"
)
const pattern = "/api/user/:id"
const uri = "/api/user/728342"
func getRegex(p string) (*regexp.Regexp, error) {
pattern := "^"
segments := strings.Split(p, "/")
for _, s := range segments {
if s == "" {
continue
}
if s[0] == ':' {
if strings.Contains(s, "?") {
pattern += "(?:/([^/]+?))?"
} else {
pattern += "/(?:([^/]+?))"
}
} else if s[0] == '*' {
pattern += "/(.*)"
} else {
pattern += "/" + s
}
}
pattern += "/?$"
regex, err := regexp.Compile(pattern)
return regex, err
}
func matchRegex(regex *regexp.Regexp, p string) (match bool, values []string) {
if regex.MatchString(p) {
// get values for parameters
matches := regex.FindAllStringSubmatch(p, -1)
// did we get the values?
if len(matches) > 0 && len(matches[0]) > 1 {
values = matches[0][1:len(matches[0])]
return true, values
}
return false, values
}
return false, values
}
func BenchmarkRegexp(b *testing.B) {
regex, _ := getRegex(pattern)
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = matchRegex(regex, uri)
}
}
func BenchmarkUrlPath(b *testing.B) {
parser := urlpath.New(pattern)
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = parser.Match(uri)
}
}
func BenchmarkMatch(b *testing.B) {
p := New(pattern)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = p.Match(uri)
}
}
func BenchmarkFastPathCompileTime(bParent *testing.B) {
cases := []string{
"",
"/",
"/api/v1/:param/*",
"/api/*",
"/api/v1/const",
"/api/v1/test",
"/api/v1/:param?",
"/api/v1/:param2?",
"/api/v1/:param/:param2?",
"/api/v1/:param/:param2/:param3",
"/api/v1/:param/:param2/:nomatch",
}
for _, bPattern := range cases {
bParent.Run(fmt.Sprintf("%s", bPattern), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = New(bPattern)
}
})
}
}
func BenchmarkMatchCases(bParent *testing.B) {
cases := map[string]string{
"": "/",
"/": "/",
"/api/v1/:param/*": "/api/v1/entity/1",
"/api/*": "/api/v1/entity/1",
"/api/v1/const": "/api/v1/const",
"/api/v1/test": "/api/v1/noMatch",
"/api/v1/:param?": "/api/v1/entity",
"/api/v1/:param2?": "/api/v1/",
"/api/v1/:param/:param2?": "/api/v1/entity/1",
"/api/v1/:param/:param2/:param3": "/api/v1/entity/1/2",
"/api/v1/*/:param3": "/api/v1/entity/1/2",
"/api/v1/:param/:param2/:nomatch": "/api/v1/entity/1",
"/config/abc.json": "/config/abc.json",
"/config/noMatch.json": "/config/abc.json",
}
for bPattern, bUrl := range cases {
bParent.Run(fmt.Sprintf("%s - %s", bPattern, bUrl), func(b *testing.B) {
parser := New(bPattern)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = parser.Match(bUrl)
}
})
}
}