Skip to content

Commit

Permalink
v3: Add Benchmarks for Rewrite Middleware (#3092)
Browse files Browse the repository at this point in the history
Add Benchmarks for Rewrite Middleware
  • Loading branch information
gaby committed Jul 26, 2024
1 parent 0592e01 commit 99173cc
Showing 1 changed file with 207 additions and 0 deletions.
207 changes: 207 additions & 0 deletions middleware/rewrite/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
)

func Test_New(t *testing.T) {
Expand Down Expand Up @@ -170,3 +171,209 @@ func Test_Rewrite(t *testing.T) {
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
}

func Benchmark_Rewrite(b *testing.B) {
// Helper function to create a new Fiber app with rewrite middleware
createApp := func(config Config) *fiber.App {
app := fiber.New()
app.Use(New(config))
return app
}

// Benchmark: Rewrite with Next function always returns true
b.Run("Next always true", func(b *testing.B) {
app := createApp(Config{
Next: func(fiber.Ctx) bool {
return true
},
Rules: map[string]string{
"/old": "/new",
},
})

reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/old")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.Handler()(reqCtx)
}
})

// Benchmark: Rewrite with Next function always returns false
b.Run("Next always false", func(b *testing.B) {
app := createApp(Config{
Next: func(fiber.Ctx) bool {
return false
},
Rules: map[string]string{
"/old": "/new",
},
})

reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/old")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.Handler()(reqCtx)
}
})

// Benchmark: Rewrite with tokens
b.Run("Rewrite with tokens", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})

reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/users/123/orders/456")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.Handler()(reqCtx)
}
})

// Benchmark: Non-matching request, handled by default route
b.Run("NonMatch with default", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})
app.Use(func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})

reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/not-matching-any-rule")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.Handler()(reqCtx)
}
})

// Benchmark: Non-matching request, with no default route
b.Run("NonMatch without default", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})

reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/not-matching-any-rule")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
app.Handler()(reqCtx)
}
})
}

func Benchmark_Rewrite_Parallel(b *testing.B) {
// Helper function to create a new Fiber app with rewrite middleware
createApp := func(config Config) *fiber.App {
app := fiber.New()
app.Use(New(config))
return app
}

// Parallel Benchmark: Rewrite with Next function always returns true
b.Run("Next always true", func(b *testing.B) {
app := createApp(Config{
Next: func(fiber.Ctx) bool {
return true
},
Rules: map[string]string{
"/old": "/new",
},
})

b.RunParallel(func(pb *testing.PB) {
reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/old")
for pb.Next() {
app.Handler()(reqCtx)
}
})
})

// Parallel Benchmark: Rewrite with Next function always returns false
b.Run("Next always false", func(b *testing.B) {
app := createApp(Config{
Next: func(fiber.Ctx) bool {
return false
},
Rules: map[string]string{
"/old": "/new",
},
})

b.RunParallel(func(pb *testing.PB) {
reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/old")
for pb.Next() {
app.Handler()(reqCtx)
}
})
})

// Parallel Benchmark: Rewrite with tokens
b.Run("Rewrite with tokens", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})

b.RunParallel(func(pb *testing.PB) {
reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/users/123/orders/456")
for pb.Next() {
app.Handler()(reqCtx)
}
})
})

// Parallel Benchmark: Non-matching request, handled by default route
b.Run("NonMatch with default", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})
app.Use(func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})

b.RunParallel(func(pb *testing.PB) {
reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/not-matching-any-rule")
for pb.Next() {
app.Handler()(reqCtx)
}
})
})

// Parallel Benchmark: Non-matching request, with no default route
b.Run("NonMatch without default", func(b *testing.B) {
app := createApp(Config{
Rules: map[string]string{
"/users/*/orders/*": "/user/$1/order/$2",
},
})

b.RunParallel(func(pb *testing.PB) {
reqCtx := &fasthttp.RequestCtx{}
reqCtx.Request.SetRequestURI("/not-matching-any-rule")
for pb.Next() {
app.Handler()(reqCtx)
}
})
})
}

1 comment on commit 99173cc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 99173cc Previous: 87bb93e Ratio
Benchmark_Middleware_BasicAuth - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_Middleware_BasicAuth_Upper - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth_Upper - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_CORS_NewHandler - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandler - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflight 1153 ns/op 104 B/op 5 allocs/op 759.2 ns/op 0 B/op 0 allocs/op 1.52
Benchmark_CORS_NewHandlerPreflight - ns/op 1153 ns/op 759.2 ns/op 1.52
Benchmark_CORS_NewHandlerPreflight - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflight - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin 1155 ns/op 104 B/op 5 allocs/op 757.5 ns/op 0 B/op 0 allocs/op 1.52
Benchmark_CORS_NewHandlerPreflightSingleOrigin - ns/op 1155 ns/op 757.5 ns/op 1.52
Benchmark_CORS_NewHandlerPreflightSingleOrigin - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard 1062 ns/op 104 B/op 5 allocs/op 691 ns/op 0 B/op 0 allocs/op 1.54
Benchmark_CORS_NewHandlerPreflightWildcard - ns/op 1062 ns/op 691 ns/op 1.54
Benchmark_CORS_NewHandlerPreflightWildcard - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_Middleware_CSRF_Check - allocs/op 11 allocs/op 7 allocs/op 1.57
Benchmark_Middleware_CSRF_GenerateToken - B/op 512 B/op 326 B/op 1.57
Benchmark_Middleware_CSRF_GenerateToken - allocs/op 10 allocs/op 6 allocs/op 1.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.