2020-03-25 23:47:17 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2020-01-21 19:15:09 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
package histogram_test
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-05-21 19:29:03 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
|
2020-01-21 19:15:09 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
const count = 100
|
|
|
|
|
|
|
|
type policy struct {
|
|
|
|
name string
|
|
|
|
absolute bool
|
|
|
|
sign func() int
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
positiveOnly = policy{
|
|
|
|
name: "absolute",
|
|
|
|
absolute: true,
|
|
|
|
sign: func() int { return +1 },
|
|
|
|
}
|
|
|
|
negativeOnly = policy{
|
|
|
|
name: "negative",
|
|
|
|
absolute: false,
|
|
|
|
sign: func() int { return -1 },
|
|
|
|
}
|
|
|
|
positiveAndNegative = policy{
|
|
|
|
name: "positiveAndNegative",
|
|
|
|
absolute: false,
|
|
|
|
sign: func() int {
|
|
|
|
if rand.Uint32() > math.MaxUint32/2 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
boundaries = []float64{500, 250, 750}
|
2020-01-21 19:15:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHistogramAbsolute(t *testing.T) {
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
2020-05-21 19:29:03 +02:00
|
|
|
testHistogram(t, profile, positiveOnly)
|
2020-01-21 19:15:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramNegativeOnly(t *testing.T) {
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
2020-05-21 19:29:03 +02:00
|
|
|
testHistogram(t, profile, negativeOnly)
|
2020-01-21 19:15:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramPositiveAndNegative(t *testing.T) {
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
2020-05-21 19:29:03 +02:00
|
|
|
testHistogram(t, profile, positiveAndNegative)
|
2020-01-21 19:15:09 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validates count, sum and buckets for a given profile and policy
|
2020-05-21 19:29:03 +02:00
|
|
|
func testHistogram(t *testing.T, profile test.Profile, policy policy) {
|
2020-01-21 19:15:09 +02:00
|
|
|
ctx := context.Background()
|
2020-05-16 07:11:12 +02:00
|
|
|
descriptor := test.NewAggregatorTest(metric.ValueRecorderKind, profile.NumberKind)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
agg := histogram.New(descriptor, boundaries)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
all := test.NewNumbers(profile.NumberKind)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(policy.sign())
|
|
|
|
all.Append(x)
|
|
|
|
test.CheckedUpdate(t, agg, x, descriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
agg.Checkpoint(ctx, descriptor)
|
|
|
|
|
|
|
|
all.Sort()
|
|
|
|
|
|
|
|
asum, err := agg.Sum()
|
|
|
|
sum := all.Sum()
|
|
|
|
require.InEpsilon(t,
|
|
|
|
sum.CoerceToFloat64(profile.NumberKind),
|
|
|
|
asum.CoerceToFloat64(profile.NumberKind),
|
|
|
|
0.000000001,
|
|
|
|
"Same sum - "+policy.name)
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
count, err := agg.Count()
|
|
|
|
require.Equal(t, all.Count(), count, "Same count -"+policy.name)
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buckets, err := agg.Histogram()
|
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
require.Equal(t, len(buckets.Counts), len(boundaries)+1, "There should be b + 1 counts, where b is the number of boundaries")
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
counts := calcBuckets(all.Points(), profile)
|
|
|
|
for i, v := range counts {
|
2020-05-21 19:29:03 +02:00
|
|
|
bCount := uint64(buckets.Counts[i])
|
|
|
|
require.Equal(t, v, bCount, "Wrong bucket #%d count: %v != %v", i, counts, buckets.Counts)
|
2020-01-21 19:15:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 18:44:33 +02:00
|
|
|
func TestHistogramInitial(t *testing.T) {
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
descriptor := test.NewAggregatorTest(metric.ValueRecorderKind, profile.NumberKind)
|
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
agg := histogram.New(descriptor, boundaries)
|
2020-05-18 18:44:33 +02:00
|
|
|
buckets, err := agg.Histogram()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
2020-05-21 19:29:03 +02:00
|
|
|
require.Equal(t, len(buckets.Counts), len(boundaries)+1)
|
|
|
|
require.Equal(t, len(buckets.Boundaries), len(boundaries))
|
2020-05-18 18:44:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:15:09 +02:00
|
|
|
func TestHistogramMerge(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
2020-05-16 07:11:12 +02:00
|
|
|
descriptor := test.NewAggregatorTest(metric.ValueRecorderKind, profile.NumberKind)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
agg1 := histogram.New(descriptor, boundaries)
|
|
|
|
agg2 := histogram.New(descriptor, boundaries)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
all := test.NewNumbers(profile.NumberKind)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
all.Append(x)
|
|
|
|
test.CheckedUpdate(t, agg1, x, descriptor)
|
|
|
|
}
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
all.Append(x)
|
|
|
|
test.CheckedUpdate(t, agg2, x, descriptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
agg1.Checkpoint(ctx, descriptor)
|
|
|
|
agg2.Checkpoint(ctx, descriptor)
|
|
|
|
|
|
|
|
test.CheckedMerge(t, agg1, agg2, descriptor)
|
|
|
|
|
|
|
|
all.Sort()
|
|
|
|
|
|
|
|
asum, err := agg1.Sum()
|
|
|
|
sum := all.Sum()
|
|
|
|
require.InEpsilon(t,
|
|
|
|
sum.CoerceToFloat64(profile.NumberKind),
|
|
|
|
asum.CoerceToFloat64(profile.NumberKind),
|
|
|
|
0.000000001,
|
|
|
|
"Same sum - absolute")
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
count, err := agg1.Count()
|
|
|
|
require.Equal(t, all.Count(), count, "Same count - absolute")
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buckets, err := agg1.Histogram()
|
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
require.Equal(t, len(buckets.Counts), len(boundaries)+1, "There should be b + 1 counts, where b is the number of boundaries")
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
counts := calcBuckets(all.Points(), profile)
|
|
|
|
for i, v := range counts {
|
2020-05-21 19:29:03 +02:00
|
|
|
bCount := uint64(buckets.Counts[i])
|
|
|
|
require.Equal(t, v, bCount, "Wrong bucket #%d count: %v != %v", i, counts, buckets.Counts)
|
2020-01-21 19:15:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHistogramNotSet(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
2020-05-16 07:11:12 +02:00
|
|
|
descriptor := test.NewAggregatorTest(metric.ValueRecorderKind, profile.NumberKind)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
agg := histogram.New(descriptor, boundaries)
|
2020-01-21 19:15:09 +02:00
|
|
|
agg.Checkpoint(ctx, descriptor)
|
|
|
|
|
|
|
|
asum, err := agg.Sum()
|
2020-05-11 08:44:42 +02:00
|
|
|
require.Equal(t, metric.Number(0), asum, "Empty checkpoint sum = 0")
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
|
|
|
count, err := agg.Count()
|
|
|
|
require.Equal(t, int64(0), count, "Empty checkpoint count = 0")
|
2020-05-21 19:29:03 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
buckets, err := agg.Histogram()
|
|
|
|
require.NoError(t, err)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
require.Equal(t, len(buckets.Counts), len(boundaries)+1, "There should be b + 1 counts, where b is the number of boundaries")
|
|
|
|
for i, bCount := range buckets.Counts {
|
|
|
|
require.Equal(t, uint64(0), uint64(bCount), "Bucket #%d must have 0 observed values", i)
|
2020-01-21 19:15:09 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-11 08:44:42 +02:00
|
|
|
func calcBuckets(points []metric.Number, profile test.Profile) []uint64 {
|
2020-05-21 19:29:03 +02:00
|
|
|
sortedBoundaries := make([]float64, len(boundaries))
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
copy(sortedBoundaries, boundaries)
|
|
|
|
sort.Float64s(sortedBoundaries)
|
2020-01-21 19:15:09 +02:00
|
|
|
|
2020-05-21 19:29:03 +02:00
|
|
|
counts := make([]uint64, len(sortedBoundaries)+1)
|
2020-01-21 19:15:09 +02:00
|
|
|
idx := 0
|
|
|
|
for _, p := range points {
|
2020-05-21 20:09:10 +02:00
|
|
|
for idx < len(sortedBoundaries) && p.CoerceToFloat64(profile.NumberKind) >= sortedBoundaries[idx] {
|
2020-01-21 19:15:09 +02:00
|
|
|
idx++
|
|
|
|
}
|
|
|
|
counts[idx]++
|
|
|
|
}
|
|
|
|
|
|
|
|
return counts
|
|
|
|
}
|