2023-06-20 07:55:12 +02:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-20 04:08:30 +02:00
|
|
|
"sync/atomic"
|
2023-06-20 07:55:12 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-05-12 16:33:29 +02:00
|
|
|
func BenchmarkTryParseTimestampRFC3339Nano(b *testing.B) {
|
|
|
|
a := []string{
|
|
|
|
"2023-01-15T23:45:51Z",
|
|
|
|
"2023-02-15T23:45:51.123Z",
|
|
|
|
"2024-02-15T23:45:51.123456Z",
|
|
|
|
"2025-02-15T22:45:51.123456789Z",
|
|
|
|
"2023-02-15T22:45:51.000000000Z",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(a)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum := int64(0)
|
2024-05-12 16:33:29 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for _, s := range a {
|
2024-06-17 12:13:18 +02:00
|
|
|
n, ok := TryParseTimestampRFC3339Nano(s)
|
2024-05-12 16:33:29 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("cannot parse timestamp %q", s))
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum += n
|
2024-05-12 16:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
GlobalSink.Add(uint64(nSum))
|
2024-05-12 16:33:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-20 07:55:12 +02:00
|
|
|
func BenchmarkTryParseTimestampISO8601(b *testing.B) {
|
|
|
|
a := []string{
|
|
|
|
"2023-01-15T23:45:51.123Z",
|
|
|
|
"2023-02-15T23:45:51.123Z",
|
2024-05-12 16:33:29 +02:00
|
|
|
"2024-02-15T23:45:51.123Z",
|
|
|
|
"2025-02-15T22:45:51.123Z",
|
2023-06-20 07:55:12 +02:00
|
|
|
"2023-02-15T22:45:51.000Z",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(a)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum := int64(0)
|
2023-06-20 07:55:12 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for _, s := range a {
|
2024-06-17 12:13:18 +02:00
|
|
|
n, ok := TryParseTimestampISO8601(s)
|
2023-06-20 07:55:12 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("cannot parse timestamp %q", s))
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum += n
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
GlobalSink.Add(uint64(nSum))
|
2023-06-20 07:55:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTryParseIPv4(b *testing.B) {
|
|
|
|
a := []string{
|
|
|
|
"1.2.3.4",
|
|
|
|
"127.0.0.1",
|
|
|
|
"255.255.255.255",
|
|
|
|
"192.43.234.22",
|
|
|
|
"32.34.54.198",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(a)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum := uint32(0)
|
2023-06-20 07:55:12 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for _, s := range a {
|
2024-05-20 04:08:30 +02:00
|
|
|
n, ok := tryParseIPv4(s)
|
2023-06-20 07:55:12 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("cannot parse ipv4 %q", s))
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum += n
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
GlobalSink.Add(uint64(nSum))
|
2023-06-20 07:55:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTryParseUint64(b *testing.B) {
|
|
|
|
a := []string{
|
|
|
|
"1234",
|
|
|
|
"483932",
|
|
|
|
"28494",
|
|
|
|
"90012",
|
|
|
|
"889111",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(a)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2024-05-20 04:08:30 +02:00
|
|
|
var nSum uint64
|
2023-06-20 07:55:12 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for _, s := range a {
|
2024-05-20 04:08:30 +02:00
|
|
|
n, ok := tryParseUint64(s)
|
2023-06-20 07:55:12 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("cannot parse uint %q", s))
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
nSum += n
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
GlobalSink.Add(nSum)
|
2023-06-20 07:55:12 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTryParseFloat64(b *testing.B) {
|
|
|
|
a := []string{
|
|
|
|
"1.234",
|
|
|
|
"4.545",
|
|
|
|
"456.5645",
|
|
|
|
"-123.434",
|
|
|
|
"434.322",
|
|
|
|
}
|
|
|
|
|
|
|
|
b.SetBytes(int64(len(a)))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2024-05-20 04:08:30 +02:00
|
|
|
var fSum float64
|
2023-06-20 07:55:12 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for _, s := range a {
|
2024-05-20 04:08:30 +02:00
|
|
|
f, ok := tryParseFloat64(s)
|
2023-06-20 07:55:12 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("cannot parse float64 %q", s))
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
fSum += f
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
GlobalSink.Add(uint64(fSum))
|
2023-06-20 07:55:12 +02:00
|
|
|
})
|
|
|
|
}
|
2024-05-20 04:08:30 +02:00
|
|
|
|
|
|
|
func BenchmarkMarshalUint8String(b *testing.B) {
|
|
|
|
b.SetBytes(256)
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var buf []byte
|
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
buf = marshalUint8String(buf[:0], uint8(i))
|
|
|
|
n += len(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GlobalSink.Add(uint64(n))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var GlobalSink atomic.Uint64
|