Debian Codegraph @

pdata/pprofile/keyvalueandunit_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pprofile
5
6 import (
7 "errors"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12
13 "go.opentelemetry.io/collector/internal/testutil"
14 "go.opentelemetry.io/collector/pdata/pcommon"
15 )
16
17 func TestKeyValueAndUnitEqual(t *testing.T) {
18 for _, tt := range []struct {
19 name string
20 orig KeyValueAndUnit
21 dest KeyValueAndUnit
22 want bool
23 }{
24 {
25 name: "empty keyvalueandunit",
26 orig: NewKeyValueAndUnit(),
27 dest: NewKeyValueAndUnit(),
28 want: true,
29 },
30 {
31 name: "non-empty identical keyvalueandunit",
32 orig: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("test")),
33 dest: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("test")),
34 want: true,
35 },
36 {
37 name: "with different key index",
38 orig: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("test")),
39 dest: buildKeyValueAndUnit(2, 2, pcommon.NewValueStr("test")),
40 want: false,
41 },
42 {
43 name: "with different unit index",
44 orig: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("test")),
45 dest: buildKeyValueAndUnit(1, 3, pcommon.NewValueStr("test")),
46 want: false,
47 },
48 {
49 name: "with different value",
50 orig: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("test")),
51 dest: buildKeyValueAndUnit(1, 2, pcommon.NewValueStr("hello")),
52 want: false,
53 },
54 } {
55 t.Run(tt.name, func(t *testing.T) {
56 if tt.want {
57 assert.True(t, tt.orig.Equal(tt.dest))
58 } else {
59 assert.False(t, tt.orig.Equal(tt.dest))
60 }
61 })
62 }
63 }
64
65 func TestKeyValueAndUnitSwitchDictionary(t *testing.T) {
66 for _, tt := range []struct {
67 name string
68 keyValueAndUnit KeyValueAndUnit
69
70 src ProfilesDictionary
71 dst ProfilesDictionary
72
73 wantKeyValueAndUnit KeyValueAndUnit
74 wantDictionary ProfilesDictionary
75 wantErr error
76 }{
77 {
78 name: "with an empty key value and unit",
79 keyValueAndUnit: NewKeyValueAndUnit(),
80
81 src: NewProfilesDictionary(),
82 dst: NewProfilesDictionary(),
83
84 wantKeyValueAndUnit: NewKeyValueAndUnit(),
85 wantDictionary: NewProfilesDictionary(),
86 },
87 {
88 name: "with an existing key",
89 keyValueAndUnit: func() KeyValueAndUnit {
90 kvu := NewKeyValueAndUnit()
91 kvu.SetKeyStrindex(1)
92 return kvu
93 }(),
94
95 src: func() ProfilesDictionary {
96 d := NewProfilesDictionary()
97 d.StringTable().Append("", "test")
98 return d
99 }(),
100 dst: func() ProfilesDictionary {
101 d := NewProfilesDictionary()
102 d.StringTable().Append("", "foo")
103 return d
104 }(),
105
106 wantKeyValueAndUnit: func() KeyValueAndUnit {
107 kvu := NewKeyValueAndUnit()
108 kvu.SetKeyStrindex(2)
109 return kvu
110 }(),
111 wantDictionary: func() ProfilesDictionary {
112 d := NewProfilesDictionary()
113 d.StringTable().Append("", "foo", "test")
114 return d
115 }(),
116 },
117 {
118 name: "with a key index that does not match anything",
119 keyValueAndUnit: func() KeyValueAndUnit {
120 kvu := NewKeyValueAndUnit()
121 kvu.SetKeyStrindex(1)
122 return kvu
123 }(),
124
125 src: NewProfilesDictionary(),
126 dst: NewProfilesDictionary(),
127
128 wantKeyValueAndUnit: func() KeyValueAndUnit {
129 kvu := NewKeyValueAndUnit()
130 kvu.SetKeyStrindex(1)
131 return kvu
132 }(),
133 wantDictionary: NewProfilesDictionary(),
134 wantErr: errors.New("invalid key index 1"),
135 },
136 {
137 name: "with an existing unit",
138 keyValueAndUnit: func() KeyValueAndUnit {
139 kvu := NewKeyValueAndUnit()
140 kvu.SetUnitStrindex(1)
141 return kvu
142 }(),
143
144 src: func() ProfilesDictionary {
145 d := NewProfilesDictionary()
146 d.StringTable().Append("", "test")
147 return d
148 }(),
149 dst: func() ProfilesDictionary {
150 d := NewProfilesDictionary()
151 d.StringTable().Append("", "foo")
152 return d
153 }(),
154
155 wantKeyValueAndUnit: func() KeyValueAndUnit {
156 kvu := NewKeyValueAndUnit()
157 kvu.SetUnitStrindex(2)
158 return kvu
159 }(),
160 wantDictionary: func() ProfilesDictionary {
161 d := NewProfilesDictionary()
162 d.StringTable().Append("", "foo", "test")
163 return d
164 }(),
165 },
166 {
167 name: "with a unit index that does not match anything",
168 keyValueAndUnit: func() KeyValueAndUnit {
169 kvu := NewKeyValueAndUnit()
170 kvu.SetUnitStrindex(1)
171 return kvu
172 }(),
173
174 src: NewProfilesDictionary(),
175 dst: NewProfilesDictionary(),
176
177 wantKeyValueAndUnit: func() KeyValueAndUnit {
178 kvu := NewKeyValueAndUnit()
179 kvu.SetUnitStrindex(1)
180 return kvu
181 }(),
182 wantDictionary: NewProfilesDictionary(),
183 wantErr: errors.New("invalid unit index 1"),
184 },
185 } {
186 t.Run(tt.name, func(t *testing.T) {
187 kvu := tt.keyValueAndUnit
188 dst := tt.dst
189 err := kvu.switchDictionary(tt.src, dst)
190
191 if tt.wantErr == nil {
192 require.NoError(t, err)
193 } else {
194 require.Equal(t, tt.wantErr, err)
195 }
196
197 assert.Equal(t, tt.wantKeyValueAndUnit, kvu)
198 assert.Equal(t, tt.wantDictionary, dst)
199 })
200 }
201 }
202
203 func BenchmarkKeyValueAndUnitSwitchDictionary(b *testing.B) {
204 testutil.SkipMemoryBench(b)
205
206 kvu := NewKeyValueAndUnit()
207 kvu.SetKeyStrindex(1)
208
209 src := NewProfilesDictionary()
210 src.StringTable().Append("", "test")
211
212 b.ReportAllocs()
213
214 for b.Loop() {
215 b.StopTimer()
216 dst := NewProfilesDictionary()
217 b.StartTimer()
218
219 _ = kvu.switchDictionary(src, dst)
220 }
221 }
222
223 func buildKeyValueAndUnit(keyIdx, unitIdx int32, val pcommon.Value) KeyValueAndUnit {
224 kvu := NewKeyValueAndUnit()
225 kvu.SetKeyStrindex(keyIdx)
226 kvu.SetUnitStrindex(unitIdx)
227 val.CopyTo(kvu.Value())
228 return kvu
229 }