2021-12-06 04:49:18 -08:00
|
|
|
// Copyright 2021 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package histogram
|
|
|
|
|
|
|
|
import (
|
2021-12-06 05:47:22 -08:00
|
|
|
"fmt"
|
2023-05-16 12:15:20 -07:00
|
|
|
"math"
|
2021-12-06 04:49:18 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-05-16 12:15:20 -07:00
|
|
|
func TestFloatHistogramMul(t *testing.T) {
|
2021-12-06 04:49:18 -08:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
in *FloatHistogram
|
|
|
|
scale float64
|
|
|
|
expected *FloatHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"zero value",
|
|
|
|
&FloatHistogram{},
|
|
|
|
3.1415,
|
|
|
|
&FloatHistogram{},
|
|
|
|
},
|
2023-05-16 12:15:20 -07:00
|
|
|
{
|
|
|
|
"zero multiplier",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 0,
|
|
|
|
Count: 0,
|
|
|
|
Sum: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 0, 0},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
{
|
|
|
|
"no-op",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"double",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 11,
|
|
|
|
Count: 3493.3 * 2,
|
|
|
|
Sum: 2349209.324 * 2,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{2, 6.6, 8.4, 0.2},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{6.2, 6, 1.234e5 * 2, 2000},
|
|
|
|
},
|
|
|
|
},
|
2023-05-16 12:15:20 -07:00
|
|
|
{
|
|
|
|
"triple",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 11,
|
|
|
|
Count: 30,
|
|
|
|
Sum: 23,
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 33,
|
|
|
|
Count: 90,
|
|
|
|
Sum: 69,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{3, 0, 9, 12, 21},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{9, 3, 15, 18},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2023-05-16 12:15:20 -07:00
|
|
|
require.Equal(t, c.expected, c.in.Mul(c.scale))
|
2021-12-06 04:49:18 -08:00
|
|
|
// Has it also happened in-place?
|
|
|
|
require.Equal(t, c.expected, c.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 12:15:20 -07:00
|
|
|
func TestFloatHistogramDiv(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fh *FloatHistogram
|
|
|
|
s float64
|
|
|
|
expected *FloatHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"zero value",
|
|
|
|
&FloatHistogram{},
|
|
|
|
3.1415,
|
|
|
|
&FloatHistogram{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero divisor",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: math.Inf(1),
|
|
|
|
Count: math.Inf(1),
|
|
|
|
Sum: math.Inf(1),
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1)},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{math.Inf(1), math.Inf(1), math.Inf(1), math.Inf(1)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no-op",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"half",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 11,
|
|
|
|
Count: 30,
|
|
|
|
Sum: 23,
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 15,
|
|
|
|
Sum: 11.5,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{0.5, 0, 1.5, 2, 3.5},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{1.5, 0.5, 2.5, 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.fh.Div(c.s))
|
|
|
|
// Has it also happened in-place?
|
|
|
|
require.Equal(t, c.expected, c.fh)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 04:49:18 -08:00
|
|
|
func TestFloatHistogramDetectReset(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
previous, current *FloatHistogram
|
|
|
|
resetExpected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"zero values",
|
|
|
|
&FloatHistogram{},
|
|
|
|
&FloatHistogram{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"no buckets to some buckets",
|
|
|
|
&FloatHistogram{},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"some buckets to no buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one bucket appears, nothing else changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one bucket disappears, nothing else changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"an unpopulated bucket disappears, nothing else changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"an unpopulated bucket at the end disappears, nothing else changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {1, 2}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"an unpopulated bucket disappears in a histogram with nothing else",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{23, 1}},
|
|
|
|
PositiveBuckets: []float64{0},
|
|
|
|
},
|
|
|
|
&FloatHistogram{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero count goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.6,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero count goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.4,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.4,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.2,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sum goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349210,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"sum goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349200,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one positive bucket goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.3, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one positive bucket goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.1, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one negative bucket goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3.1, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"one negative bucket goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 2.9, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2022-01-06 08:44:30 -08:00
|
|
|
{
|
|
|
|
"zero threshold decreases",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.009,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero threshold increases without touching any existing buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.011,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero threshold increases enough to cover existing buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 1,
|
|
|
|
ZeroCount: 7.73,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{1, 3}},
|
|
|
|
PositiveBuckets: []float64{3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"zero threshold increases into the middle of an existing buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.3,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"schema increases without any other changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{{-5, 4}, {2, 6}},
|
|
|
|
PositiveBuckets: []float64{0.4, 0.6, 1, 0.23, 2, 1.3, 1.2, 3, 0.05, 0.05},
|
|
|
|
NegativeSpans: []Span{{5, 4}, {6, 4}},
|
|
|
|
NegativeBuckets: []float64{2, 1.1, 2, 1, 0.234e5, 1e5, 500, 500},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"schema decreases without any other changes",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{{-5, 4}, {2, 6}},
|
|
|
|
PositiveBuckets: []float64{0.4, 0.6, 1, 0.23, 2, 1.3, 1.2, 3, 0.05, 0.05},
|
|
|
|
NegativeSpans: []Span{{5, 4}, {6, 4}},
|
|
|
|
NegativeBuckets: []float64{2, 1.1, 2, 1, 0.234e5, 1e5, 500, 500},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"schema decreases and a bucket goes up",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{{-5, 4}, {2, 6}},
|
|
|
|
PositiveBuckets: []float64{0.4, 0.6, 1, 0.23, 2, 1.3, 1.2, 3, 0.05, 0.05},
|
|
|
|
NegativeSpans: []Span{{5, 4}, {6, 4}},
|
|
|
|
NegativeBuckets: []float64{2, 1.1, 2, 1, 0.234e5, 1e5, 500, 500},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 4.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"schema decreases and a bucket goes down",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{{-5, 4}, {2, 6}},
|
|
|
|
PositiveBuckets: []float64{0.4, 0.6, 1, 0.23, 2, 1.3, 1.2, 3, 0.05, 0.05},
|
|
|
|
NegativeSpans: []Span{{5, 4}, {6, 4}},
|
|
|
|
NegativeBuckets: []float64{2, 1.1, 2, 1, 0.234e5, 1e5, 500, 500},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 5.5,
|
|
|
|
Count: 3493.3,
|
|
|
|
Sum: 2349209.324,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 1.23, 2.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.resetExpected, c.current.DetectReset(c.previous))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFloatHistogramCompact(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
in *FloatHistogram
|
|
|
|
maxEmptyBuckets int
|
|
|
|
expected *FloatHistogram
|
|
|
|
}{
|
2021-12-21 10:11:45 -08:00
|
|
|
{
|
|
|
|
"empty histogram",
|
|
|
|
&FloatHistogram{},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"nothing should happen",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eliminate zero offsets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {0, 3}, {0, 1}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {0, 2}, {2, 1}, {0, 1}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 5}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 4}, {2, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eliminate zero length",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {2, 0}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {0, 0}, {2, 0}, {1, 4}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 4}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"eliminate multiple zero length spans",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {2, 0}, {2, 0}, {2, 0}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {9, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cut empty buckets at start or end",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 4}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4, 0},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 4}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cut empty buckets at start and end",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 4}, {5, 6}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 3.3, 4.2, 0.1, 3.3, 0, 0, 0},
|
|
|
|
NegativeSpans: []Span{{-2, 4}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{0, 0, 3.1, 3, 1.234e5, 1000, 3, 4, 0},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 4}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
2023-08-25 14:35:42 -07:00
|
|
|
{
|
|
|
|
"cut empty buckets in the middle",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{5, 4}},
|
|
|
|
PositiveBuckets: []float64{1, 3, 0, 2},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 5, Length: 2},
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{1, 3, 2},
|
|
|
|
},
|
|
|
|
},
|
2021-12-21 10:11:45 -08:00
|
|
|
{
|
2022-04-11 15:37:50 -07:00
|
|
|
"cut empty buckets at start or end of spans, even in the middle",
|
2021-12-21 10:11:45 -08:00
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 6}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 3.3, 0, 0, 4.2, 0.1, 3.3, 0, 0, 0},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {2, 6}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 0, 1.234e5, 1000, 3, 4, 0},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 4}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-25 14:35:42 -07:00
|
|
|
"cut empty buckets at start and end - also merge spans due to maxEmptyBuckets",
|
2021-12-21 10:11:45 -08:00
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 4}, {5, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4, 0},
|
|
|
|
},
|
|
|
|
10,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 10}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 0, 0, 0, 0, 0, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 9}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 0, 0, 0, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-04-11 15:37:50 -07:00
|
|
|
"cut empty buckets from the middle of a span",
|
2021-12-21 10:11:45 -08:00
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 1}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 2}, {1, 2}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-04-11 15:37:50 -07:00
|
|
|
"cut out a span containing only empty buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 3}, {2, 2}, {3, 4}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {7, 4}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
},
|
|
|
|
},
|
2023-08-25 14:35:42 -07:00
|
|
|
{
|
|
|
|
"cut empty buckets from the middle of a span, avoiding none due to maxEmptyBuckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 4}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 0, 3.3},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 1}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cut empty buckets and merge spans due to maxEmptyBuckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 4}, {3, 1}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 0, 3.3, 4.2},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 1}, {3, 1}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2},
|
|
|
|
},
|
|
|
|
},
|
2022-04-11 15:37:50 -07:00
|
|
|
{
|
|
|
|
"cut empty buckets from the middle of a span, avoiding some due to maxEmptyBuckets",
|
2021-12-21 10:11:45 -08:00
|
|
|
&FloatHistogram{
|
2023-08-25 14:35:42 -07:00
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 3}, {10, 2}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3, 2, 3},
|
2021-12-21 10:11:45 -08:00
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
&FloatHistogram{
|
2023-08-25 14:35:42 -07:00
|
|
|
PositiveSpans: []Span{{-2, 1}, {2, 1}, {3, 3}, {10, 2}},
|
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3, 2, 3},
|
2021-12-21 10:11:45 -08:00
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"avoiding all cutting of empty buckets from the middle of a chunk due to maxEmptyBuckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
2,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 4}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 0, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"everything merged into one span due to maxEmptyBuckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 2}, {3, 5}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-2, 10}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 0, 3.3, 0, 0, 0, 4.2, 0.1, 3.3},
|
|
|
|
NegativeSpans: []Span{{0, 10}},
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 0, 0, 0, 1.234e5, 1000, 0, 3, 4},
|
|
|
|
},
|
|
|
|
},
|
2022-04-07 07:24:48 -07:00
|
|
|
{
|
|
|
|
"only empty buckets and maxEmptyBuckets greater zero",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-4, 6}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
NegativeSpans: []Span{{0, 7}},
|
|
|
|
NegativeBuckets: []float64{0, 0, 0, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{},
|
|
|
|
PositiveBuckets: []float64{},
|
|
|
|
NegativeSpans: []Span{},
|
|
|
|
NegativeBuckets: []float64{},
|
|
|
|
},
|
|
|
|
},
|
2022-04-11 15:37:50 -07:00
|
|
|
{
|
|
|
|
"multiple spans of only empty buckets",
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-10, 2}, {2, 1}, {3, 3}},
|
|
|
|
PositiveBuckets: []float64{0, 0, 0, 0, 2, 3},
|
|
|
|
NegativeSpans: []Span{{-10, 2}, {2, 1}, {3, 3}},
|
|
|
|
NegativeBuckets: []float64{2, 3, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
&FloatHistogram{
|
|
|
|
PositiveSpans: []Span{{-1, 2}},
|
|
|
|
PositiveBuckets: []float64{2, 3},
|
|
|
|
NegativeSpans: []Span{{-10, 2}},
|
|
|
|
NegativeBuckets: []float64{2, 3},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.in.Compact(c.maxEmptyBuckets))
|
histograms: Add Compact method to the normal integer Histogram
And use the new method to call to compact Histograms during
parsing. This happens for both `Histogram` and `FloatHistogram`. In
this way, if targets decide to optimize the exposition size by merging
spans with empty buckets in between, we still get a normalized
results. It will also normalize away any valid but weird
representations like empty spans, spans with offset zero, and empty
buckets at the start or end of a span.
The implementation seemed easy at first as it just turns the
`compactBuckets` helper into a generic function (which now got its own
file). However, the integer Histograms have delta buckets instead of
absolute buckets, which had to be treated specially in the generic
`compactBuckets` function. To make sure it works, I have added plenty
of explicit tests for `Histogram` in addition to the `FloatHistogram`
tests.
I have also updated the doc comment for the `Compact` method.
Based on the insights now expressed in the doc comment, compacting
with a maxEmptyBuckets > 0 is rarely useful. Therefore, this commit
also sets the value to 0 in the two cases we were using 3 so far. We
might still want to reconsider, so I don't want to remove the
maxEmptyBuckets parameter right now.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-09-27 04:04:16 -07:00
|
|
|
// Compact has happened in-place, too.
|
2021-12-06 04:49:18 -08:00
|
|
|
require.Equal(t, c.expected, c.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFloatHistogramAdd(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{
|
2022-01-06 08:44:30 -08:00
|
|
|
ZeroThreshold: 0.001,
|
2021-12-06 04:49:18 -08:00
|
|
|
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{
|
2022-01-06 08:44:30 -08:00
|
|
|
ZeroThreshold: 0.001,
|
2021-12-06 04:49:18 -08:00
|
|
|
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{
|
2022-01-06 08:44:30 -08:00
|
|
|
ZeroThreshold: 0.001,
|
2021-12-06 04:49:18 -08:00
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
2022-01-06 08:44:30 -08:00
|
|
|
{
|
|
|
|
"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},
|
histograms: Add Compact method to the normal integer Histogram
And use the new method to call to compact Histograms during
parsing. This happens for both `Histogram` and `FloatHistogram`. In
this way, if targets decide to optimize the exposition size by merging
spans with empty buckets in between, we still get a normalized
results. It will also normalize away any valid but weird
representations like empty spans, spans with offset zero, and empty
buckets at the start or end of a span.
The implementation seemed easy at first as it just turns the
`compactBuckets` helper into a generic function (which now got its own
file). However, the integer Histograms have delta buckets instead of
absolute buckets, which had to be treated specially in the generic
`compactBuckets` function. To make sure it works, I have added plenty
of explicit tests for `Histogram` in addition to the `FloatHistogram`
tests.
I have also updated the doc comment for the `Compact` method.
Based on the insights now expressed in the doc comment, compacting
with a maxEmptyBuckets > 0 is rarely useful. Therefore, this commit
also sets the value to 0 in the two cases we were using 3 so far. We
might still want to reconsider, so I don't want to remove the
maxEmptyBuckets parameter right now.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-09-27 04:04:16 -07:00
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
2022-01-06 08:44:30 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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},
|
histograms: Add Compact method to the normal integer Histogram
And use the new method to call to compact Histograms during
parsing. This happens for both `Histogram` and `FloatHistogram`. In
this way, if targets decide to optimize the exposition size by merging
spans with empty buckets in between, we still get a normalized
results. It will also normalize away any valid but weird
representations like empty spans, spans with offset zero, and empty
buckets at the start or end of a span.
The implementation seemed easy at first as it just turns the
`compactBuckets` helper into a generic function (which now got its own
file). However, the integer Histograms have delta buckets instead of
absolute buckets, which had to be treated specially in the generic
`compactBuckets` function. To make sure it works, I have added plenty
of explicit tests for `Histogram` in addition to the `FloatHistogram`
tests.
I have also updated the doc comment for the `Compact` method.
Based on the insights now expressed in the doc comment, compacting
with a maxEmptyBuckets > 0 is rarely useful. Therefore, this commit
also sets the value to 0 in the two cases we were using 3 so far. We
might still want to reconsider, so I don't want to remove the
maxEmptyBuckets parameter right now.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-09-27 04:04:16 -07:00
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
2022-01-06 08:44:30 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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},
|
histograms: Add Compact method to the normal integer Histogram
And use the new method to call to compact Histograms during
parsing. This happens for both `Histogram` and `FloatHistogram`. In
this way, if targets decide to optimize the exposition size by merging
spans with empty buckets in between, we still get a normalized
results. It will also normalize away any valid but weird
representations like empty spans, spans with offset zero, and empty
buckets at the start or end of a span.
The implementation seemed easy at first as it just turns the
`compactBuckets` helper into a generic function (which now got its own
file). However, the integer Histograms have delta buckets instead of
absolute buckets, which had to be treated specially in the generic
`compactBuckets` function. To make sure it works, I have added plenty
of explicit tests for `Histogram` in addition to the `FloatHistogram`
tests.
I have also updated the doc comment for the `Compact` method.
Based on the insights now expressed in the doc comment, compacting
with a maxEmptyBuckets > 0 is rarely useful. Therefore, this commit
also sets the value to 0 in the two cases we were using 3 so far. We
might still want to reconsider, so I don't want to remove the
maxEmptyBuckets parameter right now.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-09-27 04:04:16 -07:00
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
2022-01-06 08:44:30 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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},
|
histograms: Add Compact method to the normal integer Histogram
And use the new method to call to compact Histograms during
parsing. This happens for both `Histogram` and `FloatHistogram`. In
this way, if targets decide to optimize the exposition size by merging
spans with empty buckets in between, we still get a normalized
results. It will also normalize away any valid but weird
representations like empty spans, spans with offset zero, and empty
buckets at the start or end of a span.
The implementation seemed easy at first as it just turns the
`compactBuckets` helper into a generic function (which now got its own
file). However, the integer Histograms have delta buckets instead of
absolute buckets, which had to be treated specially in the generic
`compactBuckets` function. To make sure it works, I have added plenty
of explicit tests for `Histogram` in addition to the `FloatHistogram`
tests.
I have also updated the doc comment for the `Compact` method.
Based on the insights now expressed in the doc comment, compacting
with a maxEmptyBuckets > 0 is rarely useful. Therefore, this commit
also sets the value to 0 in the two cases we were using 3 so far. We
might still want to reconsider, so I don't want to remove the
maxEmptyBuckets parameter right now.
Signed-off-by: beorn7 <beorn@grafana.com>
2022-09-27 04:04:16 -07:00
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{3, 2, 1, 4, 9, 6},
|
2022-01-06 08:44:30 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.in1.Add(c.in2))
|
|
|
|
// Has it also happened in-place?
|
|
|
|
require.Equal(t, c.expected, c.in1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFloatHistogramSub(t *testing.T) {
|
|
|
|
// This has fewer test cases than TestFloatHistogramAdd because Add and
|
|
|
|
// Sub share most of the trickier code.
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
in1, in2, expected *FloatHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"same bucket layout",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 11,
|
|
|
|
Count: 30,
|
|
|
|
Sum: 23,
|
|
|
|
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: 12,
|
|
|
|
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: 3,
|
|
|
|
Count: 9,
|
|
|
|
Sum: 11,
|
|
|
|
PositiveSpans: []Span{{-2, 2}, {1, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 1, 1, 1},
|
|
|
|
NegativeSpans: []Span{{3, 2}, {3, 2}},
|
|
|
|
NegativeBuckets: []float64{2, 0, 1, 2},
|
|
|
|
},
|
|
|
|
},
|
2022-01-06 08:44:30 -08:00
|
|
|
{
|
|
|
|
"schema change",
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 8,
|
|
|
|
Count: 59,
|
|
|
|
Sum: 1.234,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{{-2, 5}, {0, 3}},
|
|
|
|
PositiveBuckets: []float64{2, 5, 4, 2, 3, 6, 7, 5},
|
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{4, 10, 1, 4, 14, 7},
|
|
|
|
},
|
|
|
|
&FloatHistogram{
|
|
|
|
ZeroThreshold: 0.01,
|
|
|
|
ZeroCount: 2,
|
|
|
|
Count: 19,
|
|
|
|
Sum: 0.345,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{{-4, 3}, {5, 5}},
|
|
|
|
PositiveBuckets: []float64{1, 0, 0, 1, 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: 6,
|
|
|
|
Count: 40,
|
|
|
|
Sum: 0.889,
|
|
|
|
PositiveSpans: []Span{{-2, 5}, {0, 3}},
|
|
|
|
PositiveBuckets: []float64{1, 5, 4, 2, 2, 2, 0, 5},
|
|
|
|
NegativeSpans: []Span{{3, 3}, {1, 3}},
|
|
|
|
NegativeBuckets: []float64{1, 9, 1, 4, 9, 1},
|
|
|
|
},
|
|
|
|
},
|
2021-12-06 04:49:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.in1.Sub(c.in2))
|
|
|
|
// Has it also happened in-place?
|
|
|
|
require.Equal(t, c.expected, c.in1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 05:47:22 -08:00
|
|
|
|
2022-01-06 08:44:30 -08:00
|
|
|
func TestFloatHistogramCopyToSchema(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
targetSchema int32
|
|
|
|
in, expected *FloatHistogram
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"no schema change",
|
|
|
|
1,
|
|
|
|
&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: 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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"schema change",
|
|
|
|
0,
|
|
|
|
&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: 11,
|
|
|
|
Count: 30,
|
|
|
|
Sum: 2.345,
|
|
|
|
Schema: 0,
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.in.CopyToSchema(c.targetSchema))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 05:47:22 -08:00
|
|
|
func TestReverseFloatBucketIterator(t *testing.T) {
|
|
|
|
h := &FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assuming that the regular iterator is correct.
|
|
|
|
|
|
|
|
// Positive buckets.
|
2022-10-03 04:15:27 -07:00
|
|
|
var expBuckets, actBuckets []Bucket[float64]
|
2021-12-06 05:47:22 -08:00
|
|
|
it := h.PositiveBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
// Append in reverse to check reversed list.
|
2022-10-03 04:15:27 -07:00
|
|
|
expBuckets = append([]Bucket[float64]{it.At()}, expBuckets...)
|
2021-12-06 05:47:22 -08:00
|
|
|
}
|
|
|
|
it = h.PositiveReverseBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
actBuckets = append(actBuckets, it.At())
|
|
|
|
}
|
|
|
|
require.Greater(t, len(expBuckets), 0)
|
|
|
|
require.Greater(t, len(actBuckets), 0)
|
|
|
|
require.Equal(t, expBuckets, actBuckets)
|
|
|
|
|
|
|
|
// Negative buckets.
|
|
|
|
expBuckets = expBuckets[:0]
|
|
|
|
actBuckets = actBuckets[:0]
|
|
|
|
it = h.NegativeBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
// Append in reverse to check reversed list.
|
2022-10-03 04:15:27 -07:00
|
|
|
expBuckets = append([]Bucket[float64]{it.At()}, expBuckets...)
|
2021-12-06 05:47:22 -08:00
|
|
|
}
|
|
|
|
it = h.NegativeReverseBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
actBuckets = append(actBuckets, it.At())
|
|
|
|
}
|
|
|
|
require.Greater(t, len(expBuckets), 0)
|
|
|
|
require.Greater(t, len(actBuckets), 0)
|
|
|
|
require.Equal(t, expBuckets, actBuckets)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllFloatBucketIterator(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
h FloatHistogram
|
|
|
|
// To determine the expected buckets.
|
|
|
|
includeNeg, includeZero, includePos bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: false,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
},
|
|
|
|
includeNeg: false,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 0,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: false,
|
|
|
|
includePos: true,
|
|
|
|
},
|
2022-05-03 07:24:11 -07:00
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 447,
|
|
|
|
ZeroCount: 42,
|
|
|
|
ZeroThreshold: 0.5, // Coinciding with bucket boundary.
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 447,
|
|
|
|
ZeroCount: 42,
|
|
|
|
ZeroThreshold: 0.6, // Within the bucket closest to zero.
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
2021-12-06 05:47:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2022-10-03 04:15:27 -07:00
|
|
|
var expBuckets, actBuckets []Bucket[float64]
|
2021-12-06 05:47:22 -08:00
|
|
|
|
|
|
|
if c.includeNeg {
|
|
|
|
it := c.h.NegativeReverseBucketIterator()
|
|
|
|
for it.Next() {
|
2022-05-03 07:24:11 -07:00
|
|
|
b := it.At()
|
|
|
|
if c.includeZero && b.Upper > -c.h.ZeroThreshold {
|
|
|
|
b.Upper = -c.h.ZeroThreshold
|
|
|
|
}
|
|
|
|
expBuckets = append(expBuckets, b)
|
2021-12-06 05:47:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.includeZero {
|
2022-10-03 04:15:27 -07:00
|
|
|
expBuckets = append(expBuckets, Bucket[float64]{
|
2021-12-06 05:47:22 -08:00
|
|
|
Lower: -c.h.ZeroThreshold,
|
|
|
|
Upper: c.h.ZeroThreshold,
|
|
|
|
LowerInclusive: true,
|
|
|
|
UpperInclusive: true,
|
|
|
|
Count: c.h.ZeroCount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.includePos {
|
|
|
|
it := c.h.PositiveBucketIterator()
|
|
|
|
for it.Next() {
|
2022-05-03 07:24:11 -07:00
|
|
|
b := it.At()
|
|
|
|
if c.includeZero && b.Lower < c.h.ZeroThreshold {
|
|
|
|
b.Lower = c.h.ZeroThreshold
|
|
|
|
}
|
|
|
|
expBuckets = append(expBuckets, b)
|
2021-12-06 05:47:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 07:50:37 -08:00
|
|
|
it := c.h.AllBucketIterator()
|
2021-12-06 05:47:22 -08:00
|
|
|
for it.Next() {
|
|
|
|
actBuckets = append(actBuckets, it.At())
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expBuckets, actBuckets)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 14:54:29 -07:00
|
|
|
|
|
|
|
func TestAllReverseFloatBucketIterator(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
h FloatHistogram
|
|
|
|
// To determine the expected buckets.
|
|
|
|
includeNeg, includeZero, includePos bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: false,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 102,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
},
|
|
|
|
includeNeg: false,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
ZeroCount: 0,
|
|
|
|
ZeroThreshold: 0.001,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: false,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 447,
|
|
|
|
ZeroCount: 42,
|
|
|
|
ZeroThreshold: 0.5, // Coinciding with bucket boundary.
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
h: FloatHistogram{
|
|
|
|
Count: 447,
|
|
|
|
ZeroCount: 42,
|
|
|
|
ZeroThreshold: 0.6, // Within the bucket closest to zero.
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 0,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 1, Length: 0},
|
|
|
|
{Offset: 3, Length: 0},
|
|
|
|
{Offset: 3, Length: 4},
|
|
|
|
{Offset: 2, Length: 0},
|
|
|
|
{Offset: 5, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
},
|
|
|
|
includeNeg: true,
|
|
|
|
includeZero: true,
|
|
|
|
includePos: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
var expBuckets, actBuckets []Bucket[float64]
|
|
|
|
|
|
|
|
if c.includePos {
|
|
|
|
it := c.h.PositiveReverseBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
b := it.At()
|
|
|
|
if c.includeZero && b.Lower < c.h.ZeroThreshold {
|
|
|
|
b.Lower = c.h.ZeroThreshold
|
|
|
|
}
|
|
|
|
expBuckets = append(expBuckets, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.includeZero {
|
|
|
|
expBuckets = append(expBuckets, Bucket[float64]{
|
|
|
|
Lower: -c.h.ZeroThreshold,
|
|
|
|
Upper: c.h.ZeroThreshold,
|
|
|
|
LowerInclusive: true,
|
|
|
|
UpperInclusive: true,
|
|
|
|
Count: c.h.ZeroCount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.includeNeg {
|
|
|
|
it := c.h.NegativeBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
b := it.At()
|
|
|
|
if c.includeZero && b.Upper > -c.h.ZeroThreshold {
|
|
|
|
b.Upper = -c.h.ZeroThreshold
|
|
|
|
}
|
|
|
|
expBuckets = append(expBuckets, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it := c.h.AllReverseBucketIterator()
|
|
|
|
for it.Next() {
|
|
|
|
actBuckets = append(actBuckets, it.At())
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expBuckets, actBuckets)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-07-18 09:17:47 -07:00
|
|
|
|
|
|
|
func TestFloatBucketIteratorTargetSchema(t *testing.T) {
|
|
|
|
h := FloatHistogram{
|
|
|
|
Count: 405,
|
|
|
|
Sum: 1008.4,
|
|
|
|
Schema: 1,
|
|
|
|
PositiveSpans: []Span{
|
|
|
|
{Offset: 0, Length: 4},
|
|
|
|
{Offset: 1, Length: 3},
|
|
|
|
{Offset: 2, Length: 3},
|
|
|
|
},
|
|
|
|
PositiveBuckets: []float64{100, 344, 123, 55, 3, 63, 2, 54, 235, 33},
|
|
|
|
NegativeSpans: []Span{
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
{Offset: 7, Length: 4},
|
|
|
|
{Offset: 1, Length: 3},
|
|
|
|
},
|
|
|
|
NegativeBuckets: []float64{10, 34, 1230, 54, 67, 63, 2, 554, 235, 33},
|
|
|
|
}
|
|
|
|
expPositiveBuckets := []Bucket[float64]{
|
|
|
|
{Lower: 0.25, Upper: 1, LowerInclusive: false, UpperInclusive: true, Count: 100, Index: 0},
|
|
|
|
{Lower: 1, Upper: 4, LowerInclusive: false, UpperInclusive: true, Count: 522, Index: 1},
|
|
|
|
{Lower: 4, Upper: 16, LowerInclusive: false, UpperInclusive: true, Count: 68, Index: 2},
|
|
|
|
{Lower: 16, Upper: 64, LowerInclusive: false, UpperInclusive: true, Count: 322, Index: 3},
|
|
|
|
}
|
|
|
|
expNegativeBuckets := []Bucket[float64]{
|
|
|
|
{Lower: -1, Upper: -0.25, LowerInclusive: true, UpperInclusive: false, Count: 10, Index: 0},
|
|
|
|
{Lower: -4, Upper: -1, LowerInclusive: true, UpperInclusive: false, Count: 1264, Index: 1},
|
|
|
|
{Lower: -64, Upper: -16, LowerInclusive: true, UpperInclusive: false, Count: 184, Index: 3},
|
|
|
|
{Lower: -256, Upper: -64, LowerInclusive: true, UpperInclusive: false, Count: 791, Index: 4},
|
|
|
|
{Lower: -1024, Upper: -256, LowerInclusive: true, UpperInclusive: false, Count: 33, Index: 5},
|
|
|
|
}
|
|
|
|
|
|
|
|
it := h.floatBucketIterator(true, 0, -1)
|
|
|
|
for i, b := range expPositiveBuckets {
|
|
|
|
require.True(t, it.Next(), "positive iterator exhausted too early")
|
|
|
|
require.Equal(t, b, it.At(), "bucket %d", i)
|
|
|
|
}
|
|
|
|
require.False(t, it.Next(), "positive iterator not exhausted")
|
|
|
|
|
|
|
|
it = h.floatBucketIterator(false, 0, -1)
|
|
|
|
for i, b := range expNegativeBuckets {
|
|
|
|
require.True(t, it.Next(), "negative iterator exhausted too early")
|
|
|
|
require.Equal(t, b, it.At(), "bucket %d", i)
|
|
|
|
}
|
|
|
|
require.False(t, it.Next(), "negative iterator not exhausted")
|
|
|
|
}
|
2023-09-27 23:06:54 -07:00
|
|
|
|
|
|
|
// TestFloatHistogramEquals tests FloatHistogram with float-specific cases that
|
|
|
|
// cannot be covered by TestHistogramEquals.
|
|
|
|
func TestFloatHistogramEquals(t *testing.T) {
|
|
|
|
h1 := FloatHistogram{
|
2023-10-09 06:09:46 -07:00
|
|
|
Schema: 3,
|
|
|
|
Count: 2.2,
|
|
|
|
Sum: 9.7,
|
|
|
|
ZeroThreshold: 0.1,
|
|
|
|
ZeroCount: 1.1,
|
|
|
|
PositiveBuckets: []float64{3},
|
|
|
|
NegativeBuckets: []float64{4},
|
2023-09-27 23:06:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
equals := func(h1, h2 FloatHistogram) {
|
|
|
|
require.True(t, h1.Equals(&h2))
|
|
|
|
require.True(t, h2.Equals(&h1))
|
|
|
|
}
|
|
|
|
notEquals := func(h1, h2 FloatHistogram) {
|
|
|
|
require.False(t, h1.Equals(&h2))
|
|
|
|
require.False(t, h2.Equals(&h1))
|
|
|
|
}
|
|
|
|
|
|
|
|
h2 := h1.Copy()
|
|
|
|
equals(h1, *h2)
|
|
|
|
|
|
|
|
// Count is NaN (but not a StaleNaN).
|
|
|
|
hCountNaN := h1.Copy()
|
|
|
|
hCountNaN.Count = math.NaN()
|
|
|
|
notEquals(h1, *hCountNaN)
|
|
|
|
equals(*hCountNaN, *hCountNaN)
|
|
|
|
|
|
|
|
// ZeroCount is NaN (but not a StaleNaN).
|
|
|
|
hZeroCountNaN := h1.Copy()
|
|
|
|
hZeroCountNaN.ZeroCount = math.NaN()
|
|
|
|
notEquals(h1, *hZeroCountNaN)
|
|
|
|
equals(*hZeroCountNaN, *hZeroCountNaN)
|
2023-10-09 06:09:46 -07:00
|
|
|
|
|
|
|
// Positive bucket value is NaN.
|
|
|
|
hPosBucketNaN := h1.Copy()
|
|
|
|
hPosBucketNaN.PositiveBuckets[0] = math.NaN()
|
|
|
|
notEquals(h1, *hPosBucketNaN)
|
|
|
|
equals(*hPosBucketNaN, *hPosBucketNaN)
|
|
|
|
|
|
|
|
// Negative bucket value is NaN.
|
|
|
|
hNegBucketNaN := h1.Copy()
|
|
|
|
hNegBucketNaN.NegativeBuckets[0] = math.NaN()
|
|
|
|
notEquals(h1, *hNegBucketNaN)
|
|
|
|
equals(*hNegBucketNaN, *hNegBucketNaN)
|
2023-09-27 23:06:54 -07:00
|
|
|
}
|
2023-07-28 01:49:36 -07:00
|
|
|
|
|
|
|
func TestFloatHistogramSize(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
fh *FloatHistogram
|
|
|
|
expected int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"without spans and buckets",
|
2023-10-18 02:53:42 -07:00
|
|
|
&FloatHistogram{ // 8 bytes.
|
|
|
|
CounterResetHint: 0, // 1 byte.
|
|
|
|
Schema: 1, // 4 bytes.
|
|
|
|
ZeroThreshold: 0.01, // 8 bytes.
|
|
|
|
ZeroCount: 5.5, // 8 bytes.
|
|
|
|
Count: 3493.3, // 8 bytes.
|
|
|
|
Sum: 2349209.324, // 8 bytes.
|
|
|
|
PositiveSpans: nil, // 24 bytes.
|
|
|
|
PositiveBuckets: nil, // 24 bytes.
|
|
|
|
NegativeSpans: nil, // 24 bytes.
|
|
|
|
NegativeBuckets: nil, // 24 bytes.
|
2023-07-28 01:49:36 -07:00
|
|
|
},
|
2023-10-18 02:49:56 -07:00
|
|
|
8 + 4 + 4 + 8 + 8 + 8 + 8 + 24 + 24 + 24 + 24,
|
2023-07-28 01:49:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"complete struct",
|
2023-10-18 02:53:42 -07:00
|
|
|
&FloatHistogram{ // 8 bytes.
|
|
|
|
CounterResetHint: 0, // 1 byte.
|
|
|
|
Schema: 1, // 4 bytes.
|
|
|
|
ZeroThreshold: 0.01, // 8 bytes.
|
|
|
|
ZeroCount: 5.5, // 8 bytes.
|
|
|
|
Count: 3493.3, // 8 bytes.
|
|
|
|
Sum: 2349209.324, // 8 bytes.
|
|
|
|
PositiveSpans: []Span{ // 24 bytes.
|
|
|
|
{-2, 1}, // 2 * 4 bytes.
|
|
|
|
{2, 3}, // 2 * 4 bytes.
|
2023-07-28 01:49:36 -07:00
|
|
|
},
|
2023-10-18 02:53:42 -07:00
|
|
|
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1}, // 24 bytes + 4 * 8 bytes.
|
|
|
|
NegativeSpans: []Span{ // 24 bytes.
|
|
|
|
{3, 2}, // 2 * 4 bytes.
|
|
|
|
{3, 2}}, // 2 * 4 bytes.
|
|
|
|
NegativeBuckets: []float64{3.1, 3, 1.234e5, 1000}, // 24 bytes + 4 * 8 bytes.
|
2023-07-28 01:49:36 -07:00
|
|
|
},
|
2023-10-18 02:49:56 -07:00
|
|
|
8 + 4 + 4 + 8 + 8 + 8 + 8 + (24 + 2*4 + 2*4) + (24 + 2*4 + 2*4) + (24 + 4*8) + (24 + 4*8),
|
2023-07-28 01:49:36 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
require.Equal(t, c.expected, c.fh.Size())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|