-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter_bench_test.go
More file actions
332 lines (264 loc) · 7.53 KB
/
Copy pathrouter_bench_test.go
File metadata and controls
332 lines (264 loc) · 7.53 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package fursy
import (
"net/http"
"net/http/httptest"
"testing"
)
// BenchmarkRouter_StaticRoute benchmarks routing for a simple static route.
func BenchmarkRouter_StaticRoute(b *testing.B) {
router := New()
router.GET("/users", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/users", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_ParameterRoute benchmarks routing with URL parameters.
func BenchmarkRouter_ParameterRoute(b *testing.B) {
router := New()
router.GET("/users/:id", func(c *Context) error {
_ = c.Param("id")
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/users/123", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_ParameterRoute_MultipleParams benchmarks multiple parameters.
func BenchmarkRouter_ParameterRoute_MultipleParams(b *testing.B) {
router := New()
router.GET("/posts/:category/:postID", func(c *Context) error {
_ = c.Param("category")
_ = c.Param("postID")
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/posts/tech/456", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_WildcardRoute benchmarks catch-all wildcard routing.
func BenchmarkRouter_WildcardRoute(b *testing.B) {
router := New()
router.GET("/files/*path", func(c *Context) error {
_ = c.Param("path")
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/files/documents/report.pdf", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_MultipleRoutes benchmarks routing with many registered routes.
func BenchmarkRouter_MultipleRoutes(b *testing.B) {
router := New()
// Register multiple routes to simulate realistic scenario.
routes := []string{
"/",
"/users",
"/users/:id",
"/posts",
"/posts/:category/:id",
"/api/v1/users",
"/api/v1/users/:id/profile",
"/api/v1/posts",
"/api/v1/posts/:id",
"/files/*path",
"/static/*path",
"/admin/users",
"/admin/posts",
}
handler := func(c *Context) error {
return c.NoContent(http.StatusOK)
}
for _, route := range routes {
router.GET(route, handler)
}
req := httptest.NewRequest("GET", "/users/123", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_DeepNesting benchmarks deeply nested routes.
func BenchmarkRouter_DeepNesting(b *testing.B) {
router := New()
router.GET("/api/v1/organizations/:orgID/projects/:projectID/issues/:issueID/comments/:commentID",
func(c *Context) error {
_ = c.Param("orgID")
_ = c.Param("projectID")
_ = c.Param("issueID")
_ = c.Param("commentID")
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/api/v1/organizations/123/projects/456/issues/789/comments/101", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_NotFound benchmarks 404 lookup performance.
func BenchmarkRouter_NotFound(b *testing.B) {
router := New()
router.GET("/users", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/posts", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_MethodNotAllowed benchmarks 405 lookup performance.
func BenchmarkRouter_MethodNotAllowed(b *testing.B) {
router := New()
router.GET("/users", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("POST", "/users", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkContext_Param benchmarks Box.Param() extraction.
func BenchmarkContext_Param(b *testing.B) {
c := newContext()
c.params = []Param{
{Key: "id", Value: "123"},
{Key: "category", Value: "tech"},
{Key: "postID", Value: "456"},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = c.Param("id")
}
}
// BenchmarkContext_Query benchmarks Box.Query() extraction.
func BenchmarkContext_Query(b *testing.B) {
req := httptest.NewRequest("GET", "/test?page=1&limit=10&sort=asc", http.NoBody)
c := newContext()
c.Request = req
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = c.Query("page")
}
}
// BenchmarkRouter_RootPath benchmarks root path routing.
func BenchmarkRouter_RootPath(b *testing.B) {
router := New()
router.GET("/", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_LongStaticPath benchmarks long static paths.
func BenchmarkRouter_LongStaticPath(b *testing.B) {
router := New()
router.GET("/api/v1/organizations/settings/security/authentication/providers", func(c *Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest("GET", "/api/v1/organizations/settings/security/authentication/providers", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkRouter_MixedRoutes benchmarks mix of static and parametric routes.
func BenchmarkRouter_MixedRoutes(b *testing.B) {
router := New()
// Mix of static and parametric routes.
router.GET("/api/users", func(_ *Context) error { return nil })
router.GET("/api/users/:id", func(_ *Context) error { return nil })
router.GET("/api/posts", func(_ *Context) error { return nil })
router.GET("/api/posts/:category/:id", func(_ *Context) error { return nil })
router.GET("/api/comments/:id", func(_ *Context) error { return nil })
req := httptest.NewRequest("GET", "/api/posts/tech/42", http.NoBody)
w := httptest.NewRecorder()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, req)
}
}
// BenchmarkContext_JSON benchmarks JSON response encoding.
func BenchmarkContext_JSON(b *testing.B) {
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", http.NoBody)
c := newContext()
c.Response = w
c.Request = req
data := map[string]string{
"id": "123",
"name": "John Doe",
"email": "john@example.com",
"status": "active",
"role": "admin",
"created": "2024-01-01",
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = c.JSON(200, data)
}
}
// BenchmarkContext_String benchmarks String response.
func BenchmarkContext_String(b *testing.B) {
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", http.NoBody)
c := newContext()
c.Response = w
c.Request = req
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = c.String(200, "Hello, World!")
}
}
// BenchmarkContext_Pooling benchmarks Context pooling efficiency.
func BenchmarkContext_Pooling(b *testing.B) {
router := New()
router.GET("/test", func(c *Context) error {
return c.String(200, "OK")
})
req := httptest.NewRequest("GET", "/test", http.NoBody)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
}
}