|
| 1 | +package metastore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/go-kit/log" |
| 9 | + "github.com/grafana/dskit/user" |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/prometheus/model/labels" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "github.com/thanos-io/objstore" |
| 14 | + |
| 15 | + "github.com/grafana/loki/v3/pkg/dataobj/index/indexobj" |
| 16 | + "github.com/grafana/loki/v3/pkg/dataobj/sections/streams" |
| 17 | + "github.com/grafana/loki/v3/pkg/dataobj/uploader" |
| 18 | + "github.com/grafana/loki/v3/pkg/logql/syntax" |
| 19 | +) |
| 20 | + |
| 21 | +type readSectionsBenchmarkParams struct { |
| 22 | + name string |
| 23 | + indexFilesNum int |
| 24 | +} |
| 25 | + |
| 26 | +func BenchmarkReadSections(b *testing.B) { |
| 27 | + benchmarks := []readSectionsBenchmarkParams{ |
| 28 | + { |
| 29 | + name: "single index file", |
| 30 | + indexFilesNum: 1, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "multiple index files", |
| 34 | + indexFilesNum: 200, |
| 35 | + }, |
| 36 | + } |
| 37 | + for _, bm := range benchmarks { |
| 38 | + benchmarkReadSections(b, bm) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func benchmarkReadSections(b *testing.B, bm readSectionsBenchmarkParams) { |
| 43 | + b.Run(bm.name, func(b *testing.B) { |
| 44 | + ctx := context.Background() |
| 45 | + bucket := objstore.NewInMemBucket() |
| 46 | + |
| 47 | + objUploader := uploader.New(uploader.Config{SHAPrefixSize: 2}, bucket, log.NewNopLogger()) |
| 48 | + require.NoError(b, objUploader.RegisterMetrics(prometheus.NewPedanticRegistry())) |
| 49 | + |
| 50 | + metastoreTocWriter := NewTableOfContentsWriter(bucket, log.NewNopLogger()) |
| 51 | + |
| 52 | + // Calculate how many streams per index file |
| 53 | + streamsPerIndex := len(testStreams) / bm.indexFilesNum |
| 54 | + if streamsPerIndex == 0 { |
| 55 | + streamsPerIndex = 1 |
| 56 | + } |
| 57 | + |
| 58 | + // Track global stream ID counter across all index files |
| 59 | + globalStreamID := int64(0) |
| 60 | + |
| 61 | + // Create multiple index files |
| 62 | + for fileIdx := 0; fileIdx < bm.indexFilesNum; fileIdx++ { |
| 63 | + // Create index builder for this file |
| 64 | + builder, err := indexobj.NewBuilder(indexobj.BuilderConfig{ |
| 65 | + TargetPageSize: 1024 * 1024, |
| 66 | + TargetObjectSize: 10 * 1024 * 1024, |
| 67 | + TargetSectionSize: 128, |
| 68 | + BufferSize: 1024 * 1024, |
| 69 | + SectionStripeMergeLimit: 2, |
| 70 | + }, nil) |
| 71 | + require.NoError(b, err) |
| 72 | + |
| 73 | + // Determine which streams to add to this index file |
| 74 | + // Use modulo to cycle through testStreams if we need more entries than available |
| 75 | + startIdx := fileIdx * streamsPerIndex |
| 76 | + endIdx := startIdx + streamsPerIndex |
| 77 | + if fileIdx == bm.indexFilesNum-1 { |
| 78 | + // Last file gets all remaining streams needed to reach the desired count |
| 79 | + endIdx = startIdx + streamsPerIndex + (len(testStreams)-endIdx%len(testStreams))%len(testStreams) |
| 80 | + } |
| 81 | + |
| 82 | + // Add test streams to this index file, cycling through testStreams if necessary |
| 83 | + for i := startIdx; i < endIdx; i++ { |
| 84 | + streamIdx := i % len(testStreams) |
| 85 | + ts := testStreams[streamIdx] |
| 86 | + lbls, err := syntax.ParseLabels(ts.Labels) |
| 87 | + require.NoError(b, err) |
| 88 | + |
| 89 | + newIdx, err := builder.AppendStream(tenantID, streams.Stream{ |
| 90 | + ID: globalStreamID, |
| 91 | + Labels: lbls, |
| 92 | + MinTimestamp: ts.Entries[0].Timestamp, |
| 93 | + MaxTimestamp: ts.Entries[0].Timestamp, |
| 94 | + UncompressedSize: 0, |
| 95 | + }) |
| 96 | + require.NoError(b, err) |
| 97 | + |
| 98 | + err = builder.ObserveLogLine(tenantID, "test-path", int64(fileIdx+1), newIdx, globalStreamID, ts.Entries[0].Timestamp, int64(len(ts.Entries[0].Line))) |
| 99 | + require.NoError(b, err) |
| 100 | + |
| 101 | + globalStreamID++ |
| 102 | + } |
| 103 | + |
| 104 | + // Build and store the index object |
| 105 | + timeRanges := builder.TimeRanges() |
| 106 | + obj, closer, err := builder.Flush() |
| 107 | + require.NoError(b, err) |
| 108 | + b.Cleanup(func() { _ = closer.Close() }) |
| 109 | + |
| 110 | + path, err := objUploader.Upload(context.Background(), obj) |
| 111 | + require.NoError(b, err) |
| 112 | + |
| 113 | + err = metastoreTocWriter.WriteEntry(context.Background(), path, timeRanges) |
| 114 | + require.NoError(b, err) |
| 115 | + } |
| 116 | + |
| 117 | + // Create the metastore instance |
| 118 | + mstore := NewObjectMetastore(bucket, log.NewNopLogger(), nil) |
| 119 | + |
| 120 | + // Prepare benchmark parameters |
| 121 | + benchCtx := user.InjectOrgID(ctx, tenantID) |
| 122 | + start := now.Add(-5 * time.Hour) |
| 123 | + end := now.Add(5 * time.Hour) |
| 124 | + matchers := []*labels.Matcher{ |
| 125 | + labels.MustNewMatcher(labels.MatchEqual, "app", "foo"), |
| 126 | + } |
| 127 | + |
| 128 | + b.ResetTimer() |
| 129 | + b.ReportAllocs() |
| 130 | + |
| 131 | + // Run the benchmark |
| 132 | + for range b.N { |
| 133 | + sections, err := mstore.Sections(benchCtx, start, end, matchers, nil) |
| 134 | + require.NoError(b, err) |
| 135 | + require.NotEmpty(b, sections) |
| 136 | + } |
| 137 | + |
| 138 | + // Stop timer before cleanup |
| 139 | + b.StopTimer() |
| 140 | + }) |
| 141 | +} |
0 commit comments