2022-09-28 09:39:01 +02:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2022-09-30 09:38:44 +02:00
|
|
|
"sync/atomic"
|
2022-09-28 09:39:01 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkFastStringTransformer(b *testing.B) {
|
|
|
|
for _, s := range []string{"", "foo", "foo-bar-baz", "http_requests_total"} {
|
|
|
|
b.Run(s, func(b *testing.B) {
|
|
|
|
benchmarkFastStringTransformer(b, s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkFastStringTransformer(b *testing.B, s string) {
|
|
|
|
fst := NewFastStringTransformer(strings.ToUpper)
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(1)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2022-09-30 09:38:44 +02:00
|
|
|
n := uint64(0)
|
2022-09-28 09:39:01 +02:00
|
|
|
for pb.Next() {
|
|
|
|
sTransformed := fst.Transform(s)
|
2022-09-30 09:38:44 +02:00
|
|
|
n += uint64(len(sTransformed))
|
2022-09-28 09:39:01 +02:00
|
|
|
}
|
2022-09-30 09:38:44 +02:00
|
|
|
atomic.AddUint64(&GlobalSink, n)
|
2022-09-28 09:39:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:38:44 +02:00
|
|
|
var GlobalSink uint64
|