Debian Codegraph @

pdata/pprofile/scopeprofiles_test.go

raw
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 TestScopeProfilesSwitchDictionary(t *testing.T) {
17 for _, tt := range []struct {
18 name string
19 scopeProfiles ScopeProfiles
20
21 src ProfilesDictionary
22 dst ProfilesDictionary
23
24 wantScopeProfiles ScopeProfiles
25 wantDictionary ProfilesDictionary
26 wantErr error
27 }{
28 {
29 name: "with an empty scope profile",
30 scopeProfiles: NewScopeProfiles(),
31
32 src: NewProfilesDictionary(),
33 dst: NewProfilesDictionary(),
34
35 wantScopeProfiles: NewScopeProfiles(),
36 wantDictionary: NewProfilesDictionary(),
37 },
38 {
39 name: "with a scope profiles that has a profile",
40 scopeProfiles: func() ScopeProfiles {
41 s := NewScopeProfiles()
42 profile := s.Profiles().AppendEmpty()
43 profile.Samples().AppendEmpty().SetLinkIndex(1)
44 return s
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 wantScopeProfiles: func() ScopeProfiles {
62 s := NewScopeProfiles()
63 profile := s.Profiles().AppendEmpty()
64 profile.Samples().AppendEmpty().SetLinkIndex(2)
65 return s
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 sp := tt.scopeProfiles
79 dst := tt.dst
80 err := sp.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.wantScopeProfiles, sp)
89 assert.Equal(t, tt.wantDictionary, dst)
90 })
91 }
92 }
93
94 func BenchmarkScopeProfilesSwitchDictionary(b *testing.B) {
95 testutil.SkipMemoryBench(b)
96
97 s := NewScopeProfiles()
98 profile := s.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 dst := NewProfilesDictionary()
106
107 b.ReportAllocs()
108
109 for b.Loop() {
110 _ = s.switchDictionary(src, dst)
111 }
112 }