prometheus/util/convertnhcb/convertnhcb_test.go
György Krajcsovits eafe72a0d0
Some checks failed
CI / Go tests (push) Has been cancelled
CI / More Go tests (push) Has been cancelled
CI / Go tests with previous Go version (push) Has been cancelled
CI / UI tests (push) Has been cancelled
CI / Go tests on Windows (push) Has been cancelled
CI / Mixins tests (push) Has been cancelled
CI / Build Prometheus for common architectures (0) (push) Has been cancelled
CI / Build Prometheus for common architectures (1) (push) Has been cancelled
CI / Build Prometheus for common architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (0) (push) Has been cancelled
CI / Build Prometheus for all architectures (1) (push) Has been cancelled
CI / Build Prometheus for all architectures (10) (push) Has been cancelled
CI / Build Prometheus for all architectures (11) (push) Has been cancelled
CI / Build Prometheus for all architectures (2) (push) Has been cancelled
CI / Build Prometheus for all architectures (3) (push) Has been cancelled
CI / Build Prometheus for all architectures (4) (push) Has been cancelled
CI / Build Prometheus for all architectures (5) (push) Has been cancelled
CI / Build Prometheus for all architectures (6) (push) Has been cancelled
CI / Build Prometheus for all architectures (7) (push) Has been cancelled
CI / Build Prometheus for all architectures (8) (push) Has been cancelled
CI / Build Prometheus for all architectures (9) (push) Has been cancelled
CI / Check generated parser (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
CI / fuzzing (push) Has been cancelled
CI / codeql (push) Has been cancelled
CI / Report status of build Prometheus for all architectures (push) Has been cancelled
CI / Publish main branch artifacts (push) Has been cancelled
CI / Publish release artefacts (push) Has been cancelled
CI / Publish UI on npm Registry (push) Has been cancelled
perf(nhcb): optimize away most allocations in convertnhcb
In general aim for the happy case when the exposer lists the buckets
in ascending order.

Use Compact(2) to compact the result of nhcb convert.

This is more in line with how client_golang optimizes spans vs
buckets.
aef8aedb4b/prometheus/histogram.go (L1485)

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
2024-10-28 08:34:54 +01:00

190 lines
5 KiB
Go

// Copyright 2024 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 convertnhcb
import (
"math"
"testing"
"github.com/stretchr/testify/require"
"github.com/prometheus/prometheus/model/histogram"
)
func TestNHCBConvert(t *testing.T) {
tests := map[string]struct {
setup func() *TempHistogram
expectedErr error
expectedH *histogram.Histogram
expectedFH *histogram.FloatHistogram
}{
"empty": {
setup: func() *TempHistogram {
h := NewTempHistogram()
return &h
},
expectedH: &histogram.Histogram{
Schema: histogram.CustomBucketsSchema,
PositiveSpans: []histogram.Span{},
PositiveBuckets: []int64{},
},
},
"sum only": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetSum(1000.25)
return &h
},
expectedH: &histogram.Histogram{
Schema: histogram.CustomBucketsSchema,
Sum: 1000.25,
PositiveSpans: []histogram.Span{},
PositiveBuckets: []int64{},
},
},
"single integer bucket": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetSum(1000.25)
h.SetBucketCount(0.5, 1000)
return &h
},
expectedH: &histogram.Histogram{
Schema: histogram.CustomBucketsSchema,
Count: 1000,
Sum: 1000.25,
PositiveSpans: []histogram.Span{{Length: 1}},
PositiveBuckets: []int64{1000},
CustomValues: []float64{0.5},
},
},
"single float bucket": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetSum(1000.25)
h.SetBucketCount(0.5, 1337.42)
return &h
},
expectedFH: &histogram.FloatHistogram{
Schema: histogram.CustomBucketsSchema,
Count: 1337.42,
Sum: 1000.25,
PositiveSpans: []histogram.Span{{Length: 1}},
PositiveBuckets: []float64{1337.42},
CustomValues: []float64{0.5},
},
},
"happy case integer bucket": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetCount(1000)
h.SetSum(1000.25)
h.SetBucketCount(0.5, 50)
h.SetBucketCount(1.0, 950)
h.SetBucketCount(math.Inf(1), 1000)
return &h
},
expectedH: &histogram.Histogram{
Schema: histogram.CustomBucketsSchema,
Count: 1000,
Sum: 1000.25,
PositiveSpans: []histogram.Span{{Length: 3}},
PositiveBuckets: []int64{50, 850, -850},
CustomValues: []float64{0.5, 1.0},
},
},
"happy case float bucket": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetCount(1000)
h.SetSum(1000.25)
h.SetBucketCount(0.5, 50)
h.SetBucketCount(1.0, 950.5)
h.SetBucketCount(math.Inf(1), 1000)
return &h
},
expectedFH: &histogram.FloatHistogram{
Schema: histogram.CustomBucketsSchema,
Count: 1000,
Sum: 1000.25,
PositiveSpans: []histogram.Span{{Length: 3}},
PositiveBuckets: []float64{50, 900.5, 49.5},
CustomValues: []float64{0.5, 1.0},
},
},
"non cumulative bucket": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetCount(1000)
h.SetSum(1000.25)
h.SetBucketCount(0.5, 50)
h.SetBucketCount(1.0, 950)
h.SetBucketCount(math.Inf(1), 900)
return &h
},
expectedErr: errCountNotCumulative,
},
"negative count": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetCount(-1000)
h.SetSum(1000.25)
h.SetBucketCount(0.5, 50)
h.SetBucketCount(1.0, 950)
h.SetBucketCount(math.Inf(1), 900)
return &h
},
expectedErr: errNegativeCount,
},
"mixed order": {
setup: func() *TempHistogram {
h := NewTempHistogram()
h.SetBucketCount(0.5, 50)
h.SetBucketCount(math.Inf(1), 1000)
h.SetBucketCount(1.0, 950)
h.SetCount(1000)
h.SetSum(1000.25)
return &h
},
expectedH: &histogram.Histogram{
Schema: histogram.CustomBucketsSchema,
Count: 1000,
Sum: 1000.25,
PositiveSpans: []histogram.Span{{Length: 3}},
PositiveBuckets: []int64{50, 850, -850},
CustomValues: []float64{0.5, 1.0},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
th := test.setup()
h, fh, err := th.Convert()
if test.expectedErr != nil {
require.ErrorIs(t, err, test.expectedErr)
return
}
require.Equal(t, test.expectedH, h)
if h != nil {
require.NoError(t, h.Validate())
}
require.Equal(t, test.expectedFH, fh)
if fh != nil {
require.NoError(t, fh.Validate())
}
})
}
}