|
| 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/pdata/pcommon" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSetLink(t *testing.T) { |
| 16 | + table := NewLinkSlice() |
| 17 | + l := NewLink() |
| 18 | + l.SetTraceID(pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1})) |
| 19 | + l2 := NewLink() |
| 20 | + l.SetTraceID(pcommon.TraceID([16]byte{2, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 2})) |
| 21 | + smpl := NewSample() |
| 22 | + |
| 23 | + // Put a first link |
| 24 | + require.NoError(t, SetLink(table, smpl, l)) |
| 25 | + assert.Equal(t, 1, table.Len()) |
| 26 | + assert.Equal(t, int32(0), smpl.LinkIndex()) |
| 27 | + |
| 28 | + // Put the same link |
| 29 | + // This should be a no-op. |
| 30 | + require.NoError(t, SetLink(table, smpl, l)) |
| 31 | + assert.Equal(t, 1, table.Len()) |
| 32 | + assert.Equal(t, int32(0), smpl.LinkIndex()) |
| 33 | + |
| 34 | + // Set a new link |
| 35 | + // This sets the index and adds to the table. |
| 36 | + require.NoError(t, SetLink(table, smpl, l2)) |
| 37 | + assert.Equal(t, 2, table.Len()) |
| 38 | + assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115 |
| 39 | + |
| 40 | + // Set an existing link |
| 41 | + require.NoError(t, SetLink(table, smpl, l)) |
| 42 | + assert.Equal(t, 2, table.Len()) |
| 43 | + assert.Equal(t, int32(0), smpl.LinkIndex()) |
| 44 | + // Set another existing link |
| 45 | + require.NoError(t, SetLink(table, smpl, l2)) |
| 46 | + assert.Equal(t, 2, table.Len()) |
| 47 | + assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115 |
| 48 | +} |
| 49 | + |
| 50 | +func TestSetLinkCurrentTooHigh(t *testing.T) { |
| 51 | + table := NewLinkSlice() |
| 52 | + smpl := NewSample() |
| 53 | + smpl.SetLinkIndex(42) |
| 54 | + |
| 55 | + err := SetLink(table, smpl, NewLink()) |
| 56 | + require.Error(t, err) |
| 57 | + assert.Equal(t, 0, table.Len()) |
| 58 | + assert.Equal(t, int32(42), smpl.LinkIndex()) |
| 59 | +} |
| 60 | + |
| 61 | +func BenchmarkSetLink(b *testing.B) { |
| 62 | + for _, bb := range []struct { |
| 63 | + name string |
| 64 | + link Link |
| 65 | + |
| 66 | + runBefore func(*testing.B, LinkSlice, Sample) |
| 67 | + }{ |
| 68 | + { |
| 69 | + name: "with a new link", |
| 70 | + link: NewLink(), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "with an existing link", |
| 74 | + link: func() Link { |
| 75 | + l := NewLink() |
| 76 | + l.SetTraceID(pcommon.NewTraceIDEmpty()) |
| 77 | + return l |
| 78 | + }(), |
| 79 | + |
| 80 | + runBefore: func(_ *testing.B, table LinkSlice, _ Sample) { |
| 81 | + l := table.AppendEmpty() |
| 82 | + l.SetTraceID(pcommon.NewTraceIDEmpty()) |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "with a duplicate link", |
| 87 | + link: NewLink(), |
| 88 | + |
| 89 | + runBefore: func(_ *testing.B, table LinkSlice, obj Sample) { |
| 90 | + require.NoError(b, SetLink(table, obj, NewLink())) |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "with a hundred links to loop through", |
| 95 | + link: func() Link { |
| 96 | + l := NewLink() |
| 97 | + l.SetTraceID(pcommon.NewTraceIDEmpty()) |
| 98 | + return l |
| 99 | + }(), |
| 100 | + |
| 101 | + runBefore: func(_ *testing.B, table LinkSlice, _ Sample) { |
| 102 | + for range 100 { |
| 103 | + table.AppendEmpty() |
| 104 | + } |
| 105 | + }, |
| 106 | + }, |
| 107 | + } { |
| 108 | + b.Run(bb.name, func(b *testing.B) { |
| 109 | + table := NewLinkSlice() |
| 110 | + obj := NewSample() |
| 111 | + |
| 112 | + if bb.runBefore != nil { |
| 113 | + bb.runBefore(b, table, obj) |
| 114 | + } |
| 115 | + |
| 116 | + b.ResetTimer() |
| 117 | + b.ReportAllocs() |
| 118 | + |
| 119 | + for range b.N { |
| 120 | + _ = SetLink(table, obj, bb.link) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
0 commit comments