Debian Codegraph @

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