-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbenchmark_test.go
230 lines (192 loc) · 5.38 KB
/
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package rux
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
)
func BenchmarkOneRoute(B *testing.B) {
router := New()
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkManyHandlers(B *testing.B) {
router := New()
// router.Use(Recovery(), LoggerWithWriter(newMockWriter()))
router.Use(func(c *Context) {})
router.Use(func(c *Context) {})
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark5Params(B *testing.B) {
// DefaultWriter = os.Stdout
router := New()
router.Use(func(c *Context) {})
router.GET("/param/{param1}/{params2}/{param3}/{param4}/{param5}", func(c *Context) {})
runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
}
func BenchmarkManyRoutesFist(B *testing.B) {
router := New()
router.Any("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark404(B *testing.B) {
router := New()
router.Any("/something", func(c *Context) {})
router.NotFound(func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark404Many(B *testing.B) {
router := New()
router.GET("/", func(c *Context) {})
router.GET("/path/to/something", func(c *Context) {})
router.GET("/post/{id}", func(c *Context) {})
router.GET("/view/{id}", func(c *Context) {})
router.GET("/favicon.ico", func(c *Context) {})
router.GET("/robots.txt", func(c *Context) {})
router.GET("/delete/{id}", func(c *Context) {})
router.GET("/user/{id}/{mode}", func(c *Context) {})
router.NotFound(func(c *Context) {})
runRequest(B, router, "GET", "/viewfake")
}
var (
srsHasMethod = map[string]*Route{}
srsNoMethod = map[string]*Route{}
)
func BenchmarkStableRoutes_hasMethod(B *testing.B) {
srsHasMethod["GET/"] = NewRoute("/", emptyHandler, http.MethodGet)
srsHasMethod["GET/home"] = NewRoute("/home", emptyHandler, http.MethodGet)
B.ReportAllocs()
B.ResetTimer()
path := "/"
method := http.MethodGet
for i := 0; i < B.N; i++ {
key := method + path
if _, ok := srsHasMethod[key]; ok {
// match ok
}
}
}
func BenchmarkStableRoutes_noMethod(B *testing.B) {
srsNoMethod["/"] = NewRoute("/", emptyHandler, http.MethodGet)
srsNoMethod["/home"] = NewRoute("/home", emptyHandler, http.MethodGet)
B.ReportAllocs()
B.ResetTimer()
path := "/"
method := http.MethodGet
for i := 0; i < B.N; i++ {
route, ok := srsNoMethod[path]
if ok && strings.Contains(route.MethodString("|")+"|", method+"|") {
// match ok
}
}
}
func TestMultiMatchAtOnce(t *testing.T) {
t.Skip("skip testing this")
// route: /user/{arg1}/{arg2}
regexS := `^(?|/user/([^/]+)/([^/]+)|/blog/([^/]+)/([^/]+)|/order/([^/]+)/([^/]+)|/goods/([^/]+)/([^/]+))$`
// tests := []struct{}
rgp := regexp.MustCompilePOSIX(regexS)
ret := rgp.FindAllStringSubmatch("/user/test/123", -1)
fmt.Println(ret)
}
/*************************************************************
* test allocs
*************************************************************/
func TestAlloc_formatPath(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
// r.formatPath("/blog/100")
r.formatPath("/blog/100/")
})))
}
func TestAlloc_match_static(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)
r.GET("/about", emptyHandler)
// output: 0 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
r.match(GET, "/about")
})))
}
func TestAlloc_match_regular(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
r.match(GET, "/blog/100")
})))
}
/*************************************************************
* helper methods(ref the gin framework)
*************************************************************/
type (
m map[string]string
md struct {
// body
B string
// headers
H m
}
)
// mock an HTTP Request
// Usage:
// handler := router.New()
// res := mockRequest(handler, "GET", "/path", nil)
// // with data
// res := mockRequest(handler, "GET", "/path", &md{B: "data", H: m{"x-head": "val"}})
func mockRequest(h http.Handler, method, path string, data *md, beforeSend ...func(req *http.Request)) *httptest.ResponseRecorder {
var body io.Reader
if data != nil && len(data.B) > 0 {
body = strings.NewReader(data.B)
}
// create fake request
req, err := http.NewRequest(method, path, body)
if err != nil {
panic(err)
}
req.RequestURI = req.URL.String()
if data != nil && len(data.H) > 0 {
// req.Header.Set("Content-Type", "text/plain")
for k, v := range data.H {
req.Header.Set(k, v)
}
}
if len(beforeSend) > 0 {
beforeSend[0](req)
}
w := httptest.NewRecorder()
// s := httptest.NewServer()
h.ServeHTTP(w, req)
// return w.Result() will return http.Response
return w
}
// will store old env value, set new val. will restore old value on end.
func mockEnvValue(key, val string, fn func()) {
old := os.Getenv(key)
_ = os.Setenv(key, val)
fn()
if old != "" {
_ = os.Setenv(key, old)
}
}
func runRequest(B *testing.B, r *Router, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := httptest.NewRecorder()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
r.ServeHTTP(w, req)
}
}