Debian Codegraph @

pdata/pprofile/attributes_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pprofile
5
6 import (
7 "fmt"
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 TestFromAttributeIndices(t *testing.T) {
18 dic := NewProfilesDictionary()
19 dic.StringTable().Append("")
20 dic.StringTable().Append("hello")
21 dic.StringTable().Append("bonjour")
22
23 table := NewKeyValueAndUnitSlice()
24 att := table.AppendEmpty()
25 att.SetKeyStrindex(1)
26 att.Value().SetStr("world")
27 att2 := table.AppendEmpty()
28 att2.SetKeyStrindex(2)
29 att2.Value().SetStr("monde")
30
31 attrs := FromAttributeIndices(table, NewProfile(), dic)
32 assert.Equal(t, attrs, pcommon.NewMap())
33
34 // A Location with a single attribute
35 loc := NewLocation()
36 loc.AttributeIndices().Append(0)
37
38 attrs = FromAttributeIndices(table, loc, dic)
39
40 m := map[string]any{"hello": "world"}
41 assert.Equal(t, attrs.AsRaw(), m)
42
43 // A Mapping with two attributes
44 mapp := NewLocation()
45 mapp.AttributeIndices().Append(0, 1)
46
47 attrs = FromAttributeIndices(table, mapp, dic)
48
49 m = map[string]any{"hello": "world", "bonjour": "monde"}
50 assert.Equal(t, attrs.AsRaw(), m)
51 }
52
53 func BenchmarkFromAttributeIndices(b *testing.B) {
54 dic := NewProfilesDictionary()
55 table := NewKeyValueAndUnitSlice()
56
57 for i := range 10 {
58 dic.StringTable().Append(fmt.Sprintf("key_%d", i))
59
60 att := table.AppendEmpty()
61 att.SetKeyStrindex(int32(dic.StringTable().Len()))
62 att.Value().SetStr(fmt.Sprintf("value_%d", i))
63 }
64
65 obj := NewLocation()
66 obj.AttributeIndices().Append(1, 3, 7)
67
68 b.ReportAllocs()
69
70 for b.Loop() {
71 _ = FromAttributeIndices(table, obj, dic)
72 }
73 }
74
75 func TestSetAttribute(t *testing.T) {
76 table := NewKeyValueAndUnitSlice()
77 attr := NewKeyValueAndUnit()
78 attr.SetKeyStrindex(1)
79 attr.SetUnitStrindex(2)
80 require.NoError(t, attr.Value().FromRaw("test"))
81 attr2 := NewKeyValueAndUnit()
82 attr2.SetKeyStrindex(3)
83 attr2.SetUnitStrindex(4)
84 require.NoError(t, attr.Value().FromRaw("test2"))
85
86 // Put a first value
87 idx, err := SetAttribute(table, attr)
88 require.NoError(t, err)
89 assert.Equal(t, 1, table.Len())
90 assert.Equal(t, int32(0), idx)
91
92 // Put the same attribute
93 // This should be a no-op.
94 idx, err = SetAttribute(table, attr)
95 require.NoError(t, err)
96 assert.Equal(t, 1, table.Len())
97 assert.Equal(t, int32(0), idx)
98
99 // Set a new value
100 // This sets the index and adds to the table.
101 idx, err = SetAttribute(table, attr2)
102 require.NoError(t, err)
103 assert.Equal(t, 2, table.Len())
104 assert.Equal(t, int32(table.Len()-1), idx)
105
106 // Set an existing value
107 idx, err = SetAttribute(table, attr)
108 require.NoError(t, err)
109 assert.Equal(t, 2, table.Len())
110 assert.Equal(t, int32(0), idx)
111 // Set another existing value
112 idx, err = SetAttribute(table, attr2)
113 require.NoError(t, err)
114 assert.Equal(t, 2, table.Len())
115 assert.Equal(t, int32(table.Len()-1), idx)
116 }
117
118 func BenchmarkSetAttribute(b *testing.B) {
119 testutil.SkipMemoryBench(b)
120 for _, bb := range []struct {
121 name string
122 attr KeyValueAndUnit
123
124 runBefore func(*testing.B, KeyValueAndUnitSlice)
125 }{
126 {
127 name: "with a new attribute",
128 attr: NewKeyValueAndUnit(),
129 },
130 {
131 name: "with an existing attribute",
132 attr: func() KeyValueAndUnit {
133 a := NewKeyValueAndUnit()
134 a.SetKeyStrindex(1)
135 return a
136 }(),
137
138 runBefore: func(_ *testing.B, table KeyValueAndUnitSlice) {
139 a := table.AppendEmpty()
140 a.SetKeyStrindex(1)
141 },
142 },
143 {
144 name: "with a duplicate attribute",
145 attr: NewKeyValueAndUnit(),
146
147 runBefore: func(_ *testing.B, table KeyValueAndUnitSlice) {
148 _, err := SetAttribute(table, NewKeyValueAndUnit())
149 require.NoError(b, err)
150 },
151 },
152 {
153 name: "with a hundred locations to loop through",
154 attr: func() KeyValueAndUnit {
155 a := NewKeyValueAndUnit()
156 a.SetKeyStrindex(1)
157 return a
158 }(),
159
160 runBefore: func(_ *testing.B, table KeyValueAndUnitSlice) {
161 for i := range 100 {
162 l := table.AppendEmpty()
163 l.SetKeyStrindex(int32(i))
164 }
165 },
166 },
167 } {
168 b.Run(bb.name, func(b *testing.B) {
169 table := NewKeyValueAndUnitSlice()
170
171 if bb.runBefore != nil {
172 bb.runBefore(b, table)
173 }
174
175 b.ResetTimer()
176 b.ReportAllocs()
177
178 for b.Loop() {
179 _, _ = SetAttribute(table, bb.attr)
180 }
181 })
182 }
183 }