2019-10-29 22:27:22 +02:00
|
|
|
// Copyright 2019, OpenTelemetry 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 ddsketch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-05 00:24:01 +02:00
|
|
|
"fmt"
|
2019-10-29 22:27:22 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/test"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
const count = 1000
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
type updateTest struct {
|
|
|
|
absolute bool
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func (ut *updateTest) run(t *testing.T, profile test.Profile) {
|
2019-10-29 22:27:22 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.MeasureKind, profile.NumberKind, !ut.absolute)
|
|
|
|
agg := New(NewDefaultConfig(), descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
all := test.NewNumbers(profile.NumberKind)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
all.Append(x)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, x, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
if !ut.absolute {
|
|
|
|
y := profile.Random(-1)
|
|
|
|
all.Append(y)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, y, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
2019-11-05 00:24:01 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg.Checkpoint(ctx, descriptor)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
all.Sort()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
sum, err := agg.Sum()
|
2019-12-09 23:03:11 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
allSum := all.Sum()
|
2019-11-05 00:24:01 +02:00
|
|
|
require.InDelta(t,
|
2019-12-09 23:03:11 +02:00
|
|
|
(&allSum).CoerceToFloat64(profile.NumberKind),
|
2019-11-15 23:01:20 +02:00
|
|
|
sum.CoerceToFloat64(profile.NumberKind),
|
2019-11-05 00:24:01 +02:00
|
|
|
1,
|
|
|
|
"Same sum - absolute")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
count, err := agg.Count()
|
|
|
|
require.Equal(t, all.Count(), count, "Same count - absolute")
|
|
|
|
require.Nil(t, err)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
max, err := agg.Max()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t,
|
|
|
|
all.Max(),
|
|
|
|
max,
|
|
|
|
"Same max - absolute")
|
|
|
|
|
|
|
|
median, err := agg.Quantile(0.5)
|
|
|
|
require.Nil(t, err)
|
2019-12-09 23:03:11 +02:00
|
|
|
allMedian := all.Median()
|
2019-11-05 00:24:01 +02:00
|
|
|
require.InDelta(t,
|
2019-12-09 23:03:11 +02:00
|
|
|
(&allMedian).CoerceToFloat64(profile.NumberKind),
|
2019-11-05 00:24:01 +02:00
|
|
|
median.CoerceToFloat64(profile.NumberKind),
|
|
|
|
10,
|
|
|
|
"Same median - absolute")
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func TestDDSketchUpdate(t *testing.T) {
|
|
|
|
// Test absolute and non-absolute
|
|
|
|
for _, absolute := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprint("Absolute=", absolute), func(t *testing.T) {
|
|
|
|
ut := updateTest{
|
|
|
|
absolute: absolute,
|
|
|
|
}
|
|
|
|
// Test integer and floating point
|
|
|
|
test.RunProfiles(t, ut.run)
|
|
|
|
})
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
type mergeTest struct {
|
|
|
|
absolute bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mt *mergeTest) run(t *testing.T, profile test.Profile) {
|
2019-10-31 07:15:27 +02:00
|
|
|
ctx := context.Background()
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.MeasureKind, profile.NumberKind, !mt.absolute)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg1 := New(NewDefaultConfig(), descriptor)
|
|
|
|
agg2 := New(NewDefaultConfig(), descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
all := test.NewNumbers(profile.NumberKind)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
all.Append(x)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg1, x, descriptor)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
if !mt.absolute {
|
|
|
|
y := profile.Random(-1)
|
|
|
|
all.Append(y)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg1, y, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
2019-11-05 00:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
all.Append(x)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg2, x, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
if !mt.absolute {
|
|
|
|
y := profile.Random(-1)
|
|
|
|
all.Append(y)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg2, y, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
2019-11-05 00:24:01 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg1.Checkpoint(ctx, descriptor)
|
|
|
|
agg2.Checkpoint(ctx, descriptor)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedMerge(t, agg1, agg2, descriptor)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
all.Sort()
|
|
|
|
|
2019-12-09 23:03:11 +02:00
|
|
|
aggSum, err := agg1.Sum()
|
|
|
|
require.Nil(t, err)
|
|
|
|
allSum := all.Sum()
|
2019-11-05 00:24:01 +02:00
|
|
|
require.InDelta(t,
|
2019-12-09 23:03:11 +02:00
|
|
|
(&allSum).CoerceToFloat64(profile.NumberKind),
|
|
|
|
aggSum.CoerceToFloat64(profile.NumberKind),
|
2019-11-05 00:24:01 +02:00
|
|
|
1,
|
|
|
|
"Same sum - absolute")
|
2019-11-15 23:01:20 +02:00
|
|
|
|
|
|
|
count, err := agg1.Count()
|
|
|
|
require.Equal(t, all.Count(), count, "Same count - absolute")
|
|
|
|
require.Nil(t, err)
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
max, err := agg1.Max()
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t,
|
|
|
|
all.Max(),
|
|
|
|
max,
|
|
|
|
"Same max - absolute")
|
|
|
|
|
|
|
|
median, err := agg1.Quantile(0.5)
|
|
|
|
require.Nil(t, err)
|
2019-12-09 23:03:11 +02:00
|
|
|
allMedian := all.Median()
|
2019-11-05 00:24:01 +02:00
|
|
|
require.InDelta(t,
|
2019-12-09 23:03:11 +02:00
|
|
|
(&allMedian).CoerceToFloat64(profile.NumberKind),
|
2019-11-05 00:24:01 +02:00
|
|
|
median.CoerceToFloat64(profile.NumberKind),
|
|
|
|
10,
|
|
|
|
"Same median - absolute")
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-05 00:24:01 +02:00
|
|
|
func TestDDSketchMerge(t *testing.T) {
|
|
|
|
// Test absolute and non-absolute
|
|
|
|
for _, absolute := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprint("Absolute=", absolute), func(t *testing.T) {
|
|
|
|
mt := mergeTest{
|
|
|
|
absolute: absolute,
|
|
|
|
}
|
|
|
|
// Test integer and floating point
|
|
|
|
test.RunProfiles(t, mt.run)
|
|
|
|
})
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|