From 2f1a48842739958cca1480db60904d89065524b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Tue, 23 Jun 2026 22:49:33 +0200 Subject: [PATCH] test(format): measure realistic allocations in Format/Append benchmarks The Format*/Append* benchmarks discarded their result with "_ =". For inlinable functions such as FormatUint, escape analysis then kept the returned string on the stack, reporting a misleading 0 allocs/op that real callers, which retain the string, never observe. Switch the loops to b.Loop() (Go 1.24+). b.Loop keeps the call results alive so the value escapes, and the benchmark measures the true allocation cost, giving a fair fiber-vs-strconv comparison. It also resolves the modernize linter's "bloop" suggestion for these loops. Measured (FormatUint/medium): before, "_ =" discard: 0 B/op, 0 allocs/op (misleading) after, b.Loop(): 16 B/op, 1 allocs/op (the real cost) No production code changed. Co-Authored-By: Claude Opus 4.8 (1M context) --- format_test.go | 80 +++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/format_test.go b/format_test.go index b878637..d25933c 100644 --- a/format_test.go +++ b/format_test.go @@ -152,14 +152,14 @@ func Benchmark_FormatUint(b *testing.B) { for _, input := range inputs { b.Run(input.name+"/fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatUint(input.value) + for b.Loop() { + FormatUint(input.value) } }) b.Run(input.name+"/strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatUint(input.value, 10) + for b.Loop() { + strconv.FormatUint(input.value, 10) } }) } @@ -181,14 +181,14 @@ func Benchmark_FormatInt(b *testing.B) { for _, input := range inputs { b.Run(input.name+"/fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatInt(input.value) + for b.Loop() { + FormatInt(input.value) } }) b.Run(input.name+"/strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatInt(input.value, 10) + for b.Loop() { + strconv.FormatInt(input.value, 10) } }) } @@ -199,14 +199,14 @@ func Benchmark_FormatUint32(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatUint32(input) + for b.Loop() { + FormatUint32(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatUint(uint64(input), 10) + for b.Loop() { + strconv.FormatUint(uint64(input), 10) } }) } @@ -216,14 +216,14 @@ func Benchmark_FormatInt32(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatInt32(input) + for b.Loop() { + FormatInt32(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatInt(int64(input), 10) + for b.Loop() { + strconv.FormatInt(int64(input), 10) } }) } @@ -233,14 +233,14 @@ func Benchmark_FormatUint16(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatUint16(input) + for b.Loop() { + FormatUint16(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatUint(uint64(input), 10) + for b.Loop() { + strconv.FormatUint(uint64(input), 10) } }) } @@ -250,14 +250,14 @@ func Benchmark_FormatInt16(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatInt16(input) + for b.Loop() { + FormatInt16(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatInt(int64(input), 10) + for b.Loop() { + strconv.FormatInt(int64(input), 10) } }) } @@ -267,14 +267,14 @@ func Benchmark_FormatUint8(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatUint8(input) + for b.Loop() { + FormatUint8(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatUint(uint64(input), 10) + for b.Loop() { + strconv.FormatUint(uint64(input), 10) } }) } @@ -284,14 +284,14 @@ func Benchmark_FormatInt8(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = FormatInt8(input) + for b.Loop() { + FormatInt8(input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.FormatInt(int64(input), 10) + for b.Loop() { + strconv.FormatInt(int64(input), 10) } }) } @@ -302,14 +302,14 @@ func Benchmark_AppendUint(b *testing.B) { b.Run("fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = AppendUint(dst, input) + for b.Loop() { + AppendUint(dst, input) } }) b.Run("strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.AppendUint(dst, input, 10) + for b.Loop() { + strconv.AppendUint(dst, input, 10) } }) } @@ -327,14 +327,14 @@ func Benchmark_AppendInt(b *testing.B) { for _, input := range inputs { b.Run(input.name+"/fiber", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = AppendInt(dst, input.value) + for b.Loop() { + AppendInt(dst, input.value) } }) b.Run(input.name+"/strconv", func(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.AppendInt(dst, input.value, 10) + for b.Loop() { + strconv.AppendInt(dst, input.value, 10) } }) }