2023-05-10 19:57:01 +00:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkToUnsafeString(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2024-02-29 14:23:31 +00:00
|
|
|
b.SetBytes(int64(len(benchBytes)))
|
2023-05-10 22:53:58 +00:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 19:57:01 +00:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
2024-02-29 14:23:31 +00:00
|
|
|
for _, b := range benchBytes {
|
|
|
|
s := ToUnsafeString(b)
|
2023-05-10 19:57:01 +00:00
|
|
|
n += len(s)
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 19:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkToUnsafeBytes(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2024-02-29 14:23:31 +00:00
|
|
|
b.SetBytes(int64(len(benchStrings)))
|
2023-05-10 22:53:58 +00:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 19:57:01 +00:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
2024-02-29 14:23:31 +00:00
|
|
|
for _, s := range benchStrings {
|
|
|
|
b := ToUnsafeBytes(s)
|
|
|
|
n += len(b)
|
2023-05-10 19:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 19:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-29 14:23:31 +00:00
|
|
|
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
|
|
|
|
}()
|
|
|
|
|
2024-02-24 00:07:51 +00:00
|
|
|
var Sink atomic.Uint64
|