Skip to content

Commit 588c288

Browse files
committed
test: add compile benchmark
1 parent fd397e9 commit 588c288

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

rmhttp_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,31 @@ func Test_Compile(t *testing.T) {
324324
assert.Equal(t, "mw1", res.Header.Get("x-mw1"), "they should be equal")
325325
assert.Equal(t, "mw2", res.Header.Get("x-mw2"), "they should be equal")
326326
}
327+
328+
// Benchmark_Compile benchmarks the performance of compiling routes with middleware.
329+
// It sets up an app with multiple routes and groups to simulate real-world usage.
330+
func Benchmark_Compile(b *testing.B) {
331+
// Setup: create an app with a variety of routes and groups
332+
app := New()
333+
334+
// Add direct routes
335+
for i := range 50 {
336+
app.Get(fmt.Sprintf("/route%d", i), createTestHandlerFunc(200, "ok"))
337+
}
338+
339+
// Add groups with nested routes
340+
for i := range 10 {
341+
g := app.Group(fmt.Sprintf("/group%d", i))
342+
for j := range 10 {
343+
g.Get(fmt.Sprintf("/sub%d", j), createTestHandlerFunc(200, "ok"))
344+
}
345+
// Add some middleware to groups for realism
346+
g.Use(createTestMiddlewareHandler("x-group", fmt.Sprintf("group%d", i)))
347+
}
348+
349+
// Benchmark Compile() - reset the router each iteration to avoid conflicts
350+
for b.Loop() {
351+
app.Router = NewRouter()
352+
app.Compile()
353+
}
354+
}

0 commit comments

Comments
 (0)