You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prometheus/model/histogram/histogram_test.go

166 lines
4.1 KiB

Style cleanup of all the changes in sparsehistogram so far A lot of this code was hacked together, literally during a hackathon. This commit intends not to change the code substantially, but just make the code obey the usual style practices. A (possibly incomplete) list of areas: * Generally address linter warnings. * The `pgk` directory is deprecated as per dev-summit. No new packages should be added to it. I moved the new `pkg/histogram` package to `model` anticipating what's proposed in #9478. * Make the naming of the Sparse Histogram more consistent. Including abbreviations, there were just too many names for it: SparseHistogram, Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in general. Only add "Sparse" if it is needed to avoid confusion with conventional Histograms (which is rare because the TSDB really has no notion of conventional Histograms). Use abbreviations only in local scope, and then really abbreviate (not just removing three out of seven letters like in "Histo"). This is in the spirit of https://github.com/golang/go/wiki/CodeReviewComments#variable-names * Several other minor name changes. * A lot of formatting of doc comments. For one, following https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences , but also layout question, anticipating how things will look like when rendered by `godoc` (even where `godoc` doesn't render them right now because they are for unexported types or not a doc comment at all but just a normal code comment - consistency is queen!). * Re-enabled `TestQueryLog` and `TestEndopints` (they pass now, leaving them disabled was presumably an oversight). * Bucket iterator for histogram.Histogram is now created with a method. * HistogramChunk.iterator now allows iterator recycling. (I think @dieterbe only commented it out because he was confused by the question in the comment.) * HistogramAppender.Append panics now because we decided to treat staleness marker differently. Signed-off-by: beorn7 <beorn@grafana.com>
3 years ago
// 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 (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestCumulativeBucketIterator(t *testing.T) {
cases := []struct {
histogram Histogram
expectedCumulativeBuckets []Bucket
}{
{
histogram: Histogram{
Schema: 0,
PositiveSpans: []Span{
{Offset: 0, Length: 2},
{Offset: 1, Length: 2},
},
PositiveBuckets: []int64{1, 1, -1, 0},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 1, Count: 1},
{Upper: 2, Count: 3},
{Upper: 4, Count: 3},
{Upper: 8, Count: 4},
{Upper: 16, Count: 5},
},
},
{
histogram: Histogram{
Schema: 0,
PositiveSpans: []Span{
{Offset: 0, Length: 5},
{Offset: 1, Length: 1},
},
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 1, Count: 1},
{Upper: 2, Count: 4},
{Upper: 4, Count: 5},
{Upper: 8, Count: 7},
{Upper: 16, Count: 8},
{Upper: 32, Count: 8},
{Upper: 64, Count: 9},
},
},
{
histogram: Histogram{
Schema: 0,
PositiveSpans: []Span{
{Offset: 0, Length: 7},
},
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0, 0},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 1, Count: 1},
{Upper: 2, Count: 4},
{Upper: 4, Count: 5},
{Upper: 8, Count: 7},
{Upper: 16, Count: 8},
{Upper: 32, Count: 9},
{Upper: 64, Count: 10},
},
},
{
histogram: Histogram{
Schema: 3,
PositiveSpans: []Span{
{Offset: -5, Length: 2}, // -5 -4
{Offset: 2, Length: 3}, // -1 0 1
{Offset: 2, Length: 2}, // 4 5
},
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0, 3},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 0.6484197773255048, Count: 1}, // -5
{Upper: 0.7071067811865475, Count: 4}, // -4
{Upper: 0.7711054127039704, Count: 4}, // -3
{Upper: 0.8408964152537144, Count: 4}, // -2
{Upper: 0.9170040432046711, Count: 5}, // -1
{Upper: 1, Count: 7}, // 1
{Upper: 1.0905077326652577, Count: 8}, // 0
{Upper: 1.189207115002721, Count: 8}, // 1
{Upper: 1.2968395546510096, Count: 8}, // 2
{Upper: 1.414213562373095, Count: 9}, // 3
{Upper: 1.5422108254079407, Count: 13}, // 4
},
},
{
histogram: Histogram{
Schema: -2,
PositiveSpans: []Span{
{Offset: -2, Length: 4}, // -2 -1 0 1
{Offset: 2, Length: 2}, // 4 5
},
PositiveBuckets: []int64{1, 2, -2, 1, -1, 0},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 0.00390625, Count: 1}, // -2
{Upper: 0.0625, Count: 4}, // -1
{Upper: 1, Count: 5}, // 0
{Upper: 16, Count: 7}, // 1
{Upper: 256, Count: 7}, // 2
{Upper: 4096, Count: 7}, // 3
{Upper: 65536, Count: 8}, // 4
{Upper: 1048576, Count: 9}, // 5
},
},
{
histogram: Histogram{
Schema: -1,
PositiveSpans: []Span{
{Offset: -2, Length: 5}, // -2 -1 0 1 2
},
PositiveBuckets: []int64{1, 2, -2, 1, -1},
},
expectedCumulativeBuckets: []Bucket{
{Upper: 0.0625, Count: 1}, // -2
{Upper: 0.25, Count: 4}, // -1
{Upper: 1, Count: 5}, // 0
{Upper: 4, Count: 7}, // 1
{Upper: 16, Count: 8}, // 2
},
},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
it := c.histogram.CumulativeBucketIterator()
actualBuckets := make([]Bucket, 0, len(c.expectedCumulativeBuckets))
for it.Next() {
actualBuckets = append(actualBuckets, it.At())
}
require.NoError(t, it.Err())
require.Equal(t, c.expectedCumulativeBuckets, actualBuckets)
})
}
}