-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathbench_test.go
203 lines (185 loc) · 5.29 KB
/
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
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
package kami_test
import (
"net/http"
"net/http/httptest"
"testing"
"golang.org/x/net/context"
"github.com/guregu/kami"
)
func routeBench(b *testing.B, route string) {
kami.Reset()
kami.Use("/Z/", noopMW)
kami.After("/Z/", noopMW)
kami.Get(route, noop)
req, _ := http.NewRequest("GET", route, nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
func BenchmarkShortRoute(b *testing.B) {
routeBench(b, "/hello")
}
func BenchmarkLongRoute(b *testing.B) {
routeBench(b, "/aaaaaaaaaaaa/")
}
func BenchmarkDeepRoute(b *testing.B) {
routeBench(b, "/a/b/c/d/e/f/g")
}
func BenchmarkDeepRouteUnicode(b *testing.B) {
routeBench(b, "/ä/蜂/海/🐶/神/🍺/🍻")
}
func BenchmarkSuperDeepRoute(b *testing.B) {
routeBench(b, "/a/b/c/d/e/f/g/h/i/l/k/l/m/n/o/p/q/r/hello world")
}
// Param benchmarks test accessing URL params
func BenchmarkParameter(b *testing.B) {
kami.Reset()
kami.Get("/hello/:name", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
kami.Param(ctx, "name")
})
req, _ := http.NewRequest("GET", "/hello/bob", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
func BenchmarkParameter5(b *testing.B) {
kami.Reset()
kami.Get("/:a/:b/:c/:d/:e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, v := range []string{"a", "b", "c", "d", "e"} {
kami.Param(ctx, v)
}
})
req, _ := http.NewRequest("GET", "/a/b/c/d/e", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
// Middleware tests setting and using values with middleware
// These test the speed of kami's middleware engine AND using
// x/net/context to store values, so it could be a somewhat
// realitic idea of what using kami would be like.
func BenchmarkMiddleware(b *testing.B) {
kami.Reset()
kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, "test", "ok")
})
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if ctx.Value("test") != "ok" {
w.WriteHeader(http.StatusServiceUnavailable)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
func BenchmarkMiddleware5(b *testing.B) {
kami.Reset()
numbers := []int{1, 2, 3, 4, 5}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, n := range numbers {
if ctx.Value(n) != n {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
})
req, _ := http.NewRequest("GET", "/test", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
func BenchmarkMiddleware1Afterware1(b *testing.B) {
kami.Reset()
numbers := []int{1}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.After("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
for _, n := range numbers {
if ctx.Value(n) != n {
panic(n)
}
}
return ctx
})
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
// ...
})
req, _ := http.NewRequest("GET", "/test", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
func BenchmarkMiddleware5Afterware1(b *testing.B) {
kami.Reset()
numbers := []int{1, 2, 3, 4, 5}
for _, n := range numbers {
n := n // wtf
kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return context.WithValue(ctx, n, n)
})
}
kami.After("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
for _, n := range numbers {
if ctx.Value(n) != n {
panic(n)
}
}
return ctx
})
kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
for _, n := range numbers {
if ctx.Value(n) != n {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
})
req, _ := http.NewRequest("GET", "/test", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}
// This tests just the URL walking middleware engine.
func BenchmarkMiddlewareAfterwareMiss(b *testing.B) {
kami.Reset()
kami.Use("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return nil
})
kami.After("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return nil
})
kami.Get("/a/bbb/cc/d/e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
req, _ := http.NewRequest("GET", "/a/bbb/cc/d/e", nil)
b.ResetTimer()
for n := 0; n < b.N; n++ {
resp := httptest.NewRecorder()
kami.Handler().ServeHTTP(resp, req)
}
}