lib/bytesutil: make BenchmarkToUnsafeString and BenchmarkToUnsafeBytes more reliable

This is needed for https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5880
This commit is contained in:
Aliaksandr Valialkin 2024-02-29 16:23:31 +02:00
parent 8266b77d0e
commit a3cf3d7de1
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -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