lib/logstorage: add benchmark for streamID.marshalString

This commit is contained in:
Aliaksandr Valialkin 2024-09-24 18:31:21 +02:00
parent 9a0f697622
commit 919d2dc90e
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 41 additions and 10 deletions

View file

@ -6,23 +6,27 @@ import (
) )
func TestStreamIDMarshalUnmarshalString(t *testing.T) { func TestStreamIDMarshalUnmarshalString(t *testing.T) {
f := func(sid *streamID) { f := func(sid *streamID, resultExpected string) {
t.Helper() t.Helper()
s := string(sid.marshalString(nil)) result := string(sid.marshalString(nil))
var sid2 streamID if result != resultExpected {
if !sid2.tryUnmarshalFromString(s) { t.Fatalf("unexpected result\ngot\n%q\nwant\n%q", result, resultExpected)
t.Fatalf("cannot unmarshal streamID from %q", s)
} }
s2 := string(sid2.marshalString(nil)) var sid2 streamID
if s != s2 { if !sid2.tryUnmarshalFromString(result) {
t.Fatalf("unexpected marshaled streamID; got %s; want %s", s2, s) t.Fatalf("cannot unmarshal streamID from %q", result)
}
result2 := string(sid2.marshalString(nil))
if result != result2 {
t.Fatalf("unexpected marshaled streamID; got %s; want %s", result2, result)
} }
} }
f(&streamID{}) f(&streamID{}, "000000000000000000000000000000000000000000000000")
f(&streamID{ f(&streamID{
tenantID: TenantID{ tenantID: TenantID{
AccountID: 123, AccountID: 123,
@ -32,7 +36,7 @@ func TestStreamIDMarshalUnmarshalString(t *testing.T) {
lo: 89, lo: 89,
hi: 344334, hi: 344334,
}, },
}) }, "0000007b000001c8000000000005410e0000000000000059")
} }
func TestStreamIDMarshalUnmarshal(t *testing.T) { func TestStreamIDMarshalUnmarshal(t *testing.T) {

View file

@ -0,0 +1,27 @@
package logstorage
import (
"testing"
)
func BenchmarkStreamIDMarshalString(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
sid := &streamID{
tenantID: TenantID{
AccountID: 123,
ProjectID: 456,
},
id: u128{
lo: 89,
hi: 344334,
},
}
b.RunParallel(func(pb *testing.PB) {
var b []byte
for pb.Next() {
b = sid.marshalString(b[:0])
}
})
}