2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2024-02-23 22:15:21 +00:00
|
|
|
"sync/atomic"
|
2019-05-22 21:16:55 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkMergeBlockStreamsTwoSourcesWorstCase(b *testing.B) {
|
|
|
|
benchmarkMergeBlockStreams(b, benchTwoSourcesWorstCaseMPS, benchTwoSourcesWorstCaseMPSRowsPerLoop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMergeBlockStreamsTwoSourcesBestCase(b *testing.B) {
|
|
|
|
benchmarkMergeBlockStreams(b, benchTwoSourcesBestCaseMPS, benchTwoSourcesBestCaseMPSRowsPerLoop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMergeBlockStreamsFourSourcesWorstCase(b *testing.B) {
|
|
|
|
benchmarkMergeBlockStreams(b, benchFourSourcesWorstCaseMPS, benchFourSourcesWorstCaseMPSRowsPerLoop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMergeBlockStreamsFourSourcesBestCase(b *testing.B) {
|
|
|
|
benchmarkMergeBlockStreams(b, benchFourSourcesBestCaseMPS, benchFourSourcesBestCaseMPSRowsPerLoop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkMergeBlockStreams(b *testing.B, mps []*inmemoryPart, rowsPerLoop int64) {
|
2024-02-23 22:15:21 +00:00
|
|
|
var rowsMerged, rowsDeleted atomic.Uint64
|
2022-10-23 13:23:44 +00:00
|
|
|
strg := newTestStorage()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(rowsPerLoop)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var bsw blockStreamWriter
|
|
|
|
var mpOut inmemoryPart
|
|
|
|
bsrs := make([]*blockStreamReader, len(mps))
|
|
|
|
for i := range mps {
|
|
|
|
var bsr blockStreamReader
|
|
|
|
bsrs[i] = &bsr
|
|
|
|
}
|
|
|
|
for pb.Next() {
|
|
|
|
for i, mp := range mps {
|
2023-04-14 22:46:09 +00:00
|
|
|
bsrs[i].MustInitFromInmemoryPart(mp)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
mpOut.Reset()
|
2023-04-14 22:46:09 +00:00
|
|
|
bsw.MustInitFromInmemoryPart(&mpOut, -5)
|
2022-10-23 13:08:54 +00:00
|
|
|
if err := mergeBlockStreams(&mpOut.ph, &bsw, bsrs, nil, strg, 0, &rowsMerged, &rowsDeleted); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
panic(fmt.Errorf("cannot merge block streams: %w", err))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-07-14 00:13:24 +00:00
|
|
|
|
|
|
|
stopTestStorage(strg)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var benchTwoSourcesWorstCaseMPS = func() []*inmemoryPart {
|
2023-01-24 04:10:29 +00:00
|
|
|
rng := rand.New(rand.NewSource(1))
|
2019-05-22 21:16:55 +00:00
|
|
|
var rows []rawRow
|
|
|
|
var r rawRow
|
|
|
|
r.PrecisionBits = defaultPrecisionBits
|
|
|
|
for i := 0; i < maxRowsPerBlock/2-1; i++ {
|
2023-01-24 04:10:29 +00:00
|
|
|
r.Value = rng.NormFloat64()
|
|
|
|
r.Timestamp = rng.Int63n(1e12)
|
2019-05-22 21:16:55 +00:00
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
|
|
|
mp := newTestInmemoryPart(rows)
|
|
|
|
return []*inmemoryPart{mp, mp}
|
|
|
|
}()
|
|
|
|
|
|
|
|
const benchTwoSourcesWorstCaseMPSRowsPerLoop = (maxRowsPerBlock - 2)
|
|
|
|
|
|
|
|
var benchTwoSourcesBestCaseMPS = func() []*inmemoryPart {
|
|
|
|
var r rawRow
|
|
|
|
var mps []*inmemoryPart
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
var rows []rawRow
|
|
|
|
r.PrecisionBits = defaultPrecisionBits
|
|
|
|
r.TSID.MetricID = uint64(i)
|
|
|
|
for j := 0; j < maxRowsPerBlock; j++ {
|
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
|
|
|
mp := newTestInmemoryPart(rows)
|
|
|
|
mps = append(mps, mp)
|
|
|
|
}
|
|
|
|
return mps
|
|
|
|
}()
|
|
|
|
|
|
|
|
const benchTwoSourcesBestCaseMPSRowsPerLoop = 2 * maxRowsPerBlock
|
|
|
|
|
|
|
|
var benchFourSourcesWorstCaseMPS = func() []*inmemoryPart {
|
2023-01-24 04:10:29 +00:00
|
|
|
rng := rand.New(rand.NewSource(1))
|
2019-05-22 21:16:55 +00:00
|
|
|
var rows []rawRow
|
|
|
|
var r rawRow
|
|
|
|
r.PrecisionBits = defaultPrecisionBits
|
|
|
|
for i := 0; i < maxRowsPerBlock/2-1; i++ {
|
2023-01-24 04:10:29 +00:00
|
|
|
r.Value = rng.NormFloat64()
|
|
|
|
r.Timestamp = rng.Int63n(1e12)
|
2019-05-22 21:16:55 +00:00
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
|
|
|
mp := newTestInmemoryPart(rows)
|
|
|
|
return []*inmemoryPart{mp, mp, mp, mp}
|
|
|
|
}()
|
|
|
|
|
|
|
|
const benchFourSourcesWorstCaseMPSRowsPerLoop = 2 * (maxRowsPerBlock - 2)
|
|
|
|
|
|
|
|
var benchFourSourcesBestCaseMPS = func() []*inmemoryPart {
|
|
|
|
var r rawRow
|
|
|
|
var mps []*inmemoryPart
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
var rows []rawRow
|
|
|
|
r.PrecisionBits = defaultPrecisionBits
|
|
|
|
r.TSID.MetricID = uint64(i)
|
|
|
|
for j := 0; j < maxRowsPerBlock; j++ {
|
|
|
|
rows = append(rows, r)
|
|
|
|
}
|
|
|
|
mp := newTestInmemoryPart(rows)
|
|
|
|
mps = append(mps, mp)
|
|
|
|
}
|
|
|
|
return mps
|
|
|
|
}()
|
|
|
|
|
|
|
|
const benchFourSourcesBestCaseMPSRowsPerLoop = 4 * maxRowsPerBlock
|