Skip to content

Micro optimization in newBlockQuerierSeries() #2633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pkg/querier/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,13 @@ func (bqss *blockQuerierSeriesSet) Err() error {
return nil
}

// newBlockQuerierSeries makes a new blockQuerierSeries. Input labels must be already sorted by name.
func newBlockQuerierSeries(lbls []storepb.Label, chunks []storepb.AggrChunk) *blockQuerierSeries {
sort.Slice(chunks, func(i, j int) bool {
return chunks[i].MinTime < chunks[j].MinTime
})

b := labels.NewBuilder(nil)
for _, l := range lbls {
b.Set(l.Name, l.Value)
}

return &blockQuerierSeries{labels: b.Labels(), chunks: chunks}
return &blockQuerierSeries{labels: storepb.LabelsToPromLabelsUnsafe(lbls), chunks: chunks}
}

type blockQuerierSeries struct {
Expand Down
29 changes: 26 additions & 3 deletions pkg/querier/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ func TestBlockQuerierSeriesSet(t *testing.T) {

bss := &blockQuerierSeriesSet{
series: []*storepb.Series{
// labels here are not sorted, but blockQuerierSeriesSet will sort it.
// first, with one chunk.
{
Labels: mkLabels("a", "a", "__name__", "first"),
Labels: mkLabels("__name__", "first", "a", "a"),
Chunks: []storepb.AggrChunk{
createChunkWithSineSamples(now, now.Add(100*time.Second), 3*time.Millisecond), // ceil(100 / 0.003) samples (= 33334)
},
},

// continuation of previous series. Must have exact same labels.
{
Labels: mkLabels("a", "a", "__name__", "first"),
Labels: mkLabels("__name__", "first", "a", "a"),
Chunks: []storepb.AggrChunk{
createChunkWithSineSamples(now.Add(100*time.Second), now.Add(200*time.Second), 3*time.Millisecond), // ceil(100 / 0.003) samples more, 66668 in total
},
Expand Down Expand Up @@ -250,3 +250,26 @@ func mkLabels(s ...string) []storepb.Label {

return result
}

func Benchmark_newBlockQuerierSeries(b *testing.B) {
lbls := mkLabels(
"__name__", "test",
"label_1", "value_1",
"label_2", "value_2",
"label_3", "value_3",
"label_4", "value_4",
"label_5", "value_5",
"label_6", "value_6",
"label_7", "value_7",
"label_8", "value_8",
"label_9", "value_9")

chunks := []storepb.AggrChunk{
createChunkWithSineSamples(time.Now(), time.Now().Add(-time.Hour), time.Minute),
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
newBlockQuerierSeries(lbls, chunks)
}
}