Skip to content

Commit fdc7fde

Browse files
valyalatruepele
authored andcommitted
lib/logstorage: add benchmark for streamID.marshalString
1 parent 91b5d12 commit fdc7fde

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

lib/logstorage/stream_id_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ import (
66
)
77

88
func TestStreamIDMarshalUnmarshalString(t *testing.T) {
9-
f := func(sid *streamID) {
9+
f := func(sid *streamID, resultExpected string) {
1010
t.Helper()
1111

12-
s := string(sid.marshalString(nil))
12+
result := string(sid.marshalString(nil))
13+
14+
if result != resultExpected {
15+
t.Fatalf("unexpected result\ngot\n%q\nwant\n%q", result, resultExpected)
16+
}
1317

1418
var sid2 streamID
15-
if !sid2.tryUnmarshalFromString(s) {
16-
t.Fatalf("cannot unmarshal streamID from %q", s)
19+
if !sid2.tryUnmarshalFromString(result) {
20+
t.Fatalf("cannot unmarshal streamID from %q", result)
1721
}
1822

19-
s2 := string(sid2.marshalString(nil))
20-
if s != s2 {
21-
t.Fatalf("unexpected marshaled streamID; got %s; want %s", s2, s)
23+
result2 := string(sid2.marshalString(nil))
24+
if result != result2 {
25+
t.Fatalf("unexpected marshaled streamID; got %s; want %s", result2, result)
2226
}
2327
}
2428

25-
f(&streamID{})
29+
f(&streamID{}, "000000000000000000000000000000000000000000000000")
2630
f(&streamID{
2731
tenantID: TenantID{
2832
AccountID: 123,
@@ -32,7 +36,7 @@ func TestStreamIDMarshalUnmarshalString(t *testing.T) {
3236
lo: 89,
3337
hi: 344334,
3438
},
35-
})
39+
}, "0000007b000001c8000000000005410e0000000000000059")
3640
}
3741

3842
func TestStreamIDMarshalUnmarshal(t *testing.T) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package logstorage
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func BenchmarkStreamIDMarshalString(b *testing.B) {
8+
b.ReportAllocs()
9+
b.SetBytes(1)
10+
11+
sid := &streamID{
12+
tenantID: TenantID{
13+
AccountID: 123,
14+
ProjectID: 456,
15+
},
16+
id: u128{
17+
lo: 89,
18+
hi: 344334,
19+
},
20+
}
21+
b.RunParallel(func(pb *testing.PB) {
22+
var b []byte
23+
for pb.Next() {
24+
b = sid.marshalString(b[:0])
25+
}
26+
})
27+
}

0 commit comments

Comments
 (0)