File: limit_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (56 lines) | stat: -rw-r--r-- 1,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package aggregate // import "go.opentelemetry.io/otel/sdk/metric/internal/aggregate"

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"go.opentelemetry.io/otel/attribute"
)

func TestLimiterAttributes(t *testing.T) {
	m := map[attribute.Distinct]struct{}{alice.Equivalent(): {}}
	t.Run("NoLimit", func(t *testing.T) {
		l := newLimiter[struct{}](0)
		assert.Equal(t, alice, l.Attributes(alice, m))
		assert.Equal(t, bob, l.Attributes(bob, m))
	})

	t.Run("NotAtLimit/Exists", func(t *testing.T) {
		l := newLimiter[struct{}](3)
		assert.Equal(t, alice, l.Attributes(alice, m))
	})

	t.Run("NotAtLimit/DoesNotExist", func(t *testing.T) {
		l := newLimiter[struct{}](3)
		assert.Equal(t, bob, l.Attributes(bob, m))
	})

	t.Run("AtLimit/Exists", func(t *testing.T) {
		l := newLimiter[struct{}](2)
		assert.Equal(t, alice, l.Attributes(alice, m))
	})

	t.Run("AtLimit/DoesNotExist", func(t *testing.T) {
		l := newLimiter[struct{}](2)
		assert.Equal(t, overflowSet, l.Attributes(bob, m))
	})
}

var limitedAttr attribute.Set

func BenchmarkLimiterAttributes(b *testing.B) {
	m := map[attribute.Distinct]struct{}{alice.Equivalent(): {}}
	l := newLimiter[struct{}](2)

	b.ReportAllocs()
	b.ResetTimer()

	for n := 0; n < b.N; n++ {
		limitedAttr = l.Attributes(alice, m)
		limitedAttr = l.Attributes(bob, m)
	}
}