Debian Codegraph @

pdata/pprofile/sample_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 TestSampleSwitchDictionary(t *testing.T) {
18 for _, tt := range []struct {
19 name string
20 sample Sample
21
22 src ProfilesDictionary
23 dst ProfilesDictionary
24
25 wantSample Sample
26 wantDictionary ProfilesDictionary
27 wantErr error
28 }{
29 {
30 name: "with an empty sample",
31 sample: NewSample(),
32
33 src: NewProfilesDictionary(),
34 dst: NewProfilesDictionary(),
35
36 wantSample: NewSample(),
37 wantDictionary: NewProfilesDictionary(),
38 },
39 {
40 name: "with an existing attribute",
41 sample: func() Sample {
42 s := NewSample()
43 s.AttributeIndices().Append(1)
44 return s
45 }(),
46
47 src: func() ProfilesDictionary {
48 d := NewProfilesDictionary()
49 d.StringTable().Append("", "test")
50
51 d.AttributeTable().AppendEmpty()
52 a := d.AttributeTable().AppendEmpty()
53 a.SetKeyStrindex(1)
54
55 return d
56 }(),
57 dst: func() ProfilesDictionary {
58 d := NewProfilesDictionary()
59 d.StringTable().Append("", "foo")
60
61 d.AttributeTable().AppendEmpty()
62 d.AttributeTable().AppendEmpty()
63 return d
64 }(),
65
66 wantSample: func() Sample {
67 s := NewSample()
68 s.AttributeIndices().Append(2)
69 return s
70 }(),
71 wantDictionary: func() ProfilesDictionary {
72 d := NewProfilesDictionary()
73 d.StringTable().Append("", "foo", "test")
74
75 d.AttributeTable().AppendEmpty()
76 d.AttributeTable().AppendEmpty()
77 a := d.AttributeTable().AppendEmpty()
78 a.SetKeyStrindex(2)
79 return d
80 }(),
81 },
82 {
83 name: "with an attribute index that does not match anything",
84 sample: func() Sample {
85 s := NewSample()
86 s.AttributeIndices().Append(1)
87 return s
88 }(),
89
90 src: NewProfilesDictionary(),
91 dst: NewProfilesDictionary(),
92
93 wantSample: func() Sample {
94 s := NewSample()
95 s.AttributeIndices().Append(1)
96 return s
97 }(),
98 wantDictionary: NewProfilesDictionary(),
99 wantErr: errors.New("invalid attribute index 1"),
100 },
101 {
102 name: "with an existing link",
103 sample: func() Sample {
104 s := NewSample()
105 s.SetLinkIndex(1)
106 return s
107 }(),
108
109 src: func() ProfilesDictionary {
110 d := NewProfilesDictionary()
111 d.LinkTable().AppendEmpty()
112 l := d.LinkTable().AppendEmpty()
113 l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
114 return d
115 }(),
116 dst: func() ProfilesDictionary {
117 d := NewProfilesDictionary()
118 d.LinkTable().AppendEmpty()
119 d.LinkTable().AppendEmpty()
120 return d
121 }(),
122
123 wantSample: func() Sample {
124 s := NewSample()
125 s.SetLinkIndex(2)
126 return s
127 }(),
128 wantDictionary: func() ProfilesDictionary {
129 d := NewProfilesDictionary()
130 d.LinkTable().AppendEmpty()
131 d.LinkTable().AppendEmpty()
132 l := d.LinkTable().AppendEmpty()
133 l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
134 return d
135 }(),
136 },
137 {
138 name: "with a link index that does not match anything",
139 sample: func() Sample {
140 s := NewSample()
141 s.SetLinkIndex(1)
142 return s
143 }(),
144
145 src: NewProfilesDictionary(),
146 dst: NewProfilesDictionary(),
147
148 wantSample: func() Sample {
149 s := NewSample()
150 s.SetLinkIndex(1)
151 return s
152 }(),
153 wantDictionary: NewProfilesDictionary(),
154 wantErr: errors.New("invalid link index 1"),
155 },
156 {
157 name: "with an existing stack",
158 sample: func() Sample {
159 s := NewSample()
160 s.SetStackIndex(1)
161 return s
162 }(),
163
164 src: func() ProfilesDictionary {
165 d := NewProfilesDictionary()
166 d.LocationTable().AppendEmpty().SetAddress(1)
167 d.LocationTable().AppendEmpty().SetAddress(2)
168
169 d.StackTable().AppendEmpty()
170 s := d.StackTable().AppendEmpty()
171 s.LocationIndices().Append(1)
172 return d
173 }(),
174 dst: func() ProfilesDictionary {
175 d := NewProfilesDictionary()
176 d.StackTable().AppendEmpty()
177 d.StackTable().AppendEmpty()
178 return d
179 }(),
180
181 wantSample: func() Sample {
182 s := NewSample()
183 s.SetStackIndex(2)
184 return s
185 }(),
186 wantDictionary: func() ProfilesDictionary {
187 d := NewProfilesDictionary()
188 d.LocationTable().AppendEmpty().SetAddress(2)
189
190 d.StackTable().AppendEmpty()
191 d.StackTable().AppendEmpty()
192 s := d.StackTable().AppendEmpty()
193 s.LocationIndices().Append(0)
194 return d
195 }(),
196 },
197 {
198 name: "with a stack index that does not match anything",
199 sample: func() Sample {
200 s := NewSample()
201 s.SetStackIndex(1)
202 return s
203 }(),
204
205 src: NewProfilesDictionary(),
206 dst: NewProfilesDictionary(),
207
208 wantSample: func() Sample {
209 s := NewSample()
210 s.SetStackIndex(1)
211 return s
212 }(),
213 wantDictionary: NewProfilesDictionary(),
214 wantErr: errors.New("invalid stack index 1"),
215 },
216 } {
217 t.Run(tt.name, func(t *testing.T) {
218 sample := tt.sample
219 dst := tt.dst
220 err := sample.switchDictionary(tt.src, dst)
221
222 if tt.wantErr == nil {
223 require.NoError(t, err)
224 } else {
225 require.Equal(t, tt.wantErr, err)
226 }
227
228 assert.Equal(t, tt.wantSample, sample)
229 assert.Equal(t, tt.wantDictionary, dst)
230 })
231 }
232 }
233
234 func BenchmarkSampleSwitchDictionary(b *testing.B) {
235 testutil.SkipMemoryBench(b)
236
237 s := NewSample()
238 s.SetLinkIndex(1)
239 s.SetStackIndex(1)
240
241 src := NewProfilesDictionary()
242 src.LocationTable().AppendEmpty()
243 src.LocationTable().AppendEmpty().SetAddress(2)
244 src.LinkTable().AppendEmpty()
245 src.LinkTable().AppendEmpty().SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
246 src.StackTable().AppendEmpty()
247 src.StackTable().AppendEmpty().LocationIndices().Append(1)
248
249 dst := NewProfilesDictionary()
250 src.LinkTable().AppendEmpty()
251 src.LinkTable().AppendEmpty().SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}))
252
253 b.ReportAllocs()
254
255 for b.Loop() {
256 _ = s.switchDictionary(src, dst)
257 }
258 }