From b7239c2221bdc893b9d980f4369f53a57eb582fb Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 10 May 2023 12:57:01 -0700 Subject: [PATCH] lib/bytesutil: add benchmarks for ToUnsafeString() and ToUnsafeBytes() --- lib/bytesutil/bytesutil_timing_test.go | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/bytesutil/bytesutil_timing_test.go diff --git a/lib/bytesutil/bytesutil_timing_test.go b/lib/bytesutil/bytesutil_timing_test.go new file mode 100644 index 000000000..7a320d43c --- /dev/null +++ b/lib/bytesutil/bytesutil_timing_test.go @@ -0,0 +1,40 @@ +package bytesutil + +import ( + "sync/atomic" + "testing" +) + +func BenchmarkToUnsafeString(b *testing.B) { + buf := []byte("foobarbaz abcde fafdsfds") + b.ReportAllocs() + b.SetBytes(int64(len(buf))) + b.RunParallel(func (pb *testing.PB) { + n := 0 + for pb.Next() { + for i := range buf { + s := ToUnsafeString(buf[:i]) + n += len(s) + } + } + atomic.AddUint64(&Sink, uint64(n)) + }) +} + +func BenchmarkToUnsafeBytes(b *testing.B) { + s := "foobarbaz abcde fafdsfds" + b.ReportAllocs() + b.SetBytes(int64(len(s))) + b.RunParallel(func (pb *testing.PB) { + n := 0 + for pb.Next() { + for i := range s { + s := ToUnsafeBytes(s[:i]) + n += len(s) + } + } + atomic.AddUint64(&Sink, uint64(n)) + }) +} + +var Sink uint64