Debian Codegraph @

pdata/pprofile/valuetype_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 TestValueTypeSwitchDictionary(t *testing.T) {
17 for _, tt := range []struct {
18 name string
19 valueType ValueType
20
21 src ProfilesDictionary
22 dst ProfilesDictionary
23
24 wantValueType ValueType
25 wantDictionary ProfilesDictionary
26 wantErr error
27 }{
28 {
29 name: "with an empty value type",
30 valueType: NewValueType(),
31
32 src: NewProfilesDictionary(),
33 dst: NewProfilesDictionary(),
34
35 wantValueType: NewValueType(),
36 wantDictionary: NewProfilesDictionary(),
37 },
38 {
39 name: "with an existing type",
40 valueType: func() ValueType {
41 vt := NewValueType()
42 vt.SetTypeStrindex(1)
43 return vt
44 }(),
45
46 src: func() ProfilesDictionary {
47 d := NewProfilesDictionary()
48 d.StringTable().Append("", "test")
49 return d
50 }(),
51 dst: func() ProfilesDictionary {
52 d := NewProfilesDictionary()
53 d.StringTable().Append("", "foo")
54 return d
55 }(),
56
57 wantValueType: func() ValueType {
58 vt := NewValueType()
59 vt.SetTypeStrindex(2)
60 return vt
61 }(),
62 wantDictionary: func() ProfilesDictionary {
63 d := NewProfilesDictionary()
64 d.StringTable().Append("", "foo", "test")
65 return d
66 }(),
67 },
68 {
69 name: "with a type index that does not match anything",
70 valueType: func() ValueType {
71 vt := NewValueType()
72 vt.SetTypeStrindex(1)
73 return vt
74 }(),
75
76 src: NewProfilesDictionary(),
77 dst: NewProfilesDictionary(),
78
79 wantValueType: func() ValueType {
80 vt := NewValueType()
81 vt.SetTypeStrindex(1)
82 return vt
83 }(),
84 wantDictionary: NewProfilesDictionary(),
85 wantErr: errors.New("invalid type index 1"),
86 },
87 {
88 name: "with an existing unit",
89 valueType: func() ValueType {
90 vt := NewValueType()
91 vt.SetUnitStrindex(1)
92 return vt
93 }(),
94
95 src: func() ProfilesDictionary {
96 d := NewProfilesDictionary()
97 d.StringTable().Append("", "test")
98 return d
99 }(),
100 dst: func() ProfilesDictionary {
101 d := NewProfilesDictionary()
102 d.StringTable().Append("", "foo")
103 return d
104 }(),
105
106 wantValueType: func() ValueType {
107 vt := NewValueType()
108 vt.SetUnitStrindex(2)
109 return vt
110 }(),
111 wantDictionary: func() ProfilesDictionary {
112 d := NewProfilesDictionary()
113 d.StringTable().Append("", "foo", "test")
114 return d
115 }(),
116 },
117 {
118 name: "with a unit index that does not match anything",
119 valueType: func() ValueType {
120 vt := NewValueType()
121 vt.SetUnitStrindex(1)
122 return vt
123 }(),
124
125 src: NewProfilesDictionary(),
126 dst: NewProfilesDictionary(),
127
128 wantValueType: func() ValueType {
129 vt := NewValueType()
130 vt.SetUnitStrindex(1)
131 return vt
132 }(),
133 wantDictionary: NewProfilesDictionary(),
134 wantErr: errors.New("invalid unit index 1"),
135 },
136 } {
137 t.Run(tt.name, func(t *testing.T) {
138 vt := tt.valueType
139 dst := tt.dst
140 err := vt.switchDictionary(tt.src, dst)
141
142 if tt.wantErr == nil {
143 require.NoError(t, err)
144 } else {
145 require.Equal(t, tt.wantErr, err)
146 }
147
148 assert.Equal(t, tt.wantValueType, vt)
149 assert.Equal(t, tt.wantDictionary, dst)
150 })
151 }
152 }
153
154 func BenchmarkValueTypeSwitchDictionary(b *testing.B) {
155 testutil.SkipMemoryBench(b)
156
157 vt := NewValueType()
158 vt.SetTypeStrindex(1)
159 vt.SetUnitStrindex(2)
160
161 src := NewProfilesDictionary()
162 src.StringTable().Append("", "test", "foo")
163
164 b.ReportAllocs()
165
166 for b.Loop() {
167 b.StopTimer()
168 dst := NewProfilesDictionary()
169 dst.StringTable().Append("", "foo")
170 b.StartTimer()
171
172 _ = vt.switchDictionary(src, dst)
173 }
174 }