/
mux_test.go
204 lines (160 loc) · 3.91 KB
/
mux_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
package azugo
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func TestMuxGroup(t *testing.T) {
handlerFunc := func(*Context) {}
var (
muxUseCalled int
groupUseCalled int
)
m := newMux(NewTestApp().App)
m.Use(func(next RequestHandler) RequestHandler {
return func(ctx *Context) {
muxUseCalled++
next(ctx)
}
})
g := m.Group("/group")
{
g.Use(func(next RequestHandler) RequestHandler {
return func(ctx *Context) {
groupUseCalled++
next(ctx)
}
})
g.Get("/path", handlerFunc)
}
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/group/path")
m.Handler(ctx)
assert.Equal(t, 1, muxUseCalled)
assert.Equal(t, 1, groupUseCalled)
}
func BenchmarkAllowed(b *testing.B) {
handlerFunc := func(*Context) {}
m := newMux(NewTestApp().App)
m.Post("/path", handlerFunc)
m.Get("/path", handlerFunc)
b.Run("Global", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = m.Allowed("*", fasthttp.MethodOptions)
}
})
b.Run("Path", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = m.Allowed("/path", fasthttp.MethodOptions)
}
})
}
func BenchmarkRouterGet(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/hello", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterParams(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/{id}", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/hello")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterANY(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/data", func(ctx *Context) {})
m.Any("/", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterGet_ANY(b *testing.B) {
var (
resp = "Bench GET"
respANY = "Bench GET (ANY)"
)
m := newMux(NewTestApp().App)
m.Get("/", func(ctx *Context) {
ctx.StatusCode(fasthttp.StatusOK).Text(resp)
})
m.Any("/", func(ctx *Context) {
ctx.StatusCode(fasthttp.StatusOK).Text(respANY)
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("UNICORN")
ctx.Request.SetRequestURI("/")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterNotFound(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/bench", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/notfound")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterFindCaseInsensitive(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/bench", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/BenCh/.")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func BenchmarkRouterRedirectTrailingSlash(b *testing.B) {
m := newMux(NewTestApp().App)
m.Get("/bench/", func(ctx *Context) {})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/bench")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}
func Benchmark_Get(b *testing.B) {
handler := func(ctx *Context) {}
m := newMux(NewTestApp().App)
m.Get("/", handler)
m.Get("/plaintext", handler)
m.Get("/json", handler)
m.Get("/fortune", handler)
m.Get("/fortune-quick", handler)
m.Get("/db", handler)
m.Get("/queries", handler)
m.Get("/update", handler)
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/update")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Handler(ctx)
}
}