Debian Codegraph @

pdata/pprofile/profile_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 TestProfileSwitchDictionary(t *testing.T) {
18 for _, tt := range []struct {
19 name string
20 profile Profile
21
22 src ProfilesDictionary
23 dst ProfilesDictionary
24
25 wantProfile Profile
26 wantDictionary ProfilesDictionary
27 wantErr error
28 }{
29 {
30 name: "with an empty profile",
31 profile: NewProfile(),
32
33 src: NewProfilesDictionary(),
34 dst: NewProfilesDictionary(),
35
36 wantProfile: NewProfile(),
37 wantDictionary: NewProfilesDictionary(),
38 },
39 {
40 name: "with an existing attribute",
41 profile: func() Profile {
42 p := NewProfile()
43 p.AttributeIndices().Append(1)
44 return p
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 wantProfile: func() Profile {
67 p := NewProfile()
68 p.AttributeIndices().Append(2)
69 return p
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 profile: func() Profile {
85 p := NewProfile()
86 p.AttributeIndices().Append(1)
87 return p
88 }(),
89
90 src: NewProfilesDictionary(),
91 dst: NewProfilesDictionary(),
92
93 wantProfile: func() Profile {
94 p := NewProfile()
95 p.AttributeIndices().Append(1)
96 return p
97 }(),
98 wantDictionary: NewProfilesDictionary(),
99 wantErr: errors.New("invalid attribute index 1"),
100 },
101 {
102 name: "with a profile that has a sample",
103 profile: func() Profile {
104 p := NewProfile()
105 p.Samples().AppendEmpty().SetLinkIndex(1)
106 return p
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 wantProfile: func() Profile {
124 p := NewProfile()
125 p.Samples().AppendEmpty().SetLinkIndex(2)
126 return p
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 profile that has a period type",
139 profile: func() Profile {
140 p := NewProfile()
141 p.PeriodType().SetTypeStrindex(1)
142 return p
143 }(),
144
145 src: func() ProfilesDictionary {
146 d := NewProfilesDictionary()
147 d.StringTable().Append("", "test")
148 return d
149 }(),
150 dst: func() ProfilesDictionary {
151 d := NewProfilesDictionary()
152 d.StringTable().Append("", "foo")
153 return d
154 }(),
155
156 wantProfile: func() Profile {
157 p := NewProfile()
158 p.PeriodType().SetTypeStrindex(2)
159 return p
160 }(),
161 wantDictionary: func() ProfilesDictionary {
162 d := NewProfilesDictionary()
163 d.StringTable().Append("", "foo", "test")
164 return d
165 }(),
166 },
167 {
168 name: "with a profile that has a sample type",
169 profile: func() Profile {
170 p := NewProfile()
171 p.SampleType().SetTypeStrindex(1)
172 return p
173 }(),
174
175 src: func() ProfilesDictionary {
176 d := NewProfilesDictionary()
177 d.StringTable().Append("", "test")
178 return d
179 }(),
180 dst: func() ProfilesDictionary {
181 d := NewProfilesDictionary()
182 d.StringTable().Append("", "foo")
183 return d
184 }(),
185
186 wantProfile: func() Profile {
187 p := NewProfile()
188 p.SampleType().SetTypeStrindex(2)
189 return p
190 }(),
191 wantDictionary: func() ProfilesDictionary {
192 d := NewProfilesDictionary()
193 d.StringTable().Append("", "foo", "test")
194 return d
195 }(),
196 },
197 } {
198 t.Run(tt.name, func(t *testing.T) {
199 profile := tt.profile
200 dst := tt.dst
201 err := profile.switchDictionary(tt.src, dst)
202
203 if tt.wantErr == nil {
204 require.NoError(t, err)
205 } else {
206 require.Equal(t, tt.wantErr, err)
207 }
208
209 assert.Equal(t, tt.wantProfile, profile)
210 assert.Equal(t, tt.wantDictionary, dst)
211 })
212 }
213 }
214
215 func BenchmarkProfileSwitchDictionary(b *testing.B) {
216 testutil.SkipMemoryBench(b)
217
218 p := NewProfile()
219 p.AttributeIndices().Append(1, 2)
220
221 src := NewProfilesDictionary()
222 src.StringTable().Append("", "test", "foo")
223 src.AttributeTable().AppendEmpty()
224 src.AttributeTable().AppendEmpty().SetKeyStrindex(1)
225 src.AttributeTable().AppendEmpty().SetKeyStrindex(2)
226
227 b.ReportAllocs()
228
229 for b.Loop() {
230 b.StopTimer()
231 dst := NewProfilesDictionary()
232 dst.StringTable().Append("", "foo")
233 dst.AttributeTable().AppendEmpty()
234 dst.AttributeTable().AppendEmpty().SetKeyStrindex(1)
235 b.StartTimer()
236
237 _ = p.switchDictionary(src, dst)
238 }
239 }
240
241 func TestProfile_Duration(_ *testing.T) {
242 ms := NewProfile()
243 ms.SetDuration(0)
244
245 ts := ms.Duration()
246 _ = ts
247 }