-
Notifications
You must be signed in to change notification settings - Fork 124
/
speed_test.go
259 lines (215 loc) · 6.75 KB
/
speed_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package web
import (
"crypto/sha1"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
)
//
// Null response writer
//
type NullWriter struct{}
func (w *NullWriter) Header() http.Header {
return nil
}
func (w *NullWriter) Write(data []byte) (n int, err error) {
return len(data), nil
}
func (w *NullWriter) WriteHeader(statusCode int) {}
//
// Types used by any/all frameworks:
//
type RouterBuilder func(namespaces []string, resources []string) http.Handler
//
// Benchmarks for gocraft/web:
//
type BenchContext struct {
MyField string
}
type BenchContextB struct {
*BenchContext
}
type BenchContextC struct {
*BenchContextB
}
func (c *BenchContext) Action(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "hello")
}
func (c *BenchContextB) Action(w ResponseWriter, r *Request) {
fmt.Fprintf(w, c.MyField)
}
func (c *BenchContextC) Action(w ResponseWriter, r *Request) {
fmt.Fprintf(w, "hello")
}
func (c *BenchContext) Middleware(rw ResponseWriter, r *Request, next NextMiddlewareFunc) {
next(rw, r)
}
func (c *BenchContextB) Middleware(rw ResponseWriter, r *Request, next NextMiddlewareFunc) {
next(rw, r)
}
func (c *BenchContextC) Middleware(rw ResponseWriter, r *Request, next NextMiddlewareFunc) {
next(rw, r)
}
func gocraftWebHandler(rw ResponseWriter, r *Request) {
fmt.Fprintf(rw, "hello")
}
func gocraftWebRouterFor(namespaces []string, resources []string) http.Handler {
router := New(BenchContext{})
for _, ns := range namespaces {
subrouter := router.Subrouter(BenchContext{}, "/"+ns)
for _, res := range resources {
subrouter.Get("/"+res, (*BenchContext).Action)
subrouter.Post("/"+res, (*BenchContext).Action)
subrouter.Get("/"+res+"/:id", (*BenchContext).Action)
subrouter.Put("/"+res+"/:id", (*BenchContext).Action)
subrouter.Delete("/"+res+"/:id", (*BenchContext).Action)
}
}
return router
}
func BenchmarkGocraftWeb_Simple(b *testing.B) {
router := New(BenchContext{})
router.Get("/action", gocraftWebHandler)
rw, req := testRequest("GET", "/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(rw, req)
}
}
func BenchmarkGocraftWeb_Route15(b *testing.B) {
benchmarkRoutesN(b, 1, gocraftWebRouterFor)
}
func BenchmarkGocraftWeb_Route75(b *testing.B) {
benchmarkRoutesN(b, 5, gocraftWebRouterFor)
}
func BenchmarkGocraftWeb_Route150(b *testing.B) {
benchmarkRoutesN(b, 10, gocraftWebRouterFor)
}
func BenchmarkGocraftWeb_Route300(b *testing.B) {
benchmarkRoutesN(b, 20, gocraftWebRouterFor)
}
func BenchmarkGocraftWeb_Route3000(b *testing.B) {
benchmarkRoutesN(b, 200, gocraftWebRouterFor)
}
func BenchmarkGocraftWeb_Middleware(b *testing.B) {
router := New(BenchContext{})
router.Middleware((*BenchContext).Middleware)
router.Middleware((*BenchContext).Middleware)
routerB := router.Subrouter(BenchContextB{}, "/b")
routerB.Middleware((*BenchContextB).Middleware)
routerB.Middleware((*BenchContextB).Middleware)
routerC := routerB.Subrouter(BenchContextC{}, "/c")
routerC.Middleware((*BenchContextC).Middleware)
routerC.Middleware((*BenchContextC).Middleware)
routerC.Get("/action", (*BenchContextC).Action)
rw, req := testRequest("GET", "/b/c/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(rw, req)
// if rw.Code != 200 { panic("no good") }
}
}
// All middlweare/handlers don't accept context here.
func BenchmarkGocraftWeb_Generic(b *testing.B) {
nextMw := func(rw ResponseWriter, r *Request, next NextMiddlewareFunc) {
next(rw, r)
}
router := New(BenchContext{})
router.Middleware(nextMw)
router.Middleware(nextMw)
routerB := router.Subrouter(BenchContextB{}, "/b")
routerB.Middleware(nextMw)
routerB.Middleware(nextMw)
routerC := routerB.Subrouter(BenchContextC{}, "/c")
routerC.Middleware(nextMw)
routerC.Middleware(nextMw)
routerC.Get("/action", gocraftWebHandler)
rw, req := testRequest("GET", "/b/c/action")
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(rw, req)
// if rw.Code != 200 { panic("no good") }
}
}
// Intended to be my "single metric". It does a bit of everything. 75 routes, middleware, and middleware -> handler communication.
func BenchmarkGocraftWeb_Composite(b *testing.B) {
namespaces, resources, requests := resourceSetup(10)
router := New(BenchContext{})
router.Middleware(func(c *BenchContext, rw ResponseWriter, r *Request, next NextMiddlewareFunc) {
c.MyField = r.URL.Path
next(rw, r)
})
router.Middleware((*BenchContext).Middleware)
router.Middleware((*BenchContext).Middleware)
for _, ns := range namespaces {
subrouter := router.Subrouter(BenchContextB{}, "/"+ns)
subrouter.Middleware((*BenchContextB).Middleware)
subrouter.Middleware((*BenchContextB).Middleware)
subrouter.Middleware((*BenchContextB).Middleware)
for _, res := range resources {
subrouter.Get("/"+res, (*BenchContextB).Action)
subrouter.Post("/"+res, (*BenchContextB).Action)
subrouter.Get("/"+res+"/:id", (*BenchContextB).Action)
subrouter.Put("/"+res+"/:id", (*BenchContextB).Action)
subrouter.Delete("/"+res+"/:id", (*BenchContextB).Action)
}
}
benchmarkRoutes(b, router, requests)
}
//
// Helpers:
//
func testRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
request, _ := http.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
return recorder, request
}
func benchmarkRoutesN(b *testing.B, N int, builder RouterBuilder) {
namespaces, resources, requests := resourceSetup(N)
router := builder(namespaces, resources)
benchmarkRoutes(b, router, requests)
}
// Returns a routeset with N *resources per namespace*. so N=1 gives about 15 routes
func resourceSetup(N int) (namespaces []string, resources []string, requests []*http.Request) {
namespaces = []string{"admin", "api", "site"}
resources = []string{}
for i := 0; i < N; i++ {
sha1 := sha1.New()
io.WriteString(sha1, fmt.Sprintf("%d", i))
strResource := fmt.Sprintf("%x", sha1.Sum(nil))
resources = append(resources, strResource)
}
for _, ns := range namespaces {
for _, res := range resources {
req, _ := http.NewRequest("GET", "/"+ns+"/"+res, nil)
requests = append(requests, req)
req, _ = http.NewRequest("POST", "/"+ns+"/"+res, nil)
requests = append(requests, req)
req, _ = http.NewRequest("GET", "/"+ns+"/"+res+"/3937", nil)
requests = append(requests, req)
req, _ = http.NewRequest("PUT", "/"+ns+"/"+res+"/3937", nil)
requests = append(requests, req)
req, _ = http.NewRequest("DELETE", "/"+ns+"/"+res+"/3937", nil)
requests = append(requests, req)
}
}
return
}
func benchmarkRoutes(b *testing.B, handler http.Handler, requests []*http.Request) {
recorder := &NullWriter{}
reqID := 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
if reqID >= len(requests) {
reqID = 0
}
req := requests[reqID]
handler.ServeHTTP(recorder, req)
//if recorder.Code != 200 {
// panic("wat")
//}
reqID++
}
}