mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-02 08:31:11 -08:00
Merge pull request #3736 from tomwilkie/3715-2.1-cpu
Don't allocate a mergeSeries if there is only one series to merge.
This commit is contained in:
commit
de17498608
|
@ -201,6 +201,17 @@ type mockSeries struct {
|
||||||
iterator func() SeriesIterator
|
iterator func() SeriesIterator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newMockSeries(lset labels.Labels, samples []sample) Series {
|
||||||
|
return &mockSeries{
|
||||||
|
labels: func() labels.Labels {
|
||||||
|
return lset
|
||||||
|
},
|
||||||
|
iterator: func() SeriesIterator {
|
||||||
|
return newListSeriesIterator(samples)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *mockSeries) Labels() labels.Labels { return m.labels() }
|
func (m *mockSeries) Labels() labels.Labels { return m.labels() }
|
||||||
func (m *mockSeries) Iterator() SeriesIterator { return m.iterator() }
|
func (m *mockSeries) Iterator() SeriesIterator { return m.iterator() }
|
||||||
|
|
||||||
|
|
|
@ -303,6 +303,10 @@ type mergeSeriesSet struct {
|
||||||
// NewMergeSeriesSet returns a new series set that merges (deduplicates)
|
// NewMergeSeriesSet returns a new series set that merges (deduplicates)
|
||||||
// series returned by the input series sets when iterating.
|
// series returned by the input series sets when iterating.
|
||||||
func NewMergeSeriesSet(sets []SeriesSet) SeriesSet {
|
func NewMergeSeriesSet(sets []SeriesSet) SeriesSet {
|
||||||
|
if len(sets) == 1 {
|
||||||
|
return sets[0]
|
||||||
|
}
|
||||||
|
|
||||||
// Sets need to be pre-advanced, so we can introspect the label of the
|
// Sets need to be pre-advanced, so we can introspect the label of the
|
||||||
// series under the cursor.
|
// series under the cursor.
|
||||||
var h seriesSetHeap
|
var h seriesSetHeap
|
||||||
|
@ -340,6 +344,9 @@ func (c *mergeSeriesSet) Next() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *mergeSeriesSet) At() Series {
|
func (c *mergeSeriesSet) At() Series {
|
||||||
|
if len(c.currentSets) == 1 {
|
||||||
|
return c.currentSets[0].At()
|
||||||
|
}
|
||||||
series := []Series{}
|
series := []Series{}
|
||||||
for _, seriesSet := range c.currentSets {
|
for _, seriesSet := range c.currentSets {
|
||||||
series = append(series, seriesSet.At())
|
series = append(series, seriesSet.At())
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -218,13 +219,53 @@ func (m *mockSeriesSet) Err() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMockSeries(lset labels.Labels, samples []sample) Series {
|
var result []sample
|
||||||
return &mockSeries{
|
|
||||||
labels: func() labels.Labels {
|
func makeSeriesSet(numSeries, numSamples int) SeriesSet {
|
||||||
return lset
|
series := []Series{}
|
||||||
},
|
for j := 0; j < numSeries; j++ {
|
||||||
iterator: func() SeriesIterator {
|
labels := labels.Labels{{Name: "foo", Value: fmt.Sprintf("bar%d", j)}}
|
||||||
return newListSeriesIterator(samples)
|
samples := []sample{}
|
||||||
},
|
for k := 0; k < numSamples; k++ {
|
||||||
|
samples = append(samples, sample{t: int64(k), v: float64(k)})
|
||||||
|
}
|
||||||
|
series = append(series, newMockSeries(labels, samples))
|
||||||
|
}
|
||||||
|
return newMockSeriesSet(series...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeMergeSeriesSet(numSeriesSets, numSeries, numSamples int) SeriesSet {
|
||||||
|
seriesSets := []SeriesSet{}
|
||||||
|
for i := 0; i < numSeriesSets; i++ {
|
||||||
|
seriesSets = append(seriesSets, makeSeriesSet(numSeries, numSamples))
|
||||||
|
}
|
||||||
|
return NewMergeSeriesSet(seriesSets)
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkDrain(seriesSet SeriesSet, b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
for seriesSet.Next() {
|
||||||
|
result = drainSamples(seriesSet.At().Iterator())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNoMergeSeriesSet_100_100(b *testing.B) {
|
||||||
|
seriesSet := makeSeriesSet(100, 100)
|
||||||
|
benchmarkDrain(seriesSet, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMergeSeriesSet(b *testing.B) {
|
||||||
|
for _, bm := range []struct {
|
||||||
|
numSeriesSets, numSeries, numSamples int
|
||||||
|
}{
|
||||||
|
{1, 100, 100},
|
||||||
|
{10, 100, 100},
|
||||||
|
{100, 100, 100},
|
||||||
|
} {
|
||||||
|
seriesSet := makeMergeSeriesSet(bm.numSeriesSets, bm.numSeries, bm.numSamples)
|
||||||
|
b.Run(fmt.Sprintf("%d_%d_%d", bm.numSeriesSets, bm.numSeries, bm.numSamples), func(b *testing.B) {
|
||||||
|
benchmarkDrain(seriesSet, b)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue