|
|
|
@ -193,3 +193,129 @@ func TestHistoChunkBucketChanges(t *testing.T) {
|
|
|
|
|
require.NoError(t, it.Err())
|
|
|
|
|
require.Equal(t, exp, act)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHistoChunkAppendable(t *testing.T) {
|
|
|
|
|
c := Chunk(NewHistoChunk())
|
|
|
|
|
|
|
|
|
|
// Create fresh appender and add the first histogram.
|
|
|
|
|
app, err := c.Appender()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, 0, c.NumSamples())
|
|
|
|
|
|
|
|
|
|
ts := int64(1234567890)
|
|
|
|
|
h1 := histogram.SparseHistogram{
|
|
|
|
|
Count: 5,
|
|
|
|
|
ZeroCount: 2,
|
|
|
|
|
Sum: 18.4,
|
|
|
|
|
ZeroThreshold: 1e-125,
|
|
|
|
|
Schema: 1,
|
|
|
|
|
PositiveSpans: []histogram.Span{
|
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
|
{Offset: 2, Length: 1},
|
|
|
|
|
{Offset: 3, Length: 2},
|
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
|
},
|
|
|
|
|
PositiveBuckets: []int64{6, -3, 0, -1, 2, 1, -4}, // counts: 6, 3, 3, 2, 4, 5, 1 (total 24)
|
|
|
|
|
NegativeSpans: nil,
|
|
|
|
|
NegativeBuckets: []int64{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.AppendHistogram(ts, h1)
|
|
|
|
|
require.Equal(t, 1, c.NumSamples())
|
|
|
|
|
|
|
|
|
|
{ // New histogram that has more buckets.
|
|
|
|
|
h2 := h1
|
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
|
}
|
|
|
|
|
h2.Count += 9
|
|
|
|
|
h2.ZeroCount++
|
|
|
|
|
h2.Sum = 30
|
|
|
|
|
// Existing histogram should get values converted from the above to: 6 3 0 3 0 0 2 4 5 0 1 (previous values with some new empty buckets in between)
|
|
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
|
h2.PositiveBuckets = []int64{7, -2, -4, 2, -2, -1, 2, 3, 0, -5, 1} // 7 5 1 3 1 0 2 5 5 0 1 (total 30)
|
|
|
|
|
|
|
|
|
|
histoApp, _ := app.(*HistoAppender)
|
|
|
|
|
posInterjections, negInterjections, ok := histoApp.Appendable(h2)
|
|
|
|
|
require.Greater(t, len(posInterjections), 0)
|
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
|
require.True(t, ok) // Only new buckets came in.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ // New histogram that has a bucket missing.
|
|
|
|
|
h2 := h1
|
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
|
{Offset: 0, Length: 2},
|
|
|
|
|
{Offset: 5, Length: 2},
|
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
|
}
|
|
|
|
|
h2.Sum = 21
|
|
|
|
|
h2.PositiveBuckets = []int64{6, -3, -1, 2, 1, -4} // counts: 6, 3, 2, 4, 5, 1 (total 21)
|
|
|
|
|
|
|
|
|
|
histoApp, _ := app.(*HistoAppender)
|
|
|
|
|
posInterjections, negInterjections, ok := histoApp.Appendable(h2)
|
|
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ // New histogram that has a counter reset while buckets are same.
|
|
|
|
|
h2 := h1
|
|
|
|
|
h2.Sum = 23
|
|
|
|
|
h2.PositiveBuckets = []int64{6, -4, 1, -1, 2, 1, -4} // counts: 6, 2, 3, 2, 4, 5, 1 (total 23)
|
|
|
|
|
|
|
|
|
|
histoApp, _ := app.(*HistoAppender)
|
|
|
|
|
posInterjections, negInterjections, ok := histoApp.Appendable(h2)
|
|
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ // New histogram that has a counter reset while new buckets were added.
|
|
|
|
|
h2 := h1
|
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
|
{Offset: 0, Length: 3},
|
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
|
{Offset: 1, Length: 4},
|
|
|
|
|
{Offset: 3, Length: 3},
|
|
|
|
|
}
|
|
|
|
|
h2.Sum = 29
|
|
|
|
|
// Existing histogram should get values converted from the above to: 6 3 0 3 0 0 2 4 5 0 1 (previous values with some new empty buckets in between)
|
|
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
|
h2.PositiveBuckets = []int64{7, -2, -4, 2, -2, -1, 2, 3, 0, -5, 0} // 7 5 1 3 1 0 2 5 5 0 0 (total 29)
|
|
|
|
|
|
|
|
|
|
histoApp, _ := app.(*HistoAppender)
|
|
|
|
|
posInterjections, negInterjections, ok := histoApp.Appendable(h2)
|
|
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ // New histogram that has a counter reset while new buckets were added before the first bucket and reset on first bucket.
|
|
|
|
|
// (to catch the edge case where the new bucket should be forwarded ahead until first old bucket at start)
|
|
|
|
|
h2 := h1
|
|
|
|
|
h2.PositiveSpans = []histogram.Span{
|
|
|
|
|
{Offset: -3, Length: 2},
|
|
|
|
|
{Offset: 1, Length: 2},
|
|
|
|
|
{Offset: 2, Length: 1},
|
|
|
|
|
{Offset: 3, Length: 2},
|
|
|
|
|
{Offset: 3, Length: 1},
|
|
|
|
|
{Offset: 1, Length: 1},
|
|
|
|
|
}
|
|
|
|
|
h2.Sum = 26
|
|
|
|
|
// Existing histogram should get values converted from the above to: 0, 0, 6, 3, 3, 2, 4, 5, 1
|
|
|
|
|
// so the new histogram should have new counts >= these per-bucket counts, e.g.:
|
|
|
|
|
h2.PositiveBuckets = []int64{1, 1, 3, -2, 0, -1, 2, 1, -4} // counts: 1, 2, 5, 3, 3, 2, 4, 5, 1 (total 26)
|
|
|
|
|
|
|
|
|
|
histoApp, _ := app.(*HistoAppender)
|
|
|
|
|
posInterjections, negInterjections, ok := histoApp.Appendable(h2)
|
|
|
|
|
require.Equal(t, 0, len(posInterjections))
|
|
|
|
|
require.Equal(t, 0, len(negInterjections))
|
|
|
|
|
require.False(t, ok) // Need to cut a new chunk.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|