Skip to content

Commit 13ac7eb

Browse files
authored
Merge pull request #4042 from gofiber/add-parallel-benchmarks-to-test-files
2 parents ca1bb6a + 9be4804 commit 13ac7eb

File tree

3 files changed

+405
-30
lines changed

3 files changed

+405
-30
lines changed

app_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,14 @@ func Benchmark_NewError(b *testing.B) {
18811881
}
18821882
}
18831883

1884+
func Benchmark_NewError_Parallel(b *testing.B) {
1885+
b.RunParallel(func(pb *testing.PB) {
1886+
for pb.Next() {
1887+
NewError(200, "test") //nolint:errcheck // not needed
1888+
}
1889+
})
1890+
}
1891+
18841892
// go test -run Test_NewError
18851893
func Test_NewError(t *testing.T) {
18861894
t.Parallel()
@@ -2835,6 +2843,34 @@ func Benchmark_Communication_Flow(b *testing.B) {
28352843
require.Equal(b, "Hello, World!", string(fctx.Response.Body()))
28362844
}
28372845

2846+
func Benchmark_Communication_Flow_Parallel(b *testing.B) {
2847+
app := New()
2848+
2849+
app.Get("/", func(c Ctx) error {
2850+
return c.SendString("Hello, World!")
2851+
})
2852+
2853+
h := app.Handler()
2854+
2855+
b.ReportAllocs()
2856+
b.RunParallel(func(pb *testing.PB) {
2857+
fctx := &fasthttp.RequestCtx{}
2858+
fctx.Request.Header.SetMethod(MethodGet)
2859+
fctx.Request.SetRequestURI("/")
2860+
for pb.Next() {
2861+
h(fctx)
2862+
}
2863+
})
2864+
2865+
verifyCtx := &fasthttp.RequestCtx{}
2866+
verifyCtx.Request.Header.SetMethod(MethodGet)
2867+
verifyCtx.Request.SetRequestURI("/")
2868+
h(verifyCtx)
2869+
2870+
require.Equal(b, 200, verifyCtx.Response.Header.StatusCode())
2871+
require.Equal(b, "Hello, World!", string(verifyCtx.Response.Body()))
2872+
}
2873+
28382874
// go test -v -run=^$ -bench=Benchmark_Ctx_AcquireReleaseFlow -benchmem -count=4
28392875
func Benchmark_Ctx_AcquireReleaseFlow(b *testing.B) {
28402876
app := New()
@@ -2860,6 +2896,44 @@ func Benchmark_Ctx_AcquireReleaseFlow(b *testing.B) {
28602896
})
28612897
}
28622898

2899+
func acquireDefaultCtxForAppBenchmark(b *testing.B, app *App, fctx *fasthttp.RequestCtx) *DefaultCtx {
2900+
b.Helper()
2901+
2902+
ctx := app.AcquireCtx(fctx)
2903+
defaultCtx, ok := ctx.(*DefaultCtx)
2904+
if !ok {
2905+
b.Fatal("AcquireCtx did not return *DefaultCtx")
2906+
}
2907+
return defaultCtx
2908+
}
2909+
2910+
func Benchmark_Ctx_AcquireReleaseFlow_Parallel(b *testing.B) {
2911+
app := New()
2912+
2913+
b.Run("withoutRequestCtx", func(b *testing.B) {
2914+
b.ReportAllocs()
2915+
2916+
b.RunParallel(func(pb *testing.PB) {
2917+
fctx := &fasthttp.RequestCtx{}
2918+
for pb.Next() {
2919+
c := acquireDefaultCtxForAppBenchmark(b, app, fctx)
2920+
app.ReleaseCtx(c)
2921+
}
2922+
})
2923+
})
2924+
2925+
b.Run("withRequestCtx", func(b *testing.B) {
2926+
b.ReportAllocs()
2927+
2928+
b.RunParallel(func(pb *testing.PB) {
2929+
for pb.Next() {
2930+
c := acquireDefaultCtxForAppBenchmark(b, app, &fasthttp.RequestCtx{})
2931+
app.ReleaseCtx(c)
2932+
}
2933+
})
2934+
})
2935+
}
2936+
28632937
func TestErrorHandler_PicksRightOne(t *testing.T) {
28642938
t.Parallel()
28652939
// common handler to be used by all routes,

ctx_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8431,21 +8431,6 @@ func Benchmark_Ctx_IsFromLocalhost(b *testing.B) {
84318431
app.ReleaseCtx(c)
84328432
})
84338433

8434-
// Scenario without localhost check in parallel
8435-
b.Run("Non_Localhost_Parallel", func(b *testing.B) {
8436-
app := New()
8437-
b.ReportAllocs()
8438-
b.ResetTimer()
8439-
b.RunParallel(func(pb *testing.PB) {
8440-
c := app.AcquireCtx(&fasthttp.RequestCtx{})
8441-
c.Request().SetRequestURI("http://google.com:8080/test")
8442-
for pb.Next() {
8443-
c.IsFromLocal()
8444-
}
8445-
app.ReleaseCtx(c)
8446-
})
8447-
})
8448-
84498434
// Scenario with localhost check
84508435
b.Run("Localhost", func(b *testing.B) {
84518436
app := New()
@@ -8457,21 +8442,6 @@ func Benchmark_Ctx_IsFromLocalhost(b *testing.B) {
84578442
}
84588443
app.ReleaseCtx(c)
84598444
})
8460-
8461-
// Scenario with localhost check in parallel
8462-
b.Run("Localhost_Parallel", func(b *testing.B) {
8463-
app := New()
8464-
b.ReportAllocs()
8465-
b.ResetTimer()
8466-
b.RunParallel(func(pb *testing.PB) {
8467-
c := app.AcquireCtx(&fasthttp.RequestCtx{})
8468-
c.Request().SetRequestURI("http://localhost:8080/test")
8469-
for pb.Next() {
8470-
c.IsFromLocal()
8471-
}
8472-
app.ReleaseCtx(c)
8473-
})
8474-
})
84758445
}
84768446

84778447
// go test -v -run=^$ -bench=Benchmark_Ctx_OverrideParam -benchmem -count=4

0 commit comments

Comments
 (0)