Skip to content

Commit

Permalink
Add cumulative testing
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed May 7, 2024
1 parent 3d77ec2 commit 4f7d169
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions sdk/metric/internal/aggregate/lastvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestLastValue(t *testing.T) {

t.Run("Int64/DeltaLastValue", testDeltaLastValue[int64]())
t.Run("Float64/DeltaLastValue", testDeltaLastValue[float64]())

t.Run("Int64/CumulativeLastValue", testCumulativeLastValue[int64]())
t.Run("Float64/CumulativeLastValue", testCumulativeLastValue[float64]())
}

func testDeltaLastValue[N int64 | float64]() func(*testing.T) {
Expand Down Expand Up @@ -121,6 +124,128 @@ func testDeltaLastValue[N int64 | float64]() func(*testing.T) {
})
}

func testCumulativeLastValue[N int64 | float64]() func(*testing.T) {
in, out := Builder[N]{
Temporality: metricdata.CumulativeTemporality,
Filter: attrFltr,
AggregationLimit: 3,
}.LastValue()
ctx := context.Background()
return test[N](in, out, []teststep[N]{
{
// Empty output if nothing is measured.
input: []arg[N]{},
expect: output{n: 0, agg: metricdata.Gauge[N]{}},
}, {
input: []arg[N]{
{ctx, 1, alice},
{ctx, -1, bob},
{ctx, 1, fltrAlice},
{ctx, 2, alice},
{ctx, -10, bob},
},
expect: output{
n: 2,
agg: metricdata.Gauge[N]{
DataPoints: []metricdata.DataPoint[N]{
{
Attributes: fltrAlice,
StartTime: staticTime,
Time: staticTime,
Value: 2,
},
{
Attributes: fltrBob,
StartTime: staticTime,
Time: staticTime,
Value: -10,
},
},
},
},
}, {
// Cumulative temporality means no resets.
input: []arg[N]{},
expect: output{
n: 2,
agg: metricdata.Gauge[N]{
DataPoints: []metricdata.DataPoint[N]{
{
Attributes: fltrAlice,
StartTime: staticTime,
Time: staticTime,
Value: 2,
},
{
Attributes: fltrBob,
StartTime: staticTime,
Time: staticTime,
Value: -10,
},
},
},
},
}, {
input: []arg[N]{
{ctx, 10, alice},
{ctx, 3, bob},
},
expect: output{
n: 2,
agg: metricdata.Gauge[N]{
DataPoints: []metricdata.DataPoint[N]{
{
Attributes: fltrAlice,
StartTime: staticTime,
Time: staticTime,
Value: 10,
},
{
Attributes: fltrBob,
StartTime: staticTime,
Time: staticTime,
Value: 3,
},
},
},
},
}, {
input: []arg[N]{
{ctx, 1, alice},
{ctx, 1, bob},
// These will exceed cardinality limit.
{ctx, 1, carol},
{ctx, 1, dave},
},
expect: output{
n: 3,
agg: metricdata.Gauge[N]{
DataPoints: []metricdata.DataPoint[N]{
{
Attributes: fltrAlice,
StartTime: staticTime,
Time: staticTime,
Value: 1,
},
{
Attributes: fltrBob,
StartTime: staticTime,
Time: staticTime,
Value: 1,
},
{
Attributes: overflowSet,
StartTime: staticTime,
Time: staticTime,
Value: 1,
},
},
},
},
},
})
}

func BenchmarkLastValue(b *testing.B) {
b.Run("Int64", benchmarkAggregate(Builder[int64]{}.PrecomputedLastValue))
b.Run("Float64", benchmarkAggregate(Builder[float64]{}.PrecomputedLastValue))
Expand Down

0 comments on commit 4f7d169

Please sign in to comment.