2022-09-30 09:38:44 +02:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkFastStringMatcher(b *testing.B) {
|
|
|
|
for _, s := range []string{"", "foo", "foo-bar-baz", "http_requests_total"} {
|
|
|
|
b.Run(s, func(b *testing.B) {
|
|
|
|
benchmarkFastStringMatcher(b, s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkFastStringMatcher(b *testing.B, s string) {
|
|
|
|
fsm := NewFastStringMatcher(func(s string) bool {
|
|
|
|
return strings.HasPrefix(s, "foo")
|
|
|
|
})
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(1)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
n := uint64(0)
|
|
|
|
for pb.Next() {
|
|
|
|
v := fsm.Match(s)
|
|
|
|
if v {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 01:07:51 +01:00
|
|
|
GlobalSink.Add(n)
|
2022-09-30 09:38:44 +02:00
|
|
|
})
|
|
|
|
}
|