@@ -74,6 +74,48 @@ func TestAddAttribute(t *testing.T) {
74
74
assert .Equal (t , []int32 {0 }, mapp .AttributeIndices ().AsRaw ())
75
75
}
76
76
77
+ func TestPutAttribute (t * testing.T ) {
78
+ table := NewAttributeTableSlice ()
79
+ indices := NewProfile ()
80
+
81
+ // Put a first attribute.
82
+ require .NoError (t , PutAttribute (table , indices , "hello" , pcommon .NewValueStr ("world" )))
83
+ assert .Equal (t , 1 , table .Len ())
84
+ assert .Equal (t , []int32 {0 }, indices .AttributeIndices ().AsRaw ())
85
+
86
+ // Put an attribute, same key, same value.
87
+ // This should be a no-op.
88
+ require .NoError (t , PutAttribute (table , indices , "hello" , pcommon .NewValueStr ("world" )))
89
+ assert .Equal (t , 1 , table .Len ())
90
+ assert .Equal (t , []int32 {0 }, indices .AttributeIndices ().AsRaw ())
91
+
92
+ // Put an attribute, same key, different value.
93
+ // This updates the index and adds to the table.
94
+ fmt .Printf ("test\n " )
95
+ require .NoError (t , PutAttribute (table , indices , "hello" , pcommon .NewValueStr ("world2" )))
96
+ assert .Equal (t , 2 , table .Len ())
97
+ assert .Equal (t , []int32 {1 }, indices .AttributeIndices ().AsRaw ())
98
+
99
+ // Put an attribute that already exists in the table.
100
+ // This updates the index and does not add to the table.
101
+ require .NoError (t , PutAttribute (table , indices , "hello" , pcommon .NewValueStr ("world" )))
102
+ assert .Equal (t , 2 , table .Len ())
103
+ assert .Equal (t , []int32 {0 }, indices .AttributeIndices ().AsRaw ())
104
+
105
+ // Put a new attribute.
106
+ // This adds an index and adds to the table.
107
+ require .NoError (t , PutAttribute (table , indices , "good" , pcommon .NewValueStr ("day" )))
108
+ assert .Equal (t , 3 , table .Len ())
109
+ assert .Equal (t , []int32 {0 , 2 }, indices .AttributeIndices ().AsRaw ())
110
+
111
+ // Add multiple distinct attributes.
112
+ for i := range 100 {
113
+ require .NoError (t , PutAttribute (table , indices , fmt .Sprintf ("key_%d" , i ), pcommon .NewValueStr ("day" )))
114
+ assert .Equal (t , i + 4 , table .Len ())
115
+ assert .Equal (t , i + 3 , indices .AttributeIndices ().Len ())
116
+ }
117
+ }
118
+
77
119
func BenchmarkFromAttributeIndices (b * testing.B ) {
78
120
table := NewAttributeTableSlice ()
79
121
@@ -162,3 +204,72 @@ func BenchmarkAddAttribute(b *testing.B) {
162
204
})
163
205
}
164
206
}
207
+
208
+ func BenchmarkPutAttribute (b * testing.B ) {
209
+ for _ , bb := range []struct {
210
+ name string
211
+ key string
212
+ value pcommon.Value
213
+
214
+ runBefore func (* testing.B , AttributeTableSlice , attributable )
215
+ }{
216
+ {
217
+ name : "with a new string attribute" ,
218
+ key : "attribute" ,
219
+ value : pcommon .NewValueStr ("test" ),
220
+ },
221
+ {
222
+ name : "with an existing attribute" ,
223
+ key : "attribute" ,
224
+ value : pcommon .NewValueStr ("test" ),
225
+
226
+ runBefore : func (_ * testing.B , table AttributeTableSlice , _ attributable ) {
227
+ entry := table .AppendEmpty ()
228
+ entry .SetKey ("attribute" )
229
+ entry .Value ().SetStr ("test" )
230
+ },
231
+ },
232
+ {
233
+ name : "with a duplicate attribute" ,
234
+ key : "attribute" ,
235
+ value : pcommon .NewValueStr ("test" ),
236
+
237
+ runBefore : func (_ * testing.B , table AttributeTableSlice , obj attributable ) {
238
+ require .NoError (b , PutAttribute (table , obj , "attribute" , pcommon .NewValueStr ("test" )))
239
+ },
240
+ },
241
+ {
242
+ name : "with a hundred attributes to loop through" ,
243
+ key : "attribute" ,
244
+ value : pcommon .NewValueStr ("test" ),
245
+
246
+ runBefore : func (_ * testing.B , table AttributeTableSlice , _ attributable ) {
247
+ for i := range 100 {
248
+ entry := table .AppendEmpty ()
249
+ entry .SetKey (fmt .Sprintf ("attr_%d" , i ))
250
+ entry .Value ().SetStr ("test" )
251
+ }
252
+
253
+ entry := table .AppendEmpty ()
254
+ entry .SetKey ("attribute" )
255
+ entry .Value ().SetStr ("test" )
256
+ },
257
+ },
258
+ } {
259
+ b .Run (bb .name , func (b * testing.B ) {
260
+ table := NewAttributeTableSlice ()
261
+ obj := NewLocation ()
262
+
263
+ if bb .runBefore != nil {
264
+ bb .runBefore (b , table , obj )
265
+ }
266
+
267
+ b .ResetTimer ()
268
+ b .ReportAllocs ()
269
+
270
+ for n := 0 ; n < b .N ; n ++ {
271
+ _ = PutAttribute (table , obj , bb .key , bb .value )
272
+ }
273
+ })
274
+ }
275
+ }
0 commit comments