2019-05-22 23:16:55 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-02-23 23:15:21 +01:00
|
|
|
"sync/atomic"
|
2019-05-22 23:16:55 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkBlockStreamWriterBlocksWorstCase(b *testing.B) {
|
|
|
|
benchmarkBlockStreamWriter(b, benchBlocksWorstCase, len(benchRawRowsWorstCase), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBlockStreamWriterBlocksBestCase(b *testing.B) {
|
|
|
|
benchmarkBlockStreamWriter(b, benchBlocksBestCase, len(benchRawRowsBestCase), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBlockStreamWriterRowsWorstCase(b *testing.B) {
|
|
|
|
benchmarkBlockStreamWriter(b, benchBlocksWorstCase, len(benchRawRowsWorstCase), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkBlockStreamWriterRowsBestCase(b *testing.B) {
|
|
|
|
benchmarkBlockStreamWriter(b, benchBlocksBestCase, len(benchRawRowsBestCase), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkBlockStreamWriter(b *testing.B, ebs []Block, rowsCount int, writeRows bool) {
|
2024-02-23 23:15:21 +01:00
|
|
|
var rowsMerged atomic.Uint64
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(rowsCount))
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var bsw blockStreamWriter
|
|
|
|
var mp inmemoryPart
|
|
|
|
var ph partHeader
|
|
|
|
var ebsCopy []Block
|
|
|
|
for i := range ebs {
|
|
|
|
var ebCopy Block
|
|
|
|
ebCopy.CopyFrom(&ebs[i])
|
|
|
|
ebsCopy = append(ebsCopy, ebCopy)
|
|
|
|
}
|
|
|
|
loopCount := 0
|
|
|
|
for pb.Next() {
|
|
|
|
if writeRows {
|
|
|
|
for i := range ebsCopy {
|
|
|
|
eb := &ebsCopy[i]
|
|
|
|
if err := eb.UnmarshalData(); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
panic(fmt.Errorf("cannot unmarshal block %d on loop %d: %w", i, loopCount, err))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:46:09 +02:00
|
|
|
bsw.MustInitFromInmemoryPart(&mp, -5)
|
2019-05-22 23:16:55 +02:00
|
|
|
for i := range ebsCopy {
|
2021-12-15 14:58:27 +01:00
|
|
|
bsw.WriteExternalBlock(&ebsCopy[i], &ph, &rowsMerged)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
bsw.MustClose()
|
|
|
|
mp.Reset()
|
|
|
|
loopCount++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var benchBlocksWorstCase = newBenchBlocks(benchRawRowsWorstCase)
|
|
|
|
var benchBlocksBestCase = newBenchBlocks(benchRawRowsBestCase)
|
|
|
|
|
|
|
|
func newBenchBlocks(rows []rawRow) []Block {
|
|
|
|
var ebs []Block
|
|
|
|
|
|
|
|
mp := newTestInmemoryPart(rows)
|
|
|
|
var bsr blockStreamReader
|
2023-04-15 00:46:09 +02:00
|
|
|
bsr.MustInitFromInmemoryPart(mp)
|
2019-05-22 23:16:55 +02:00
|
|
|
for bsr.NextBlock() {
|
|
|
|
var eb Block
|
|
|
|
eb.CopyFrom(&bsr.Block)
|
|
|
|
ebs = append(ebs, eb)
|
|
|
|
}
|
|
|
|
if err := bsr.Error(); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
panic(fmt.Errorf("unexpected error when reading inmemoryPart: %w", err))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return ebs
|
|
|
|
}
|