Skip to content

Commit 31f56cd

Browse files
committed
chunkenc: improve readability of compression benchmarks
For BenchmarkWrite: Use Go runtime facilities to report MB/s for each operation and compression ratio. Stop collecting all compressed chunks, as this blows up the memory of the benchmark and creates unrealistic test conditions for the later encoding. For BenchmarkRead: Replace reporting of MB/s which gave several values for each encoding as Go hunted for the right number of benchmark iterations. Add stats to the context for decoding, otherwise a new stats object is created each time round the loop. Re-order so all unsampled results come before all sampled results.
1 parent 557fdf2 commit 31f56cd

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

pkg/chunkenc/memchunk_test.go

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -643,11 +643,7 @@ func TestIteratorClose(t *testing.T) {
643643
}
644644
}
645645

646-
var result []Chunk
647-
648646
func BenchmarkWrite(b *testing.B) {
649-
chunks := []Chunk{}
650-
651647
entry := &logproto.Entry{
652648
Timestamp: time.Unix(0, 0),
653649
Line: testdata.LogString(0),
@@ -657,6 +653,7 @@ func BenchmarkWrite(b *testing.B) {
657653
for _, f := range HeadBlockFmts {
658654
for _, enc := range testEncoding {
659655
b.Run(fmt.Sprintf("%v-%v", f, enc), func(b *testing.B) {
656+
uncompressedBytes, compressedBytes := 0, 0
660657
for n := 0; n < b.N; n++ {
661658
c := NewMemChunk(enc, f, testBlockSize, testTargetSize)
662659
// adds until full so we trigger cut which serialize using gzip
@@ -666,9 +663,11 @@ func BenchmarkWrite(b *testing.B) {
666663
entry.Line = testdata.LogString(i)
667664
i++
668665
}
669-
chunks = append(chunks, c)
666+
uncompressedBytes += c.UncompressedSize()
667+
compressedBytes += c.CompressedSize()
670668
}
671-
result = chunks
669+
b.SetBytes(int64(uncompressedBytes) / int64(b.N))
670+
b.ReportMetric(float64(compressedBytes)/float64(uncompressedBytes)*100, "%compressed")
672671
})
673672
}
674673
}
@@ -685,23 +684,17 @@ func (nomatchPipeline) ProcessString(_ int64, line string) (string, log.LabelsRe
685684
}
686685

687686
func BenchmarkRead(b *testing.B) {
688-
type res struct {
689-
name string
690-
speed float64
691-
}
692-
result := []res{}
693687
for _, bs := range testBlockSizes {
694688
for _, enc := range testEncoding {
695689
name := fmt.Sprintf("%s_%s", enc.String(), humanize.Bytes(uint64(bs)))
696690
b.Run(name, func(b *testing.B) {
697691
chunks, size := generateData(enc, 5, bs, testTargetSize)
692+
_, ctx := stats.NewContext(context.Background())
698693
b.ResetTimer()
699-
bytesRead := uint64(0)
700-
now := time.Now()
701694
for n := 0; n < b.N; n++ {
702695
for _, c := range chunks {
703696
// use forward iterator for benchmark -- backward iterator does extra allocations by keeping entries in memory
704-
iterator, err := c.Iterator(context.Background(), time.Unix(0, 0), time.Now(), logproto.FORWARD, nomatchPipeline{})
697+
iterator, err := c.Iterator(ctx, time.Unix(0, 0), time.Now(), logproto.FORWARD, nomatchPipeline{})
705698
if err != nil {
706699
panic(err)
707700
}
@@ -712,24 +705,23 @@ func BenchmarkRead(b *testing.B) {
712705
b.Fatal(err)
713706
}
714707
}
715-
bytesRead += size
716708
}
717-
result = append(result, res{
718-
name: name,
719-
speed: float64(bytesRead) / time.Since(now).Seconds(),
720-
})
709+
b.SetBytes(int64(size))
721710
})
711+
}
712+
}
722713

723-
name = fmt.Sprintf("sample_%s_%s", enc.String(), humanize.Bytes(uint64(bs)))
724-
714+
for _, bs := range testBlockSizes {
715+
for _, enc := range testEncoding {
716+
name := fmt.Sprintf("sample_%s_%s", enc.String(), humanize.Bytes(uint64(bs)))
725717
b.Run(name, func(b *testing.B) {
726718
chunks, size := generateData(enc, 5, bs, testTargetSize)
719+
_, ctx := stats.NewContext(context.Background())
727720
b.ResetTimer()
728721
bytesRead := uint64(0)
729-
now := time.Now()
730722
for n := 0; n < b.N; n++ {
731723
for _, c := range chunks {
732-
iterator := c.SampleIterator(context.Background(), time.Unix(0, 0), time.Now(), countExtractor)
724+
iterator := c.SampleIterator(ctx, time.Unix(0, 0), time.Now(), countExtractor)
733725
for iterator.Next() {
734726
_ = iterator.Sample()
735727
}
@@ -739,19 +731,10 @@ func BenchmarkRead(b *testing.B) {
739731
}
740732
bytesRead += size
741733
}
742-
result = append(result, res{
743-
name: name,
744-
speed: float64(bytesRead) / time.Since(now).Seconds(),
745-
})
734+
b.SetBytes(int64(bytesRead) / int64(b.N))
746735
})
747736
}
748737
}
749-
sort.Slice(result, func(i, j int) bool {
750-
return result[i].speed > result[j].speed
751-
})
752-
for _, r := range result {
753-
fmt.Printf("%s: %.2f MB/s\n", r.name, r.speed/1024/1024)
754-
}
755738
}
756739

757740
func BenchmarkBackwardIterator(b *testing.B) {

0 commit comments

Comments
 (0)