Debian Codegraph @

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