This repository has been archived by the owner on May 24, 2020. It is now read-only.
forked from ant0ine/go-json-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_benchmark_test.go
143 lines (120 loc) · 2.67 KB
/
router_benchmark_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package rest
import (
"fmt"
"net/url"
"regexp"
"testing"
)
func routes() []Route {
// simulate the routes of a real but reasonable app.
// 6 + 10 * (5 + 2) + 1 = 77 routes
routePaths := []string{
"/",
"/signin",
"/signout",
"/profile",
"/settings",
"/upload/*file",
}
for i := 0; i < 10; i++ {
for j := 0; j < 5; j++ {
routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id/property%d", i, j))
}
routePaths = append(routePaths, fmt.Sprintf("/resource%d/:id", i))
routePaths = append(routePaths, fmt.Sprintf("/resource%d", i))
}
routePaths = append(routePaths, "/*")
routes := []Route{}
for _, path := range routePaths {
routes = append(routes, Route{
HttpMethod: "GET",
PathExp: path,
})
}
return routes
}
func requestUrls() []*url.URL {
// simulate a few requests
urlStrs := []string{
"http://example.org/",
"http://example.org/resource9/123",
"http://example.org/resource9/123/property1",
"http://example.org/doesnotexist",
}
urlObjs := []*url.URL{}
for _, url_str := range urlStrs {
url_obj, _ := url.Parse(url_str)
urlObjs = append(urlObjs, url_obj)
}
return urlObjs
}
func BenchmarkNoCompression(b *testing.B) {
b.StopTimer()
r := router{
routes: routes(),
disableTrieCompression: true,
}
r.start()
urlObjs := requestUrls()
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, urlObj := range urlObjs {
r.findRouteFromURL("GET", urlObj)
}
}
}
func BenchmarkCompression(b *testing.B) {
b.StopTimer()
r := router{
routes: routes(),
}
r.start()
urlObjs := requestUrls()
b.StartTimer()
for i := 0; i < b.N; i++ {
for _, urlObj := range urlObjs {
r.findRouteFromURL("GET", urlObj)
}
}
}
func BenchmarkRegExpLoop(b *testing.B) {
// reference benchmark using the usual RegExps + Loop strategy
b.StopTimer()
routes := routes()
urlObjs := requestUrls()
// build the route regexps
r1, err := regexp.Compile(":[^/\\.]*")
if err != nil {
panic(err)
}
r2, err := regexp.Compile("\\*.*")
if err != nil {
panic(err)
}
routeRegexps := []regexp.Regexp{}
for _, route := range routes {
// generate the regexp string
regStr := r2.ReplaceAllString(route.PathExp, "([^/\\.]+)")
regStr = r1.ReplaceAllString(regStr, "(.+)")
regStr = "^" + regStr + "$"
// compile it
reg, err := regexp.Compile(regStr)
if err != nil {
panic(err)
}
routeRegexps = append(routeRegexps, *reg)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
// do it for a few urls
for _, urlObj := range urlObjs {
// stop at the first route that matches
for index, reg := range routeRegexps {
if reg.FindAllString(urlObj.Path, 1) != nil {
_ = routes[index]
break
}
}
}
}
}