From 919d2dc90eebe71330896a7a81f7dbf5ad40edd5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 24 Sep 2024 18:31:21 +0200 Subject: [PATCH] lib/logstorage: add benchmark for streamID.marshalString --- lib/logstorage/stream_id_test.go | 24 +++++++++++++--------- lib/logstorage/stream_id_timing_test.go | 27 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 lib/logstorage/stream_id_timing_test.go diff --git a/lib/logstorage/stream_id_test.go b/lib/logstorage/stream_id_test.go index 830b9be45..e94ff334b 100644 --- a/lib/logstorage/stream_id_test.go +++ b/lib/logstorage/stream_id_test.go @@ -6,23 +6,27 @@ import ( ) func TestStreamIDMarshalUnmarshalString(t *testing.T) { - f := func(sid *streamID) { + f := func(sid *streamID, resultExpected string) { t.Helper() - s := string(sid.marshalString(nil)) + result := string(sid.marshalString(nil)) - var sid2 streamID - if !sid2.tryUnmarshalFromString(s) { - t.Fatalf("cannot unmarshal streamID from %q", s) + if result != resultExpected { + t.Fatalf("unexpected result\ngot\n%q\nwant\n%q", result, resultExpected) } - s2 := string(sid2.marshalString(nil)) - if s != s2 { - t.Fatalf("unexpected marshaled streamID; got %s; want %s", s2, s) + var sid2 streamID + if !sid2.tryUnmarshalFromString(result) { + 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{ tenantID: TenantID{ AccountID: 123, @@ -32,7 +36,7 @@ func TestStreamIDMarshalUnmarshalString(t *testing.T) { lo: 89, hi: 344334, }, - }) + }, "0000007b000001c8000000000005410e0000000000000059") } func TestStreamIDMarshalUnmarshal(t *testing.T) { diff --git a/lib/logstorage/stream_id_timing_test.go b/lib/logstorage/stream_id_timing_test.go new file mode 100644 index 000000000..263c6303f --- /dev/null +++ b/lib/logstorage/stream_id_timing_test.go @@ -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]) + } + }) +}