From a3cf3d7de156a2f1d022b8b46d21c1914f34106b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 29 Feb 2024 16:23:31 +0200 Subject: [PATCH] lib/bytesutil: make BenchmarkToUnsafeString and BenchmarkToUnsafeBytes more reliable This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5880 --- lib/bytesutil/bytesutil_timing_test.go | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/bytesutil/bytesutil_timing_test.go b/lib/bytesutil/bytesutil_timing_test.go index 6ba8b0d90..03a15541d 100644 --- a/lib/bytesutil/bytesutil_timing_test.go +++ b/lib/bytesutil/bytesutil_timing_test.go @@ -6,14 +6,13 @@ import ( ) func BenchmarkToUnsafeString(b *testing.B) { - buf := []byte("foobarbaz abcde fafdsfds") b.ReportAllocs() - b.SetBytes(int64(len(buf))) + b.SetBytes(int64(len(benchBytes))) b.RunParallel(func(pb *testing.PB) { n := 0 for pb.Next() { - for i := range buf { - s := ToUnsafeString(buf[:i]) + for _, b := range benchBytes { + s := ToUnsafeString(b) n += len(s) } } @@ -22,19 +21,34 @@ func BenchmarkToUnsafeString(b *testing.B) { } func BenchmarkToUnsafeBytes(b *testing.B) { - s := "foobarbaz abcde fafdsfds" b.ReportAllocs() - b.SetBytes(int64(len(s))) + b.SetBytes(int64(len(benchStrings))) b.RunParallel(func(pb *testing.PB) { n := 0 for pb.Next() { - for i := range s { - s := ToUnsafeBytes(s[:i]) - n += len(s) + for _, s := range benchStrings { + b := ToUnsafeBytes(s) + n += len(b) } } Sink.Add(uint64(n)) }) } +var benchBytes = func() [][]byte { + a := make([][]byte, 1000) + for i := range a { + a[i] = make([]byte, i) + } + return a +}() + +var benchStrings = func() []string { + a := make([]string, 1000) + for i := range a { + a[i] = string(make([]byte, i)) + } + return a +}() + var Sink atomic.Uint64