@@ -226,3 +226,112 @@ func BenchmarkPutAttribute(b *testing.B) {
226226 })
227227 }
228228}
229+
230+ func TestSetAttribute (t * testing.T ) {
231+ table := NewKeyValueAndUnitSlice ()
232+ attr := NewKeyValueAndUnit ()
233+ attr .SetKeyStrindex (1 )
234+ attr .SetUnitStrindex (2 )
235+ require .NoError (t , attr .Value ().FromRaw ("test" ))
236+ attr2 := NewKeyValueAndUnit ()
237+ attr2 .SetKeyStrindex (3 )
238+ attr2 .SetUnitStrindex (4 )
239+ require .NoError (t , attr .Value ().FromRaw ("test2" ))
240+
241+ // Put a first value
242+ idx , err := SetAttribute (table , attr )
243+ require .NoError (t , err )
244+ assert .Equal (t , 1 , table .Len ())
245+ assert .Equal (t , int32 (0 ), idx )
246+
247+ // Put the same attribute
248+ // This should be a no-op.
249+ idx , err = SetAttribute (table , attr )
250+ require .NoError (t , err )
251+ assert .Equal (t , 1 , table .Len ())
252+ assert .Equal (t , int32 (0 ), idx )
253+
254+ // Set a new value
255+ // This sets the index and adds to the table.
256+ idx , err = SetAttribute (table , attr2 )
257+ require .NoError (t , err )
258+ assert .Equal (t , 2 , table .Len ())
259+ assert .Equal (t , int32 (table .Len ()- 1 ), idx ) //nolint:gosec // G115
260+
261+ // Set an existing value
262+ idx , err = SetAttribute (table , attr )
263+ require .NoError (t , err )
264+ assert .Equal (t , 2 , table .Len ())
265+ assert .Equal (t , int32 (0 ), idx )
266+ // Set another existing value
267+ idx , err = SetAttribute (table , attr2 )
268+ require .NoError (t , err )
269+ assert .Equal (t , 2 , table .Len ())
270+ assert .Equal (t , int32 (table .Len ()- 1 ), idx ) //nolint:gosec // G115
271+ }
272+
273+ func BenchmarkSetAttribute (b * testing.B ) {
274+ for _ , bb := range []struct {
275+ name string
276+ attr KeyValueAndUnit
277+
278+ runBefore func (* testing.B , KeyValueAndUnitSlice )
279+ }{
280+ {
281+ name : "with a new attribute" ,
282+ attr : NewKeyValueAndUnit (),
283+ },
284+ {
285+ name : "with an existing attribute" ,
286+ attr : func () KeyValueAndUnit {
287+ a := NewKeyValueAndUnit ()
288+ a .SetKeyStrindex (1 )
289+ return a
290+ }(),
291+
292+ runBefore : func (_ * testing.B , table KeyValueAndUnitSlice ) {
293+ a := table .AppendEmpty ()
294+ a .SetKeyStrindex (1 )
295+ },
296+ },
297+ {
298+ name : "with a duplicate attribute" ,
299+ attr : NewKeyValueAndUnit (),
300+
301+ runBefore : func (_ * testing.B , table KeyValueAndUnitSlice ) {
302+ _ , err := SetAttribute (table , NewKeyValueAndUnit ())
303+ require .NoError (b , err )
304+ },
305+ },
306+ {
307+ name : "with a hundred locations to loop through" ,
308+ attr : func () KeyValueAndUnit {
309+ a := NewKeyValueAndUnit ()
310+ a .SetKeyStrindex (1 )
311+ return a
312+ }(),
313+
314+ runBefore : func (_ * testing.B , table KeyValueAndUnitSlice ) {
315+ for i := range 100 {
316+ l := table .AppendEmpty ()
317+ l .SetKeyStrindex (int32 (i )) //nolint:gosec // overflow checked
318+ }
319+ },
320+ },
321+ } {
322+ b .Run (bb .name , func (b * testing.B ) {
323+ table := NewKeyValueAndUnitSlice ()
324+
325+ if bb .runBefore != nil {
326+ bb .runBefore (b , table )
327+ }
328+
329+ b .ResetTimer ()
330+ b .ReportAllocs ()
331+
332+ for b .Loop () {
333+ _ , _ = SetAttribute (table , bb .attr )
334+ }
335+ })
336+ }
337+ }
0 commit comments