Debian Codegraph @

pdata/pprofile/function_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 )
15
16 func TestFunctionEqual(t *testing.T) {
17 for _, tt := range []struct {
18 name string
19 orig Function
20 dest Function
21 want bool
22 }{
23 {
24 name: "empty functions",
25 orig: NewFunction(),
26 dest: NewFunction(),
27 want: true,
28 },
29 {
30 name: "non-empty identical functions",
31 orig: buildFunction(1, 2, 3, 4),
32 dest: buildFunction(1, 2, 3, 4),
33 want: true,
34 },
35 {
36 name: "with different name",
37 orig: buildFunction(1, 2, 3, 4),
38 dest: buildFunction(2, 2, 3, 4),
39 want: false,
40 },
41 {
42 name: "with different system name",
43 orig: buildFunction(1, 2, 3, 4),
44 dest: buildFunction(1, 3, 3, 4),
45 want: false,
46 },
47 {
48 name: "with different file name",
49 orig: buildFunction(1, 2, 3, 4),
50 dest: buildFunction(1, 2, 4, 4),
51 want: false,
52 },
53 {
54 name: "with different start line",
55 orig: buildFunction(1, 2, 3, 4),
56 dest: buildFunction(1, 2, 3, 5),
57 want: false,
58 },
59 } {
60 t.Run(tt.name, func(t *testing.T) {
61 if tt.want {
62 assert.True(t, tt.orig.Equal(tt.dest))
63 } else {
64 assert.False(t, tt.orig.Equal(tt.dest))
65 }
66 })
67 }
68 }
69
70 func TestFunctionSwitchDictionary(t *testing.T) {
71 for _, tt := range []struct {
72 name string
73 function Function
74
75 src ProfilesDictionary
76 dst ProfilesDictionary
77
78 wantFunction Function
79 wantDictionary ProfilesDictionary
80 wantErr error
81 }{
82 {
83 name: "with an empty key value and unit",
84 function: NewFunction(),
85
86 src: NewProfilesDictionary(),
87 dst: NewProfilesDictionary(),
88
89 wantFunction: NewFunction(),
90 wantDictionary: NewProfilesDictionary(),
91 },
92 {
93 name: "with an existing name",
94 function: func() Function {
95 fn := NewFunction()
96 fn.SetNameStrindex(1)
97 return fn
98 }(),
99
100 src: func() ProfilesDictionary {
101 d := NewProfilesDictionary()
102 d.StringTable().Append("", "test")
103 return d
104 }(),
105 dst: func() ProfilesDictionary {
106 d := NewProfilesDictionary()
107 d.StringTable().Append("", "foo")
108 return d
109 }(),
110
111 wantFunction: func() Function {
112 fn := NewFunction()
113 fn.SetNameStrindex(2)
114 return fn
115 }(),
116 wantDictionary: func() ProfilesDictionary {
117 d := NewProfilesDictionary()
118 d.StringTable().Append("", "foo", "test")
119 return d
120 }(),
121 },
122 {
123 name: "with a name index that does not match anything",
124 function: func() Function {
125 fn := NewFunction()
126 fn.SetNameStrindex(1)
127 return fn
128 }(),
129
130 src: NewProfilesDictionary(),
131 dst: NewProfilesDictionary(),
132
133 wantFunction: func() Function {
134 fn := NewFunction()
135 fn.SetNameStrindex(1)
136 return fn
137 }(),
138 wantDictionary: NewProfilesDictionary(),
139 wantErr: errors.New("invalid name index 1"),
140 },
141 {
142 name: "with an existing system name",
143 function: func() Function {
144 fn := NewFunction()
145 fn.SetSystemNameStrindex(1)
146 return fn
147 }(),
148
149 src: func() ProfilesDictionary {
150 d := NewProfilesDictionary()
151 d.StringTable().Append("", "test")
152 return d
153 }(),
154 dst: func() ProfilesDictionary {
155 d := NewProfilesDictionary()
156 d.StringTable().Append("", "foo")
157 return d
158 }(),
159
160 wantFunction: func() Function {
161 fn := NewFunction()
162 fn.SetSystemNameStrindex(2)
163 return fn
164 }(),
165 wantDictionary: func() ProfilesDictionary {
166 d := NewProfilesDictionary()
167 d.StringTable().Append("", "foo", "test")
168 return d
169 }(),
170 },
171 {
172 name: "with a system name index that does not match anything",
173 function: func() Function {
174 fn := NewFunction()
175 fn.SetSystemNameStrindex(1)
176 return fn
177 }(),
178
179 src: NewProfilesDictionary(),
180 dst: NewProfilesDictionary(),
181
182 wantFunction: func() Function {
183 fn := NewFunction()
184 fn.SetSystemNameStrindex(1)
185 return fn
186 }(),
187 wantDictionary: NewProfilesDictionary(),
188 wantErr: errors.New("invalid system name index 1"),
189 },
190 {
191 name: "with an existing filename",
192 function: func() Function {
193 fn := NewFunction()
194 fn.SetFilenameStrindex(1)
195 return fn
196 }(),
197
198 src: func() ProfilesDictionary {
199 d := NewProfilesDictionary()
200 d.StringTable().Append("", "test")
201 return d
202 }(),
203 dst: func() ProfilesDictionary {
204 d := NewProfilesDictionary()
205 d.StringTable().Append("", "foo")
206 return d
207 }(),
208
209 wantFunction: func() Function {
210 fn := NewFunction()
211 fn.SetFilenameStrindex(2)
212 return fn
213 }(),
214 wantDictionary: func() ProfilesDictionary {
215 d := NewProfilesDictionary()
216 d.StringTable().Append("", "foo", "test")
217 return d
218 }(),
219 },
220 {
221 name: "with a filename index that does not match anything",
222 function: func() Function {
223 fn := NewFunction()
224 fn.SetFilenameStrindex(1)
225 return fn
226 }(),
227
228 src: NewProfilesDictionary(),
229 dst: NewProfilesDictionary(),
230
231 wantFunction: func() Function {
232 fn := NewFunction()
233 fn.SetFilenameStrindex(1)
234 return fn
235 }(),
236 wantDictionary: NewProfilesDictionary(),
237 wantErr: errors.New("invalid filename index 1"),
238 },
239 } {
240 t.Run(tt.name, func(t *testing.T) {
241 fn := tt.function
242 dst := tt.dst
243 err := fn.switchDictionary(tt.src, dst)
244
245 if tt.wantErr == nil {
246 require.NoError(t, err)
247 } else {
248 require.Equal(t, tt.wantErr, err)
249 }
250
251 assert.Equal(t, tt.wantFunction, fn)
252 assert.Equal(t, tt.wantDictionary, dst)
253 })
254 }
255 }
256
257 func BenchmarkFunctionSwitchDictionary(b *testing.B) {
258 testutil.SkipMemoryBench(b)
259
260 fn := NewFunction()
261 fn.SetNameStrindex(1)
262 fn.SetSystemNameStrindex(2)
263
264 src := NewProfilesDictionary()
265 src.StringTable().Append("", "test", "foo")
266
267 b.ReportAllocs()
268
269 for b.Loop() {
270 b.StopTimer()
271 dst := NewProfilesDictionary()
272 dst.StringTable().Append("", "foo")
273 b.StartTimer()
274
275 _ = fn.switchDictionary(src, dst)
276 }
277 }
278
279 func buildFunction(name, sName, fileName int32, startLine int64) Function {
280 f := NewFunction()
281 f.SetNameStrindex(name)
282 f.SetSystemNameStrindex(sName)
283 f.SetFilenameStrindex(fileName)
284 f.SetStartLine(startLine)
285 return f
286 }