-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathcompactor_test.go
215 lines (174 loc) · 6.05 KB
/
compactor_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
package vparquet3
import (
"context"
crand "crypto/rand"
"flag"
"math/rand"
"testing"
"github.com/go-kit/log"
"github.com/parquet-go/parquet-go"
"github.com/stretchr/testify/require"
tempo_io "github.com/grafana/tempo/pkg/io"
tempoUtil "github.com/grafana/tempo/pkg/util"
"github.com/grafana/tempo/pkg/util/test"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/backend/local"
"github.com/grafana/tempo/tempodb/encoding/common"
)
func BenchmarkCompactor(b *testing.B) {
b.Run("Small", func(b *testing.B) {
benchmarkCompactor(b, 1000, 100, 100) // 10M spans
})
b.Run("Medium", func(b *testing.B) {
benchmarkCompactor(b, 100, 100, 1000) // 10M spans
})
b.Run("Large", func(b *testing.B) {
benchmarkCompactor(b, 10, 1000, 1000) // 10M spans
})
}
func benchmarkCompactor(b *testing.B, traceCount, batchCount, spanCount int) {
rawR, rawW, _, err := local.New(&local.Config{
Path: b.TempDir(),
})
require.NoError(b, err)
r := backend.NewReader(rawR)
w := backend.NewWriter(rawW)
ctx := context.Background()
l := log.NewNopLogger()
cfg := &common.BlockConfig{
BloomFP: 0.01,
BloomShardSizeBytes: 100 * 1024,
RowGroupSizeBytes: 20_000_000,
}
meta := createTestBlock(b, ctx, cfg, r, w, traceCount, batchCount, spanCount, 0, nil)
inputs := []*backend.BlockMeta{meta}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := NewCompactor(common.CompactionOptions{
BlockConfig: *cfg,
OutputBlocks: 1,
FlushSizeBytes: 30_000_000,
MaxBytesPerTrace: 50_000_000,
})
_, err = c.Compact(ctx, l, r, w, inputs)
require.NoError(b, err)
}
}
func BenchmarkCompactorDupes(b *testing.B) {
rawR, rawW, _, err := local.New(&local.Config{
Path: b.TempDir(),
})
require.NoError(b, err)
r := backend.NewReader(rawR)
w := backend.NewWriter(rawW)
ctx := context.Background()
l := log.NewNopLogger()
cfg := &common.BlockConfig{
BloomFP: 0.01,
BloomShardSizeBytes: 100 * 1024,
RowGroupSizeBytes: 20_000_000,
}
// 1M span traces
meta := createTestBlock(b, ctx, cfg, r, w, 10, 1000, 1000, 1, nil)
inputs := []*backend.BlockMeta{meta, meta}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := NewCompactor(common.CompactionOptions{
BlockConfig: *cfg,
OutputBlocks: 1,
FlushSizeBytes: 30_000_000,
MaxBytesPerTrace: 50_000_000,
ObjectsCombined: func(compactionLevel, objects int) {},
SpansDiscarded: func(traceID, rootSpanName string, rootServiceName string, spans int) {},
})
_, err = c.Compact(ctx, l, r, w, inputs)
require.NoError(b, err)
}
}
// createTestBlock with the number of given traces and the needed sizes.
// Trace IDs are guaranteed to be monotonically increasing so that
// the block will be iterated in order.
// nolint: revive
func createTestBlock(t testing.TB, ctx context.Context, cfg *common.BlockConfig, r backend.Reader, w backend.Writer, traceCount, batchCount, spanCount, replicationFactor int, dc backend.DedicatedColumns) *backend.BlockMeta {
inMeta := &backend.BlockMeta{
TenantID: tenantID,
BlockID: backend.NewUUID(),
TotalObjects: int64(traceCount),
ReplicationFactor: uint32(replicationFactor),
DedicatedColumns: dc,
}
sb := newStreamingBlock(ctx, cfg, inMeta, r, w, tempo_io.NewBufferedWriter)
for i := 0; i < traceCount; i++ {
id := make([]byte, 16)
_, err := crand.Read(id)
require.NoError(t, err)
tr := test.AddDedicatedAttributes(test.MakeTraceWithSpanCount(batchCount, spanCount, id))
trp, connected := traceToParquet(inMeta, id, tr, nil)
require.False(t, connected)
require.NoError(t, sb.Add(trp, 0, 0))
if sb.EstimatedBufferedBytes() > 20_000_000 {
_, err := sb.Flush()
require.NoError(t, err)
}
}
_, err := sb.Complete()
require.NoError(t, err)
return sb.meta
}
func TestValueAlloc(_ *testing.T) {
_ = make([]parquet.Value, 1_000_000)
}
func TestCountSpans(t *testing.T) {
// It causes high mem usage when batchSize and spansEach are too big (> 500)
batchSize := 300 + rand.Intn(25)
spansEach := 250 + rand.Intn(25)
rootSpan := "foo"
rootService := "bar"
sch := parquet.SchemaOf(new(Trace))
traceID := make([]byte, 16)
_, err := crand.Read(traceID)
require.NoError(t, err)
// make Trace and convert to parquet.Row
tr := test.MakeTraceWithSpanCount(batchSize, spansEach, traceID)
trp, connected := traceToParquet(&backend.BlockMeta{}, traceID, tr, nil)
require.False(t, connected)
trp.RootServiceName = rootService
trp.RootSpanName = rootSpan
row := sch.Deconstruct(nil, trp)
// count spans for generated rows.
tID, rootSpanName, rootServiceName, spans := countSpans(sch, row)
require.Equal(t, tID, tempoUtil.TraceIDToHexString(traceID))
require.Equal(t, spans, batchSize*spansEach)
require.Equal(t, rootSpan, rootSpanName)
require.Equal(t, rootService, rootServiceName)
}
func TestCompact(t *testing.T) {
rawR, rawW, _, err := local.New(&local.Config{
Path: t.TempDir(),
})
require.NoError(t, err)
r := backend.NewReader(rawR)
w := backend.NewWriter(rawW)
blockConfig := common.BlockConfig{Version: VersionString}
blockConfig.RegisterFlagsAndApplyDefaults("", &flag.FlagSet{})
require.NoError(t, common.ValidateConfig(&blockConfig))
c := NewCompactor(common.CompactionOptions{
BlockConfig: blockConfig,
OutputBlocks: 1,
FlushSizeBytes: 30_000_000,
ObjectsCombined: func(compactionLevel, objects int) {},
})
dedicatedColumns := backend.DedicatedColumns{
{Scope: "resource", Name: "dedicated.resource.1", Type: "string"},
{Scope: "span", Name: "dedicated.span.1", Type: "string"},
}
meta1 := createTestBlock(t, context.Background(), &blockConfig, r, w, 10, 10, 10, 1, dedicatedColumns)
meta2 := createTestBlock(t, context.Background(), &blockConfig, r, w, 10, 10, 10, 1, dedicatedColumns)
inputs := []*backend.BlockMeta{meta1, meta2}
newMeta, err := c.Compact(context.Background(), log.NewNopLogger(), r, w, inputs)
require.NoError(t, err)
require.Len(t, newMeta, 1)
require.Equal(t, int64(20), newMeta[0].TotalObjects)
require.Equal(t, uint32(1), newMeta[0].ReplicationFactor)
require.Equal(t, dedicatedColumns, newMeta[0].DedicatedColumns)
}