| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofile |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 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/internal" |
| 15 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 16 |
) |
| 17 |
|
| 18 |
func TestReadOnlyProfilesInvalidUsage(t *testing.T) { |
| 19 |
pd := NewProfiles() |
| 20 |
assert.False(t, pd.IsReadOnly()) |
| 21 |
res := pd.ResourceProfiles().AppendEmpty().Resource() |
| 22 |
res.Attributes().PutStr("k1", "v1") |
| 23 |
pd.MarkReadOnly() |
| 24 |
assert.True(t, pd.IsReadOnly()) |
| 25 |
assert.Panics(t, func() { res.Attributes().PutStr("k2", "v2") }) |
| 26 |
} |
| 27 |
|
| 28 |
func TestSampleCount(t *testing.T) { |
| 29 |
pd := NewProfiles() |
| 30 |
assert.Equal(t, 0, pd.SampleCount()) |
| 31 |
|
| 32 |
rs := pd.ResourceProfiles().AppendEmpty() |
| 33 |
assert.Equal(t, 0, pd.SampleCount()) |
| 34 |
|
| 35 |
ils := rs.ScopeProfiles().AppendEmpty() |
| 36 |
assert.Equal(t, 0, pd.SampleCount()) |
| 37 |
|
| 38 |
ps := ils.Profiles().AppendEmpty() |
| 39 |
assert.Equal(t, 0, pd.SampleCount()) |
| 40 |
|
| 41 |
ps.Samples().AppendEmpty() |
| 42 |
assert.Equal(t, 1, pd.SampleCount()) |
| 43 |
|
| 44 |
ils2 := rs.ScopeProfiles().AppendEmpty() |
| 45 |
assert.Equal(t, 1, pd.SampleCount()) |
| 46 |
|
| 47 |
ps2 := ils2.Profiles().AppendEmpty() |
| 48 |
assert.Equal(t, 1, pd.SampleCount()) |
| 49 |
|
| 50 |
ps2.Samples().AppendEmpty() |
| 51 |
assert.Equal(t, 2, pd.SampleCount()) |
| 52 |
|
| 53 |
rms := pd.ResourceProfiles() |
| 54 |
rms.EnsureCapacity(3) |
| 55 |
rms.AppendEmpty().ScopeProfiles().AppendEmpty() |
| 56 |
ilss := rms.AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty().Samples() |
| 57 |
for range 5 { |
| 58 |
ilss.AppendEmpty() |
| 59 |
} |
| 60 |
// 5 + 2 (from rms.At(0) and rms.At(1) initialized first) |
| 61 |
assert.Equal(t, 7, pd.SampleCount()) |
| 62 |
} |
| 63 |
|
| 64 |
func TestSampleCountWithEmpty(t *testing.T) { |
| 65 |
assert.Equal(t, 0, newProfiles(&internal.ExportProfilesServiceRequest{ |
| 66 |
ResourceProfiles: []*internal.ResourceProfiles{{}}, |
| 67 |
}, new(internal.State)).SampleCount()) |
| 68 |
assert.Equal(t, 0, newProfiles(&internal.ExportProfilesServiceRequest{ |
| 69 |
ResourceProfiles: []*internal.ResourceProfiles{ |
| 70 |
{ |
| 71 |
ScopeProfiles: []*internal.ScopeProfiles{{}}, |
| 72 |
}, |
| 73 |
}, |
| 74 |
}, new(internal.State)).SampleCount()) |
| 75 |
assert.Equal(t, 1, newProfiles(&internal.ExportProfilesServiceRequest{ |
| 76 |
ResourceProfiles: []*internal.ResourceProfiles{ |
| 77 |
{ |
| 78 |
ScopeProfiles: []*internal.ScopeProfiles{ |
| 79 |
{ |
| 80 |
Profiles: []*internal.Profile{ |
| 81 |
{ |
| 82 |
Samples: []*internal.Sample{ |
| 83 |
{}, |
| 84 |
}, |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
}, |
| 91 |
}, new(internal.State)).SampleCount()) |
| 92 |
} |
| 93 |
|
| 94 |
func TestProfilesSwitchDictionary(t *testing.T) { |
| 95 |
for _, tt := range []struct { |
| 96 |
name string |
| 97 |
profiles Profiles |
| 98 |
|
| 99 |
src ProfilesDictionary |
| 100 |
dst ProfilesDictionary |
| 101 |
|
| 102 |
wantProfiles Profiles |
| 103 |
wantDictionary ProfilesDictionary |
| 104 |
wantErr error |
| 105 |
}{ |
| 106 |
{ |
| 107 |
name: "with an empty profiles", |
| 108 |
profiles: NewProfiles(), |
| 109 |
|
| 110 |
src: NewProfilesDictionary(), |
| 111 |
dst: NewProfilesDictionary(), |
| 112 |
|
| 113 |
wantProfiles: NewProfiles(), |
| 114 |
wantDictionary: NewProfilesDictionary(), |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "with a profiles that has a profile", |
| 118 |
profiles: func() Profiles { |
| 119 |
p := NewProfiles() |
| 120 |
profile := p.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 121 |
profile.Samples().AppendEmpty().SetLinkIndex(1) |
| 122 |
return p |
| 123 |
}(), |
| 124 |
|
| 125 |
src: func() ProfilesDictionary { |
| 126 |
d := NewProfilesDictionary() |
| 127 |
d.LinkTable().AppendEmpty() |
| 128 |
l := d.LinkTable().AppendEmpty() |
| 129 |
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 130 |
return d |
| 131 |
}(), |
| 132 |
dst: func() ProfilesDictionary { |
| 133 |
d := NewProfilesDictionary() |
| 134 |
d.LinkTable().AppendEmpty() |
| 135 |
d.LinkTable().AppendEmpty() |
| 136 |
return d |
| 137 |
}(), |
| 138 |
|
| 139 |
wantProfiles: func() Profiles { |
| 140 |
p := NewProfiles() |
| 141 |
profile := p.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 142 |
profile.Samples().AppendEmpty().SetLinkIndex(2) |
| 143 |
return p |
| 144 |
}(), |
| 145 |
wantDictionary: func() ProfilesDictionary { |
| 146 |
d := NewProfilesDictionary() |
| 147 |
d.LinkTable().AppendEmpty() |
| 148 |
d.LinkTable().AppendEmpty() |
| 149 |
l := d.LinkTable().AppendEmpty() |
| 150 |
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 151 |
return d |
| 152 |
}(), |
| 153 |
}, |
| 154 |
} { |
| 155 |
t.Run(tt.name, func(t *testing.T) { |
| 156 |
p := tt.profiles |
| 157 |
dst := tt.dst |
| 158 |
err := p.switchDictionary(tt.src, dst) |
| 159 |
|
| 160 |
if tt.wantErr == nil { |
| 161 |
require.NoError(t, err) |
| 162 |
} else { |
| 163 |
require.Equal(t, tt.wantErr, err) |
| 164 |
} |
| 165 |
|
| 166 |
assert.Equal(t, tt.wantProfiles, p) |
| 167 |
assert.Equal(t, tt.wantDictionary, dst) |
| 168 |
}) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func BenchmarkProfilesSwitchDictionary(b *testing.B) { |
| 173 |
testutil.SkipMemoryBench(b) |
| 174 |
|
| 175 |
p := NewProfiles() |
| 176 |
profile := p.ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 177 |
profile.Samples().AppendEmpty().SetLinkIndex(1) |
| 178 |
|
| 179 |
src := NewProfilesDictionary() |
| 180 |
src.LinkTable().AppendEmpty() |
| 181 |
src.LinkTable().AppendEmpty().SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 182 |
|
| 183 |
b.ReportAllocs() |
| 184 |
|
| 185 |
for b.Loop() { |
| 186 |
b.StopTimer() |
| 187 |
dst := NewProfilesDictionary() |
| 188 |
b.StartTimer() |
| 189 |
|
| 190 |
_ = p.switchDictionary(src, dst) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
func BenchmarkProfilesUsage(b *testing.B) { |
| 195 |
pd := generateTestProfiles() |
| 196 |
ts := pcommon.NewTimestampFromTime(time.Now()) |
| 197 |
dur := uint64(1_000_000_000) |
| 198 |
testValProfileID := ProfileID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) |
| 199 |
testSecondValProfileID := ProfileID([16]byte{2, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}) |
| 200 |
|
| 201 |
b.ReportAllocs() |
| 202 |
|
| 203 |
for b.Loop() { |
| 204 |
for i := 0; i < pd.ResourceProfiles().Len(); i++ { |
| 205 |
rs := pd.ResourceProfiles().At(i) |
| 206 |
res := rs.Resource() |
| 207 |
res.Attributes().PutStr("foo", "bar") |
| 208 |
v, ok := res.Attributes().Get("foo") |
| 209 |
assert.True(b, ok) |
| 210 |
assert.Equal(b, "bar", v.Str()) |
| 211 |
v.SetStr("new-bar") |
| 212 |
assert.Equal(b, "new-bar", v.Str()) |
| 213 |
res.Attributes().Remove("foo") |
| 214 |
for j := 0; j < rs.ScopeProfiles().Len(); j++ { |
| 215 |
iss := rs.ScopeProfiles().At(j) |
| 216 |
iss.Scope().SetName("new_test_name") |
| 217 |
assert.Equal(b, "new_test_name", iss.Scope().Name()) |
| 218 |
for k := 0; k < iss.Profiles().Len(); k++ { |
| 219 |
s := iss.Profiles().At(k) |
| 220 |
s.SetProfileID(testValProfileID) |
| 221 |
assert.Equal(b, testValProfileID, s.ProfileID()) |
| 222 |
s.SetTime(ts) |
| 223 |
assert.Equal(b, ts, s.Time()) |
| 224 |
s.SetDurationNano(dur) |
| 225 |
assert.Equal(b, dur, s.DurationNano()) |
| 226 |
} |
| 227 |
s := iss.Profiles().AppendEmpty() |
| 228 |
s.SetProfileID(testSecondValProfileID) |
| 229 |
s.SetTime(ts) |
| 230 |
s.SetDurationNano(dur) |
| 231 |
s.AttributeIndices().Append(1) |
| 232 |
iss.Profiles().RemoveIf(func(lr Profile) bool { |
| 233 |
return lr.ProfileID() == testSecondValProfileID |
| 234 |
}) |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func BenchmarkProfilesMarshalJSON(b *testing.B) { |
| 241 |
pd := generateTestProfiles() |
| 242 |
encoder := &JSONMarshaler{} |
| 243 |
|
| 244 |
b.ReportAllocs() |
| 245 |
|
| 246 |
for b.Loop() { |
| 247 |
jsonBuf, err := encoder.MarshalProfiles(pd) |
| 248 |
require.NoError(b, err) |
| 249 |
require.NotNil(b, jsonBuf) |
| 250 |
} |
| 251 |
} |