Skip to content

Commit

Permalink
Add a new benchmark that tests the ctx acquire and release flow
Browse files Browse the repository at this point in the history
this will be used to show differences between version 2 and 3 directly
  • Loading branch information
ReneWerner87 committed Apr 3, 2024
1 parent a2702f6 commit 799df8e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,16 +1408,6 @@ func Test_App_Next_Method(t *testing.T) {
require.Equal(t, 404, resp.StatusCode, "Status code")
}

// go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
for n := 0; n < b.N; n++ {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

app.ReleaseCtx(c)
}
}

// go test -v -run=^$ -bench=Benchmark_NewError -benchmem -count=4
func Benchmark_NewError(b *testing.B) {
for n := 0; n < b.N; n++ {
Expand Down Expand Up @@ -2000,3 +1990,27 @@ func Benchmark_Communication_Flow(b *testing.B) {
require.Equal(b, 200, fctx.Response.Header.StatusCode())
require.Equal(b, "Hello, World!", string(fctx.Response.Body()))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_AcquireReleaseFlow -benchmem -count=4
func Benchmark_Ctx_AcquireReleaseFlow(b *testing.B) {
app := New()

fctx := &fasthttp.RequestCtx{}

b.ReportAllocs()
b.ResetTimer()

b.Run("withoutRequestCtx", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c, _ := app.AcquireCtx(fctx).(*DefaultCtx) //nolint:errcheck // not needed
app.ReleaseCtx(c)
}
})

b.Run("withRequestCtx", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c, _ := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck // not needed
app.ReleaseCtx(c)
}
})
}

0 comments on commit 799df8e

Please sign in to comment.