♻️ v3: (refactor): Add parallel benchmarks to adaptor middleware (#2870)

Simplify benchmarks, add parallel benchmarks
pull/2875/head
Juan Calderon-Perez 2024-02-21 02:17:27 -05:00 committed by GitHub
parent d123008fcc
commit 26346d6908
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 106 additions and 61 deletions

View File

@ -399,86 +399,131 @@ func Test_ConvertRequest(t *testing.T) {
} }
// Benchmark for FiberHandlerFunc // Benchmark for FiberHandlerFunc
func Benchmark_FiberHandlerFunc_1MB(b *testing.B) { func Benchmark_FiberHandlerFunc(b *testing.B) {
benchmarks := []struct {
name string
bodyContent []byte
}{
{
name: "100KB",
bodyContent: make([]byte, 100*1024),
},
{
name: "500KB",
bodyContent: make([]byte, 500*1024),
},
{
name: "1MB",
bodyContent: make([]byte, 1*1024*1024),
},
{
name: "5MB",
bodyContent: make([]byte, 5*1024*1024),
},
{
name: "10MB",
bodyContent: make([]byte, 10*1024*1024),
},
{
name: "25MB",
bodyContent: make([]byte, 25*1024*1024),
},
{
name: "50MB",
bodyContent: make([]byte, 50*1024*1024),
},
}
fiberH := func(c fiber.Ctx) error { fiberH := func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }
handlerFunc := FiberHandlerFunc(fiberH) handlerFunc := FiberHandlerFunc(fiberH)
// Create body content for _, bm := range benchmarks {
bodyContent := make([]byte, 1*1024*1024) b.Run(bm.name, func(b *testing.B) {
bodyBuffer := bytes.NewBuffer(bodyContent) w := httptest.NewRecorder()
bodyBuffer := bytes.NewBuffer(bm.bodyContent)
r := http.Request{ r := http.Request{
Method: http.MethodPost, Method: http.MethodPost,
Body: http.NoBody, Body: http.NoBody,
} }
// Replace the empty Body with our buffer // Replace the empty Body with our buffer
r.Body = io.NopCloser(bodyBuffer) r.Body = io.NopCloser(bodyBuffer)
defer r.Body.Close() //nolint:errcheck // not needed defer r.Body.Close() //nolint:errcheck // not needed
// Create recorder b.ReportAllocs()
w := httptest.NewRecorder() b.ResetTimer()
b.ResetTimer() for i := 0; i < b.N; i++ {
for i := 0; i < b.N; i++ { handlerFunc.ServeHTTP(w, &r)
handlerFunc.ServeHTTP(w, &r) }
})
} }
} }
func Benchmark_FiberHandlerFunc_10MB(b *testing.B) { func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) {
benchmarks := []struct {
name string
bodyContent []byte
}{
{
name: "100KB",
bodyContent: make([]byte, 100*1024),
},
{
name: "500KB",
bodyContent: make([]byte, 500*1024),
},
{
name: "1MB",
bodyContent: make([]byte, 1*1024*1024),
},
{
name: "5MB",
bodyContent: make([]byte, 5*1024*1024),
},
{
name: "10MB",
bodyContent: make([]byte, 10*1024*1024),
},
{
name: "25MB",
bodyContent: make([]byte, 25*1024*1024),
},
{
name: "50MB",
bodyContent: make([]byte, 50*1024*1024),
},
}
fiberH := func(c fiber.Ctx) error { fiberH := func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }
handlerFunc := FiberHandlerFunc(fiberH) handlerFunc := FiberHandlerFunc(fiberH)
// Create body content for _, bm := range benchmarks {
bodyContent := make([]byte, 10*1024*1024) b.Run(bm.name, func(b *testing.B) {
bodyBuffer := bytes.NewBuffer(bodyContent) bodyBuffer := bytes.NewBuffer(bm.bodyContent)
b.ReportAllocs()
b.ResetTimer()
r := http.Request{ b.RunParallel(func(pb *testing.PB) {
Method: http.MethodPost, w := httptest.NewRecorder()
Body: http.NoBody, r := http.Request{
} Method: http.MethodPost,
Body: http.NoBody,
}
// Replace the empty Body with our buffer // Replace the empty Body with our buffer
r.Body = io.NopCloser(bodyBuffer) r.Body = io.NopCloser(bodyBuffer)
defer r.Body.Close() //nolint:errcheck // not needed defer r.Body.Close() //nolint:errcheck // not needed
// Create recorder for pb.Next() {
w := httptest.NewRecorder() handlerFunc(w, &r)
}
b.ResetTimer() })
for i := 0; i < b.N; i++ { })
handlerFunc.ServeHTTP(w, &r)
}
}
func Benchmark_FiberHandlerFunc_50MB(b *testing.B) {
fiberH := func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
handlerFunc := FiberHandlerFunc(fiberH)
// Create body content
bodyContent := make([]byte, 50*1024*1024)
bodyBuffer := bytes.NewBuffer(bodyContent)
r := http.Request{
Method: http.MethodPost,
Body: http.NoBody,
}
// Replace the empty Body with our buffer
r.Body = io.NopCloser(bodyBuffer)
defer r.Body.Close() //nolint:errcheck // not needed
// Create recorder
w := httptest.NewRecorder()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handlerFunc.ServeHTTP(w, &r)
} }
} }