From abc305483de3d7e3e378798e79131d2cbb84895c Mon Sep 17 00:00:00 2001 From: wyrapeseed Date: Thu, 16 Oct 2025 01:25:46 +0800 Subject: [PATCH] refactor: use b.Loop() in benchmark tests for better performance (#25461) Signed-off-by: wyrapeseed Co-authored-by: Alex | Cosmos Labs --- codec/types/any_test.go | 3 +-- contrib/x/nft/internal/conv/string_test.go | 2 +- internal/conv/string_test.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/codec/types/any_test.go b/codec/types/any_test.go index a7914d9484..e8d61de25e 100644 --- a/codec/types/any_test.go +++ b/codec/types/any_test.go @@ -56,9 +56,8 @@ func TestNewAnyWithCustomTypeURLWithErrorNoAllocation(t *testing.T) { var sink any func BenchmarkNewAnyWithCustomTypeURLWithErrorReturned(b *testing.B) { - b.ResetTimer() b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { any, err := types.NewAnyWithValue(eom) if err == nil { b.Fatal("err wasn't returned") diff --git a/contrib/x/nft/internal/conv/string_test.go b/contrib/x/nft/internal/conv/string_test.go index 4bdba63565..8c7328c288 100644 --- a/contrib/x/nft/internal/conv/string_test.go +++ b/contrib/x/nft/internal/conv/string_test.go @@ -52,7 +52,7 @@ func (s *StringSuite) TestUnsafeBytesToStr() { } func BenchmarkUnsafeStrToBytes(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { UnsafeStrToBytes(strconv.Itoa(i)) } } diff --git a/internal/conv/string_test.go b/internal/conv/string_test.go index c57205099b..e8f9589f99 100644 --- a/internal/conv/string_test.go +++ b/internal/conv/string_test.go @@ -52,7 +52,7 @@ func (s *StringSuite) TestUnsafeBytesToStr() { } func BenchmarkUnsafeStrToBytes(b *testing.B) { - for i := 0; i < b.N; i++ { + for i := 0; b.Loop(); i++ { UnsafeStrToBytes(strconv.Itoa(i)) } }