diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index 0a65dafbb..ffd991d86 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -279,8 +279,8 @@ func (h *FloatHistogram) AddNew(other *FloatHistogram) *FloatHistogram { // TODO(beorn7): If needed, this can be optimized by inspecting the // spans in other and create missing buckets in h in batches. - h.PositiveSpans, h.PositiveBuckets = mergeTwoSpans(h.Schema, h.ZeroThreshold, h.PositiveSpans, h.PositiveBuckets, otherPositiveSpans, otherPositiveBuckets) - h.NegativeSpans, h.NegativeBuckets = mergeTwoSpans(h.Schema, h.ZeroThreshold, h.NegativeSpans, h.NegativeBuckets, otherNegativeSpans, otherNegativeBuckets) + h.PositiveSpans, h.PositiveBuckets = addBuckets(h.Schema, h.ZeroThreshold, h.PositiveSpans, h.PositiveBuckets, otherPositiveSpans, otherPositiveBuckets) + h.NegativeSpans, h.NegativeBuckets = addBuckets(h.Schema, h.ZeroThreshold, h.NegativeSpans, h.NegativeBuckets, otherNegativeSpans, otherNegativeBuckets) return h } @@ -1080,7 +1080,7 @@ func mergeToSchema(originSpans []Span, originBuckets []float64, originSchema, ta return targetSpans, targetBuckets } -func mergeTwoSpans(schema int32, threshold float64, spansA []Span, bucketsA []float64, spansB []Span, bucketsB []float64) ([]Span, []float64) { +func addBuckets(schema int32, threshold float64, spansA []Span, bucketsA []float64, spansB []Span, bucketsB []float64) ([]Span, []float64) { var ( iSpan int = -1 iBucket int = -1 diff --git a/model/histogram/float_histogram_test.go b/model/histogram/float_histogram_test.go index 5d1e96931..bef9a1d6c 100644 --- a/model/histogram/float_histogram_test.go +++ b/model/histogram/float_histogram_test.go @@ -16,6 +16,7 @@ package histogram import ( "fmt" "math" + "math/rand" "testing" "github.com/stretchr/testify/require" @@ -2705,67 +2706,42 @@ func TestFloatHistogramAddNew(t *testing.T) { } func BenchmarkAddOld(b *testing.B) { - // run the Fib function b.N times - - f1 := &FloatHistogram{ - ZeroThreshold: 0.01, - ZeroCount: 8, - Count: 21, - Sum: 1.234, - Schema: 0, - PositiveSpans: []Span{{-1, 4}, {0, 3}}, - PositiveBuckets: []float64{5, 4, 2, 3, 6, 2, 5}, - NegativeSpans: []Span{{4, 2}, {1, 2}}, - NegativeBuckets: []float64{1, 1, 4, 4}, - } - - f2 := &FloatHistogram{ - ZeroThreshold: 0.01, - ZeroCount: 11, - Count: 30, - Sum: 2.345, - Schema: 1, - PositiveSpans: []Span{{-4, 3}, {5, 5}}, - PositiveBuckets: []float64{1, 0, 0, 3, 2, 2, 3, 4}, - NegativeSpans: []Span{{6, 3}, {6, 4}}, - NegativeBuckets: []float64{3, 0.5, 0.5, 2, 3, 2, 4}, - } - for n := 0; n < b.N; n++ { + b.StopTimer() + f1 := createRandomFloatHistogram(50) + f2 := createRandomFloatHistogram(50) + b.StartTimer() f1.Add(f2) } } func BenchmarkAddNew(b *testing.B) { - // run the Fib function b.N times - - f1 := &FloatHistogram{ - ZeroThreshold: 0.01, - ZeroCount: 8, - Count: 21, - Sum: 1.234, - Schema: 0, - PositiveSpans: []Span{{-1, 4}, {0, 3}}, - PositiveBuckets: []float64{5, 4, 2, 3, 6, 2, 5}, - NegativeSpans: []Span{{4, 2}, {1, 2}}, - NegativeBuckets: []float64{1, 1, 4, 4}, - } - - f2 := &FloatHistogram{ - ZeroThreshold: 0.01, - ZeroCount: 11, - Count: 30, - Sum: 2.345, - Schema: 1, - PositiveSpans: []Span{{-4, 3}, {5, 5}}, - PositiveBuckets: []float64{1, 0, 0, 3, 2, 2, 3, 4}, - NegativeSpans: []Span{{6, 3}, {6, 4}}, - NegativeBuckets: []float64{3, 0.5, 0.5, 2, 3, 2, 4}, - } - for n := 0; n < b.N; n++ { + b.StopTimer() + f1 := createRandomFloatHistogram(50) + f2 := createRandomFloatHistogram(50) + b.StartTimer() f1.AddNew(f2) } - +} + +func createRandomFloatHistogram(spanNum int32) *FloatHistogram { + f := &FloatHistogram{} + f.PositiveSpans, f.PositiveBuckets = createRandomSpans(spanNum) + f.NegativeSpans, f.NegativeBuckets = createRandomSpans(spanNum) + return f +} + +func createRandomSpans(spanNum int32) ([]Span, []float64) { + Spans := make([]Span, spanNum) + Buckets := make([]float64, 0) + for i := 0; i < int(spanNum); i++ { + Spans[i].Offset = rand.Int31n(spanNum) + 1 + Spans[i].Length = uint32(rand.Int31n(spanNum) + 1) + for j := 0; j < int(Spans[i].Length); j++ { + Buckets = append(Buckets, float64(rand.Int31n(spanNum)+1)) + } + } + return Spans, Buckets }