2019-05-22 23:16:55 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2023-03-25 22:33:54 +01:00
|
|
|
"path/filepath"
|
2019-05-22 23:16:55 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-12-08 19:49:32 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-04-15 05:52:36 +02:00
|
|
|
isDebug = true
|
2019-05-22 23:16:55 +02:00
|
|
|
n := m.Run()
|
2023-03-25 22:33:54 +01:00
|
|
|
if err := os.RemoveAll("benchmarkTableSearch"); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
panic(fmt.Errorf("cannot remove benchmark tables: %w", err))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
os.Exit(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTableSearch(b *testing.B) {
|
|
|
|
for _, rowsCount := range []int{1e5, 1e6, 1e7, 1e8} {
|
|
|
|
b.Run(fmt.Sprintf("rowsCount_%d", rowsCount), func(b *testing.B) {
|
|
|
|
for _, tsidsCount := range []int{1e3, 1e4} {
|
|
|
|
b.Run(fmt.Sprintf("tsidsCount_%d", tsidsCount), func(b *testing.B) {
|
|
|
|
for _, tsidsSearch := range []int{1, 1e1, 1e2, 1e3, 1e4} {
|
|
|
|
b.Run(fmt.Sprintf("tsidsSearch_%d", tsidsSearch), func(b *testing.B) {
|
2022-06-28 11:55:20 +02:00
|
|
|
benchmarkTableSearch(b, rowsCount, tsidsCount, tsidsSearch)
|
2019-05-22 23:16:55 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 02:13:24 +02:00
|
|
|
func openBenchTable(b *testing.B, startTimestamp int64, rowsPerInsert, rowsCount, tsidsCount int) (*table, *Storage) {
|
2019-05-22 23:16:55 +02:00
|
|
|
b.Helper()
|
|
|
|
|
2023-03-25 22:33:54 +01:00
|
|
|
path := filepath.Join("benchmarkTableSearch", fmt.Sprintf("rows%d_tsids%d", rowsCount, tsidsCount))
|
2019-05-22 23:16:55 +02:00
|
|
|
if !createdBenchTables[path] {
|
|
|
|
createBenchTable(b, path, startTimestamp, rowsPerInsert, rowsCount, tsidsCount)
|
|
|
|
createdBenchTables[path] = true
|
|
|
|
}
|
2022-10-23 15:23:44 +02:00
|
|
|
strg := newTestStorage()
|
2023-04-15 08:01:20 +02:00
|
|
|
tb := mustOpenTable(path, strg)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Verify rows count in the table opened from files.
|
|
|
|
insertsCount := uint64((rowsCount + rowsPerInsert - 1) / rowsPerInsert)
|
|
|
|
rowsCountExpected := insertsCount * uint64(rowsPerInsert)
|
|
|
|
var m TableMetrics
|
|
|
|
tb.UpdateMetrics(&m)
|
2022-12-06 00:15:00 +01:00
|
|
|
if rowsCount := m.TotalRowsCount(); rowsCount != rowsCountExpected {
|
|
|
|
b.Fatalf("unexpected rows count in the table %q; got %d; want %d", path, rowsCount, rowsCountExpected)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2023-07-14 02:13:24 +02:00
|
|
|
return tb, strg
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var createdBenchTables = make(map[string]bool)
|
|
|
|
|
|
|
|
func createBenchTable(b *testing.B, path string, startTimestamp int64, rowsPerInsert, rowsCount, tsidsCount int) {
|
|
|
|
b.Helper()
|
|
|
|
|
2022-10-23 15:23:44 +02:00
|
|
|
strg := newTestStorage()
|
2023-04-15 08:01:20 +02:00
|
|
|
tb := mustOpenTable(path, strg)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2024-02-23 23:47:39 +01:00
|
|
|
var insertsCount atomic.Int64
|
|
|
|
insertsCount.Store(int64((rowsCount + rowsPerInsert - 1) / rowsPerInsert))
|
|
|
|
|
|
|
|
var timestamp atomic.Uint64
|
|
|
|
timestamp.Store(uint64(startTimestamp))
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2020-12-08 19:49:32 +01:00
|
|
|
for k := 0; k < cgroup.AvailableCPUs(); k++ {
|
2019-05-22 23:16:55 +02:00
|
|
|
wg.Add(1)
|
2023-01-24 05:10:29 +01:00
|
|
|
go func(n int) {
|
|
|
|
rng := rand.New(rand.NewSource(int64(n)))
|
2019-05-22 23:16:55 +02:00
|
|
|
rows := make([]rawRow, rowsPerInsert)
|
|
|
|
value := float64(100)
|
2024-02-23 23:47:39 +01:00
|
|
|
for insertsCount.Add(-1) >= 0 {
|
2019-05-22 23:16:55 +02:00
|
|
|
for j := 0; j < rowsPerInsert; j++ {
|
2024-02-23 23:47:39 +01:00
|
|
|
ts := timestamp.Add(uint64(10 + rng.Int63n(2)))
|
2023-01-24 05:10:29 +01:00
|
|
|
value += float64(int(rng.NormFloat64() * 5))
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
r := &rows[j]
|
|
|
|
r.PrecisionBits = defaultPrecisionBits
|
2023-01-24 05:10:29 +01:00
|
|
|
r.TSID.MetricID = uint64(rng.Intn(tsidsCount) + 1)
|
2019-05-22 23:16:55 +02:00
|
|
|
r.Timestamp = int64(ts)
|
|
|
|
r.Value = value
|
|
|
|
}
|
2023-04-14 07:11:56 +02:00
|
|
|
tb.MustAddRows(rows)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
wg.Done()
|
2023-01-24 05:10:29 +01:00
|
|
|
}(k)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
tb.MustClose()
|
2023-07-14 02:13:24 +02:00
|
|
|
stopTestStorage(strg)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 11:55:20 +02:00
|
|
|
func benchmarkTableSearch(b *testing.B, rowsCount, tsidsCount, tsidsSearch int) {
|
2019-05-22 23:16:55 +02:00
|
|
|
startTimestamp := timestampFromTime(time.Now()) - 365*24*3600*1000
|
2024-02-22 19:06:37 +01:00
|
|
|
rowsPerInsert := maxRawRowsPerShard
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2023-07-14 02:13:24 +02:00
|
|
|
tb, strg := openBenchTable(b, startTimestamp, rowsPerInsert, rowsCount, tsidsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
tr := TimeRange{
|
|
|
|
MinTimestamp: startTimestamp,
|
|
|
|
MaxTimestamp: (1 << 63) - 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
rowsPerBench := int64(float64(rowsCount) * float64(tsidsSearch) / float64(tsidsCount))
|
|
|
|
if rowsPerBench > int64(rowsCount) {
|
|
|
|
rowsPerBench = int64(rowsCount)
|
|
|
|
}
|
|
|
|
b.SetBytes(rowsPerBench)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
var ts tableSearch
|
|
|
|
tsids := make([]TSID, tsidsSearch)
|
2020-04-27 07:13:41 +02:00
|
|
|
var tmpBlock Block
|
2019-05-22 23:16:55 +02:00
|
|
|
for pb.Next() {
|
|
|
|
for i := range tsids {
|
|
|
|
tsids[i].MetricID = 1 + uint64(i)
|
|
|
|
}
|
2020-04-27 07:13:41 +02:00
|
|
|
ts.Init(tb, tsids, tr)
|
2019-05-22 23:16:55 +02:00
|
|
|
for ts.NextBlock() {
|
2022-06-28 11:55:20 +02:00
|
|
|
ts.BlockRef.MustReadBlock(&tmpBlock)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
ts.MustClose()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.StopTimer()
|
|
|
|
|
|
|
|
tb.MustClose()
|
2023-07-14 02:13:24 +02:00
|
|
|
stopTestStorage(strg)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|