Debian Codegraph @

sdk/metric/internal/aggregate/limit_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10
11 "go.opentelemetry.io/otel/attribute"
12 )
13
14 func TestLimiterAttributes(t *testing.T) {
15 var val struct{}
16 m := map[attribute.Distinct]*struct{}{alice.Equivalent(): &val}
17 t.Run("NoLimit", func(t *testing.T) {
18 l := newLimiter[struct{}](0)
19 assert.Equal(t, alice, l.Attributes(alice, m))
20 assert.Equal(t, bob, l.Attributes(bob, m))
21 })
22
23 t.Run("NotAtLimit/Exists", func(t *testing.T) {
24 l := newLimiter[struct{}](3)
25 assert.Equal(t, alice, l.Attributes(alice, m))
26 })
27
28 t.Run("NotAtLimit/DoesNotExist", func(t *testing.T) {
29 l := newLimiter[struct{}](3)
30 assert.Equal(t, bob, l.Attributes(bob, m))
31 })
32
33 t.Run("AtLimit/Exists", func(t *testing.T) {
34 l := newLimiter[struct{}](2)
35 assert.Equal(t, alice, l.Attributes(alice, m))
36 })
37
38 t.Run("AtLimit/DoesNotExist", func(t *testing.T) {
39 l := newLimiter[struct{}](2)
40 assert.Equal(t, overflowSet, l.Attributes(bob, m))
41 })
42 }
43
44 var limitedAttr attribute.Set
45
46 func BenchmarkLimiterAttributes(b *testing.B) {
47 var val struct{}
48 m := map[attribute.Distinct]*struct{}{alice.Equivalent(): &val}
49 l := newLimiter[struct{}](2)
50
51 b.ReportAllocs()
52 b.ResetTimer()
53
54 for n := 0; n < b.N; n++ {
55 limitedAttr = l.Attributes(alice, m)
56 limitedAttr = l.Attributes(bob, m)
57 }
58 }