| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofile |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/internal/testutil" |
| 13 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 14 |
) |
| 15 |
|
| 16 |
func TestResourceProfilesSwitchDictionary(t *testing.T) { |
| 17 |
for _, tt := range []struct { |
| 18 |
name string |
| 19 |
resourceProfiles ResourceProfiles |
| 20 |
|
| 21 |
src ProfilesDictionary |
| 22 |
dst ProfilesDictionary |
| 23 |
|
| 24 |
wantResourceProfiles ResourceProfiles |
| 25 |
wantDictionary ProfilesDictionary |
| 26 |
wantErr error |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "with an empty resource profile", |
| 30 |
resourceProfiles: NewResourceProfiles(), |
| 31 |
|
| 32 |
src: NewProfilesDictionary(), |
| 33 |
dst: NewProfilesDictionary(), |
| 34 |
|
| 35 |
wantResourceProfiles: NewResourceProfiles(), |
| 36 |
wantDictionary: NewProfilesDictionary(), |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "with a resource profiles that has a profile", |
| 40 |
resourceProfiles: func() ResourceProfiles { |
| 41 |
r := NewResourceProfiles() |
| 42 |
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 43 |
profile.Samples().AppendEmpty().SetLinkIndex(1) |
| 44 |
return r |
| 45 |
}(), |
| 46 |
|
| 47 |
src: func() ProfilesDictionary { |
| 48 |
d := NewProfilesDictionary() |
| 49 |
d.LinkTable().AppendEmpty() |
| 50 |
l := d.LinkTable().AppendEmpty() |
| 51 |
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 52 |
return d |
| 53 |
}(), |
| 54 |
dst: func() ProfilesDictionary { |
| 55 |
d := NewProfilesDictionary() |
| 56 |
d.LinkTable().AppendEmpty() |
| 57 |
d.LinkTable().AppendEmpty() |
| 58 |
return d |
| 59 |
}(), |
| 60 |
|
| 61 |
wantResourceProfiles: func() ResourceProfiles { |
| 62 |
r := NewResourceProfiles() |
| 63 |
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 64 |
profile.Samples().AppendEmpty().SetLinkIndex(2) |
| 65 |
return r |
| 66 |
}(), |
| 67 |
wantDictionary: func() ProfilesDictionary { |
| 68 |
d := NewProfilesDictionary() |
| 69 |
d.LinkTable().AppendEmpty() |
| 70 |
d.LinkTable().AppendEmpty() |
| 71 |
l := d.LinkTable().AppendEmpty() |
| 72 |
l.SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 73 |
return d |
| 74 |
}(), |
| 75 |
}, |
| 76 |
} { |
| 77 |
t.Run(tt.name, func(t *testing.T) { |
| 78 |
rp := tt.resourceProfiles |
| 79 |
dst := tt.dst |
| 80 |
err := rp.switchDictionary(tt.src, dst) |
| 81 |
|
| 82 |
if tt.wantErr == nil { |
| 83 |
require.NoError(t, err) |
| 84 |
} else { |
| 85 |
require.Equal(t, tt.wantErr, err) |
| 86 |
} |
| 87 |
|
| 88 |
assert.Equal(t, tt.wantResourceProfiles, rp) |
| 89 |
assert.Equal(t, tt.wantDictionary, dst) |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func BenchmarkResourceProfilesSwitchDictionary(b *testing.B) { |
| 95 |
testutil.SkipMemoryBench(b) |
| 96 |
|
| 97 |
r := NewResourceProfiles() |
| 98 |
profile := r.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() |
| 99 |
profile.Samples().AppendEmpty().SetLinkIndex(1) |
| 100 |
|
| 101 |
src := NewProfilesDictionary() |
| 102 |
src.LinkTable().AppendEmpty() |
| 103 |
src.LinkTable().AppendEmpty().SetSpanID(pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8})) |
| 104 |
|
| 105 |
b.ReportAllocs() |
| 106 |
|
| 107 |
for b.Loop() { |
| 108 |
b.StopTimer() |
| 109 |
dst := NewProfilesDictionary() |
| 110 |
b.StartTimer() |
| 111 |
|
| 112 |
_ = r.switchDictionary(src, dst) |
| 113 |
} |
| 114 |
} |