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