forked from polarsignals/frostdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
466 lines (413 loc) · 11.6 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package frostdb
import (
"context"
"errors"
"io"
"math/rand"
"sort"
"strings"
"testing"
"time"
"github.com/apache/arrow/go/v14/arrow"
"github.com/apache/arrow/go/v14/arrow/array"
"github.com/apache/arrow/go/v14/arrow/memory"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/polarsignals/frostdb/dynparquet"
"github.com/polarsignals/frostdb/query"
"github.com/polarsignals/frostdb/query/logicalplan"
)
const (
dbName = "parca"
tableName = "stacktraces"
storagePath = "testdata/data"
skipReason = "requires data directory"
)
// This file runs end-to-end benchmarks on a single FrostDB instance using
// Polar Signals-specific data and schema. These benchmarks are skipped by
// default since they require a manually-generated data directory to run. To run
// these benchmarks, unskip the benchmarks and create a data directory in
// testdata/data.
func newDBForBenchmarks(ctx context.Context, b testing.TB) (*ColumnStore, *DB, error) {
b.Helper()
b.Logf("recovering DB")
start := time.Now()
col, err := New(
WithWAL(),
WithStoragePath(storagePath),
)
if err != nil {
return nil, nil, err
}
b.Logf("recovered DB in %s", time.Since(start))
colDB, err := col.DB(ctx, dbName)
if err != nil {
return nil, nil, err
}
table, err := colDB.GetTable(tableName)
if err != nil {
return nil, nil, err
}
b.Logf("ensuring compaction")
start = time.Now()
if err := table.EnsureCompaction(); err != nil {
return nil, nil, err
}
b.Logf("ensured compaction in %s", time.Since(start))
b.Logf("db initialized and recovered, starting benchmark %s", b.Name())
return col, colDB, nil
}
func getLatest15MinInterval(ctx context.Context, b testing.TB, engine *query.LocalEngine) (start, end int64) {
b.Helper()
var result arrow.Record
require.NoError(b, engine.ScanTable(tableName).
Aggregate(
[]logicalplan.Expr{
logicalplan.Max(logicalplan.Col("timestamp")),
},
nil,
).Execute(ctx,
func(ctx context.Context, r arrow.Record) error {
r.Retain()
result = r
return nil
}))
defer result.Release()
require.Equal(b, int64(1), result.NumCols())
require.Equal(b, int64(1), result.NumRows())
end = result.Column(0).(*array.Int64).Int64Values()[0]
// start will be 15 minutes before end.
start = time.UnixMilli(end).Add(-(15 * time.Minute)).UnixMilli()
require.True(b, start < end)
return start, end
}
var typeColumns = []logicalplan.Expr{
logicalplan.Col("name"),
logicalplan.Col("sample_type"),
logicalplan.Col("sample_unit"),
logicalplan.Col("period_type"),
logicalplan.Col("period_unit"),
}
func getTypesQuery(engine *query.LocalEngine) query.Builder {
return engine.ScanTable(tableName).
Distinct(
append(
typeColumns,
logicalplan.Col("duration").Gt(logicalplan.Literal(0)),
)...,
)
}
func getLabelsQuery(engine *query.LocalEngine) query.Builder {
return engine.ScanSchema(tableName).
Distinct(logicalplan.Col("name")).
Filter(logicalplan.Col("name").RegexMatch("^labels\\..+$"))
}
func getValuesForLabelQuery(engine *query.LocalEngine, labelName string) query.Builder {
return engine.ScanTable(tableName).
Distinct(logicalplan.Col(labelName))
}
type typesResult [][]string
func (t typesResult) Len() int {
return len(t)
}
func (t typesResult) Less(i, j int) bool {
for col := 0; col < len(t[0]); col++ {
if t[i][col] == t[j][col] {
continue
}
return t[i][col] < t[j][col]
}
return false
}
func (t *typesResult) Swap(i, j int) {
(*t)[i], (*t)[j] = (*t)[j], (*t)[i]
}
// getDeterministicTypeFilterExpr will always return a deterministic profile
// type across benchmark runs, as well as a pretty string to print this type.
func getDeterministicTypeFilterExpr(
ctx context.Context, engine *query.LocalEngine,
) ([]logicalplan.Expr, string, error) {
results := make(typesResult, 0)
if err := getTypesQuery(engine).Execute(ctx, func(_ context.Context, r arrow.Record) error {
for i := 0; i < int(r.NumRows()); i++ {
row := make([]string, 0, len(typeColumns))
for j := range typeColumns {
v := r.Column(j).GetOneForMarshal(i)
row = append(row, string(v.([]byte)))
}
results = append(results, row)
}
return nil
}); err != nil {
return nil, "", err
}
if len(results) == 0 {
return nil, "", errors.New("no types found")
}
sort.Sort(&results)
filterExpr := make([]logicalplan.Expr, 0, len(typeColumns))
for i, toMatch := range results[0] {
filterExpr = append(
filterExpr,
typeColumns[i].(*logicalplan.Column).Eq(logicalplan.Literal(toMatch)),
)
}
return filterExpr, strings.Join(results[0], ":"), nil
}
// getDeterministicLabel will always return a deterministic label/value pair
// across benchmarks.
func getDeterministicLabelValuePair(ctx context.Context, engine *query.LocalEngine) (string, string, error) {
labels := make([]string, 0)
if err := getLabelsQuery(engine).Execute(ctx, func(_ context.Context, r arrow.Record) error {
arr := r.Column(0)
for i := 0; i < arr.Len(); i++ {
labels = append(labels, arr.(*array.String).Value(i))
}
return nil
}); err != nil {
return "", "", err
}
sort.Strings(labels)
for _, label := range labels {
values := make([]string, 0)
if err := getValuesForLabelQuery(engine, label).Execute(ctx, func(ctx context.Context, r arrow.Record) error {
arr := r.Column(0)
for i := 0; i < arr.Len(); i++ {
if arr.IsNull(i) {
continue
}
v := arr.GetOneForMarshal(i)
values = append(values, string(v.([]byte)))
}
return nil
}); err != nil {
return "", "", err
}
if len(values) == 0 {
continue
}
sort.Strings(values)
return label, values[0], nil
}
return "", "", errors.New("no non-null values found")
}
func BenchmarkQuery(b *testing.B) {
b.Skip(skipReason)
ctx := context.Background()
c, db, err := newDBForBenchmarks(ctx, b)
require.NoError(b, err)
defer c.Close()
engine := query.NewEngine(
memory.NewGoAllocator(),
db.TableProvider(),
)
start, end := getLatest15MinInterval(ctx, b, engine)
label, value, err := getDeterministicLabelValuePair(ctx, engine)
require.NoError(b, err)
typeFilter, filterPretty, err := getDeterministicTypeFilterExpr(ctx, engine)
require.NoError(b, err)
b.Logf("using label/value pair: (label=%s,value=%s)", label, value)
b.Logf("using types filter: %s", filterPretty)
fullFilter := append(
typeFilter,
logicalplan.Col(label).Eq(logicalplan.Literal(value)),
logicalplan.Col("timestamp").Gt(logicalplan.Literal(start)),
logicalplan.Col("timestamp").Lt(logicalplan.Literal(end)),
)
b.Run("Types", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := getTypesQuery(engine).
Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
})
b.Run("Labels", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := getLabelsQuery(engine).Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
})
b.Run("Values", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := getValuesForLabelQuery(engine, label).
Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
})
// Merge executes a merge of profiles over a 15-minute time window.
b.Run("Merge", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := engine.ScanTable(tableName).
Filter(
logicalplan.And(fullFilter...),
).
Aggregate(
[]logicalplan.Expr{
logicalplan.Sum(logicalplan.Col("value")),
},
[]logicalplan.Expr{
logicalplan.Col("stacktrace"),
},
).
Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
})
// Range gets a series of profiles over a 15-minute time window.
b.Run("Range", func(b *testing.B) {
for i := 0; i < b.N; i++ {
if err := engine.ScanTable(tableName).
Filter(
logicalplan.And(fullFilter...),
).
Aggregate(
[]logicalplan.Expr{
logicalplan.Sum(logicalplan.Col("value")),
},
[]logicalplan.Expr{
logicalplan.DynCol("labels"),
logicalplan.Col("timestamp"),
},
).
Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
})
// BenchmarkFilter benchmarks performing a simple filter. This is useful to get an idea of our scan speed (minus the
// execution engine).
b.Run("Filter", func(b *testing.B) {
table, err := db.Table(tableName, nil)
require.NoError(b, err)
size := table.ActiveBlock().Size()
for i := 0; i < b.N; i++ {
if err := engine.ScanTable(tableName).
Filter(
logicalplan.And(fullFilter...),
).
Execute(ctx, func(ctx context.Context, r arrow.Record) error {
if r.NumRows() == 0 {
b.Fatal("expected at least one row")
}
return nil
}); err != nil {
b.Fatalf("query returned error: %v", err)
}
}
b.ReportMetric(float64(size)/(float64(b.Elapsed().Milliseconds())/float64(b.N)), "B/msec")
})
}
func BenchmarkReplay(b *testing.B) {
b.Skip(skipReason)
for i := 0; i < b.N; i++ {
func() {
col, err := New(
WithWAL(),
WithStoragePath(storagePath),
)
require.NoError(b, err)
defer col.Close()
}()
}
}
type writeCounter struct {
io.Writer
count int
}
func (wc *writeCounter) Write(p []byte) (int, error) {
count, err := wc.Writer.Write(p)
wc.count += count
return count, err
}
func BenchmarkSnapshot(b *testing.B) {
b.Skip(skipReason)
ctx := context.Background()
col, err := New(
WithWAL(),
WithStoragePath(storagePath),
)
require.NoError(b, err)
defer col.Close()
db, err := col.DB(ctx, dbName)
require.NoError(b, err)
b.Log("recovered DB, starting benchmark")
b.ResetTimer()
bytesWritten := 0
for i := 0; i < b.N; i++ {
wc := &writeCounter{Writer: io.Discard}
require.NoError(b, WriteSnapshot(ctx, db.HighWatermark(), db, wc, false))
bytesWritten += wc.count
}
b.ReportMetric(float64(bytesWritten)/float64(b.N), "size/op")
}
func NewTestSamples(num int) dynparquet.Samples {
samples := make(dynparquet.Samples, 0, num)
for i := 0; i < num; i++ {
samples = append(samples,
dynparquet.Sample{
ExampleType: "cpu",
Labels: map[string]string{
"node": "test3",
},
Stacktrace: []uuid.UUID{
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1},
{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2},
},
Timestamp: rand.Int63n(100000),
Value: rand.Int63(),
},
)
}
return samples
}
func Benchmark_Serialize(b *testing.B) {
ctx := context.Background()
schema := dynparquet.SampleDefinition()
col, err := New()
require.NoError(b, err)
defer col.Close()
db, err := col.DB(ctx, "test")
require.NoError(b, err)
tbl, err := db.Table("test", NewTableConfig(schema))
require.NoError(b, err)
// Insert 10k rows
samples := NewTestSamples(10000)
r, err := samples.ToRecord()
require.NoError(b, err)
_, err = tbl.InsertRecord(ctx, r)
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Serialize the table
require.NoError(b, tbl.active.Serialize(io.Discard))
}
}