mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
enhance floathistogram add and sub method
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
This commit is contained in:
parent
95cad0b070
commit
bf880a6e77
|
@ -238,6 +238,52 @@ func (h *FloatHistogram) Add(other *FloatHistogram) *FloatHistogram {
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *FloatHistogram) AddNew(other *FloatHistogram) *FloatHistogram {
|
||||||
|
switch {
|
||||||
|
case other.CounterResetHint == h.CounterResetHint:
|
||||||
|
// Adding apples to apples, all good. No need to change anything.
|
||||||
|
case h.CounterResetHint == GaugeType:
|
||||||
|
// Adding something else to a gauge. That's probably OK. Outcome is a gauge.
|
||||||
|
// Nothing to do since the receiver is already marked as gauge.
|
||||||
|
case other.CounterResetHint == GaugeType:
|
||||||
|
// Similar to before, but this time the receiver is "something else" and we have to change it to gauge.
|
||||||
|
h.CounterResetHint = GaugeType
|
||||||
|
case h.CounterResetHint == UnknownCounterReset:
|
||||||
|
// With the receiver's CounterResetHint being "unknown", this could still be legitimate
|
||||||
|
// if the caller knows what they are doing. Outcome is then again "unknown".
|
||||||
|
// No need to do anything since the receiver's CounterResetHint is already "unknown".
|
||||||
|
case other.CounterResetHint == UnknownCounterReset:
|
||||||
|
// Similar to before, but now we have to set the receiver's CounterResetHint to "unknown".
|
||||||
|
h.CounterResetHint = UnknownCounterReset
|
||||||
|
default:
|
||||||
|
// All other cases shouldn't actually happen.
|
||||||
|
// They are a direct collision of CounterReset and NotCounterReset.
|
||||||
|
// Conservatively set the CounterResetHint to "unknown" and isse a warning.
|
||||||
|
h.CounterResetHint = UnknownCounterReset
|
||||||
|
// TODO(trevorwhitney): Actually issue the warning as soon as the plumbing for it is in place
|
||||||
|
}
|
||||||
|
|
||||||
|
otherZeroCount := h.reconcileZeroBuckets(other)
|
||||||
|
h.ZeroCount += otherZeroCount
|
||||||
|
h.Count += other.Count
|
||||||
|
h.Sum += other.Sum
|
||||||
|
|
||||||
|
otherPositiveSpans := other.PositiveSpans
|
||||||
|
otherPositiveBuckets := other.PositiveBuckets
|
||||||
|
otherNegativeSpans := other.NegativeSpans
|
||||||
|
otherNegativeBuckets := other.NegativeBuckets
|
||||||
|
if other.Schema != h.Schema {
|
||||||
|
otherPositiveSpans, otherPositiveBuckets = mergeToSchema(other.PositiveSpans, other.PositiveBuckets, other.Schema, h.Schema)
|
||||||
|
otherNegativeSpans, otherNegativeBuckets = mergeToSchema(other.NegativeSpans, other.NegativeBuckets, other.Schema, h.Schema)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
// Sub works like Add but subtracts the other histogram.
|
// Sub works like Add but subtracts the other histogram.
|
||||||
func (h *FloatHistogram) Sub(other *FloatHistogram) *FloatHistogram {
|
func (h *FloatHistogram) Sub(other *FloatHistogram) *FloatHistogram {
|
||||||
otherZeroCount := h.reconcileZeroBuckets(other)
|
otherZeroCount := h.reconcileZeroBuckets(other)
|
||||||
|
@ -1033,3 +1079,115 @@ func mergeToSchema(originSpans []Span, originBuckets []float64, originSchema, ta
|
||||||
|
|
||||||
return targetSpans, targetBuckets
|
return targetSpans, targetBuckets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeTwoSpans(schema int32, threshold float64, spansA []Span, bucketsA []float64, spansB []Span, bucketsB []float64) ([]Span, []float64) {
|
||||||
|
var (
|
||||||
|
iSpan int = -1
|
||||||
|
iBucket int = -1
|
||||||
|
iInSpan int32
|
||||||
|
indexA int32
|
||||||
|
indexB int32 = 0
|
||||||
|
bIdxB int = 0
|
||||||
|
lowerThanThreshold = true
|
||||||
|
deltaIndex int32
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, spanB := range spansB {
|
||||||
|
indexB += spanB.Offset
|
||||||
|
for j := 0; j < int(spanB.Length); j++ {
|
||||||
|
if lowerThanThreshold && getBound(indexB, schema) <= threshold {
|
||||||
|
goto nextLoop
|
||||||
|
}
|
||||||
|
lowerThanThreshold = false
|
||||||
|
|
||||||
|
if iSpan == -1 {
|
||||||
|
if len(spansA) == 0 || spansA[0].Offset > indexB {
|
||||||
|
// Add bucket before all others.
|
||||||
|
bucketsA = append(bucketsA, 0)
|
||||||
|
copy(bucketsA[1:], bucketsA)
|
||||||
|
bucketsA[0] = bucketsB[bIdxB]
|
||||||
|
if len(spansA) > 0 && spansA[0].Offset == indexB+1 { // bIndex just preceed spansA[0] by one step
|
||||||
|
spansA[0].Length++
|
||||||
|
spansA[0].Offset--
|
||||||
|
goto nextLoop
|
||||||
|
} else { // if not create new span
|
||||||
|
spansA = append(spansA, Span{})
|
||||||
|
copy(spansA[1:], spansA)
|
||||||
|
spansA[0] = Span{Offset: indexB, Length: 1}
|
||||||
|
if len(spansA) > 1 {
|
||||||
|
// Convert the absolute offset in the formerly
|
||||||
|
// first span to a relative offset.
|
||||||
|
spansA[1].Offset -= indexB + 1
|
||||||
|
}
|
||||||
|
goto nextLoop
|
||||||
|
}
|
||||||
|
} else if spansA[0].Offset == indexB {
|
||||||
|
// Just add to first bucket.
|
||||||
|
bucketsA[0] += bucketsB[bIdxB]
|
||||||
|
goto nextLoop
|
||||||
|
}
|
||||||
|
iSpan, iBucket, iInSpan = 0, 0, 0
|
||||||
|
indexA = spansA[0].Offset
|
||||||
|
}
|
||||||
|
deltaIndex = indexB - indexA
|
||||||
|
for {
|
||||||
|
remainingInSpan := int32(spansA[iSpan].Length) - iInSpan
|
||||||
|
if deltaIndex < remainingInSpan {
|
||||||
|
// Bucket is in current span.
|
||||||
|
iBucket += int(deltaIndex)
|
||||||
|
iInSpan += deltaIndex
|
||||||
|
bucketsA[iBucket] += bucketsB[bIdxB]
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
deltaIndex -= remainingInSpan
|
||||||
|
iBucket += int(remainingInSpan)
|
||||||
|
iSpan++
|
||||||
|
if iSpan == len(spansA) || deltaIndex < spansA[iSpan].Offset {
|
||||||
|
// Bucket is in gap behind previous span (or there are no further spans).
|
||||||
|
bucketsA = append(bucketsA, 0)
|
||||||
|
copy(bucketsA[iBucket+1:], bucketsA[iBucket:])
|
||||||
|
bucketsA[iBucket] = bucketsB[bIdxB]
|
||||||
|
if deltaIndex == 0 {
|
||||||
|
// Directly after previous span, extend previous span.
|
||||||
|
if iSpan < len(spansA) {
|
||||||
|
spansA[iSpan].Offset--
|
||||||
|
}
|
||||||
|
iSpan--
|
||||||
|
iInSpan = int32(spansA[iSpan].Length)
|
||||||
|
spansA[iSpan].Length++
|
||||||
|
break
|
||||||
|
} else if iSpan < len(spansA) && deltaIndex == spansA[iSpan].Offset-1 {
|
||||||
|
// Directly before next span, extend next span.
|
||||||
|
iInSpan = 0
|
||||||
|
spansA[iSpan].Offset--
|
||||||
|
spansA[iSpan].Length++
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
// No next span, or next span is not directly adjacent to new bucket.
|
||||||
|
// Add new span.
|
||||||
|
iInSpan = 0
|
||||||
|
if iSpan < len(spansA) {
|
||||||
|
spansA[iSpan].Offset -= deltaIndex + 1
|
||||||
|
}
|
||||||
|
spansA = append(spansA, Span{})
|
||||||
|
copy(spansA[iSpan+1:], spansA[iSpan:])
|
||||||
|
spansA[iSpan] = Span{Length: 1, Offset: deltaIndex}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Try start of next span.
|
||||||
|
deltaIndex -= spansA[iSpan].Offset
|
||||||
|
iInSpan = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextLoop:
|
||||||
|
indexA = indexB
|
||||||
|
indexB++
|
||||||
|
bIdxB++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return spansA, bucketsA
|
||||||
|
}
|
||||||
|
|
|
@ -2252,3 +2252,520 @@ func TestFloatBucketIteratorTargetSchema(t *testing.T) {
|
||||||
}
|
}
|
||||||
require.False(t, it.Next(), "negative iterator not exhausted")
|
require.False(t, it.Next(), "negative iterator not exhausted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFloatHistogramAddNew(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
in1, in2, expected *FloatHistogram
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"same bucket layout",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
||||||
|
PositiveBuckets: []float64{0, 0, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 5, 7, 13},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{4, 2, 9, 10},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"same bucket layout, defined differently",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 1}, {0, 2}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 2}, {0, 1}},
|
||||||
|
PositiveBuckets: []float64{0, 0, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{3, 7}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 0, 0, 0, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {1, 1}, {0, 2}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 5, 7, 13},
|
||||||
|
NegativeSpans: []Span{{3, 5}, {0, 2}},
|
||||||
|
NegativeBuckets: []float64{4, 2, 0, 0, 0, 9, 10},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"non-overlapping spans",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{0, 2}, {3, 3}},
|
||||||
|
PositiveBuckets: []float64{5, 4, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{-9, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.001,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 4}, {0, 6}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 5, 4, 3, 4, 7, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{-9, 2}, {3, 2}, {5, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4, 3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"non-overlapping inverted order",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{0, 2}, {3, 3}},
|
||||||
|
PositiveBuckets: []float64{5, 4, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{-9, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {0, 5}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 5, 4, 3, 4, 7, 2, 3, 6},
|
||||||
|
NegativeSpans: []Span{{-9, 2}, {3, 2}, {5, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4, 3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"overlapping spans",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
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},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 4}, {0, 4}},
|
||||||
|
PositiveBuckets: []float64{1, 5, 4, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"overlapping spans inverted order",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
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},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 5}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 5, 4, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"schema change",
|
||||||
|
&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},
|
||||||
|
},
|
||||||
|
&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},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 19,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-2, 5}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 5, 4, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"larger zero bucket in first histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 1,
|
||||||
|
ZeroCount: 17,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{1, 2}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 3, 6, 2, 5},
|
||||||
|
NegativeSpans: []Span{{4, 2}, {1, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 1,
|
||||||
|
ZeroCount: 29,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{1, 2}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"larger zero bucket in second histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 1,
|
||||||
|
ZeroCount: 17,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{1, 2}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 3, 6, 2, 5},
|
||||||
|
NegativeSpans: []Span{{4, 2}, {1, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 1,
|
||||||
|
ZeroCount: 29,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{1, 5}},
|
||||||
|
PositiveBuckets: []float64{2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"larger zero threshold in first histogram ends up inside a populated bucket of second histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.2,
|
||||||
|
ZeroCount: 17,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{1, 2}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 3, 6, 2, 5},
|
||||||
|
NegativeSpans: []Span{{4, 2}, {1, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
ZeroCount: 29,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-1, 1}, {1, 5}},
|
||||||
|
PositiveBuckets: []float64{0, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"larger zero threshold in second histogram ends up inside a populated bucket of first histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 11,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
PositiveSpans: []Span{{-2, 2}, {2, 3}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 4, 7},
|
||||||
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
||||||
|
NegativeBuckets: []float64{3, 1, 5, 6},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.2,
|
||||||
|
ZeroCount: 17,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
PositiveSpans: []Span{{1, 2}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 3, 6, 2, 5},
|
||||||
|
NegativeSpans: []Span{{4, 2}, {1, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
ZeroCount: 29,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{1, 5}},
|
||||||
|
PositiveBuckets: []float64{2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"schema change combined with larger zero bucket in second histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.01,
|
||||||
|
ZeroCount: 8,
|
||||||
|
Count: 21,
|
||||||
|
Sum: 1.234,
|
||||||
|
Schema: 0,
|
||||||
|
PositiveSpans: []Span{{-2, 5}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{2, 5, 4, 2, 3, 6, 2, 5},
|
||||||
|
NegativeSpans: []Span{{4, 2}, {1, 2}},
|
||||||
|
NegativeBuckets: []float64{1, 1, 4, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
ZeroCount: 12,
|
||||||
|
Count: 30,
|
||||||
|
Sum: 2.345,
|
||||||
|
Schema: 1,
|
||||||
|
PositiveSpans: []Span{{-3, 2}, {5, 5}},
|
||||||
|
PositiveBuckets: []float64{1, 0, 3, 2, 2, 3, 4},
|
||||||
|
NegativeSpans: []Span{{6, 3}, {6, 4}},
|
||||||
|
NegativeBuckets: []float64{3, 0.5, 0.5, 2, 3, 2, 4},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
ZeroCount: 22,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-1, 7}},
|
||||||
|
PositiveBuckets: []float64{6, 4, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"schema change combined with larger zero bucket in first histogram",
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
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},
|
||||||
|
},
|
||||||
|
&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},
|
||||||
|
},
|
||||||
|
&FloatHistogram{
|
||||||
|
ZeroThreshold: 0.25,
|
||||||
|
ZeroCount: 20,
|
||||||
|
Count: 51,
|
||||||
|
Sum: 3.579,
|
||||||
|
PositiveSpans: []Span{{-1, 4}, {0, 3}},
|
||||||
|
PositiveBuckets: []float64{5, 4, 2, 6, 10, 9, 5},
|
||||||
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
||||||
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
require.Equal(t, c.expected, c.in1.AddNew(c.in2))
|
||||||
|
// Has it also happened in-place?
|
||||||
|
require.Equal(t, c.expected, c.in1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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++ {
|
||||||
|
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++ {
|
||||||
|
f1.AddNew(f2)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue