| 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 |
) |
| 14 |
|
| 15 |
func TestFromLocationIndices(t *testing.T) { |
| 16 |
table := NewLocationSlice() |
| 17 |
table.AppendEmpty().SetAddress(1) |
| 18 |
table.AppendEmpty().SetAddress(2) |
| 19 |
|
| 20 |
stack := NewStack() |
| 21 |
locs := FromLocationIndices(table, stack) |
| 22 |
assert.Equal(t, locs, NewLocationSlice()) |
| 23 |
|
| 24 |
// Add a location |
| 25 |
stack.LocationIndices().Append(0) |
| 26 |
locs = FromLocationIndices(table, stack) |
| 27 |
|
| 28 |
tLoc := NewLocationSlice() |
| 29 |
tLoc.AppendEmpty().SetAddress(1) |
| 30 |
assert.Equal(t, tLoc, locs) |
| 31 |
|
| 32 |
// Add another location |
| 33 |
stack.LocationIndices().Append(1) |
| 34 |
|
| 35 |
locs = FromLocationIndices(table, stack) |
| 36 |
assert.Equal(t, table, locs) |
| 37 |
} |
| 38 |
|
| 39 |
func TestSetLocation(t *testing.T) { |
| 40 |
table := NewLocationSlice() |
| 41 |
l := NewLocation() |
| 42 |
l.SetAddress(1) |
| 43 |
l2 := NewLocation() |
| 44 |
l2.SetAddress(2) |
| 45 |
|
| 46 |
// Put a first value |
| 47 |
idx, err := SetLocation(table, l) |
| 48 |
require.NoError(t, err) |
| 49 |
assert.Equal(t, 1, table.Len()) |
| 50 |
assert.Equal(t, int32(0), idx) |
| 51 |
|
| 52 |
// Put the same string |
| 53 |
// This should be a no-op. |
| 54 |
idx, err = SetLocation(table, l) |
| 55 |
require.NoError(t, err) |
| 56 |
assert.Equal(t, 1, table.Len()) |
| 57 |
assert.Equal(t, int32(0), idx) |
| 58 |
|
| 59 |
// Set a new value |
| 60 |
// This sets the index and adds to the table. |
| 61 |
idx, err = SetLocation(table, l2) |
| 62 |
require.NoError(t, err) |
| 63 |
assert.Equal(t, 2, table.Len()) |
| 64 |
assert.Equal(t, int32(table.Len()-1), idx) |
| 65 |
|
| 66 |
// Set an existing value |
| 67 |
idx, err = SetLocation(table, l) |
| 68 |
require.NoError(t, err) |
| 69 |
assert.Equal(t, 2, table.Len()) |
| 70 |
assert.Equal(t, int32(0), idx) |
| 71 |
// Set another existing value |
| 72 |
idx, err = SetLocation(table, l2) |
| 73 |
require.NoError(t, err) |
| 74 |
assert.Equal(t, 2, table.Len()) |
| 75 |
assert.Equal(t, int32(table.Len()-1), idx) |
| 76 |
} |
| 77 |
|
| 78 |
func BenchmarkFromLocationIndices(b *testing.B) { |
| 79 |
table := NewLocationSlice() |
| 80 |
|
| 81 |
for i := range 100 { |
| 82 |
table.AppendEmpty().SetAddress(uint64(i)) |
| 83 |
} |
| 84 |
|
| 85 |
obj := NewStack() |
| 86 |
for i := range int32(50) { |
| 87 |
obj.LocationIndices().Append(2*i + 1) |
| 88 |
} |
| 89 |
|
| 90 |
b.ReportAllocs() |
| 91 |
|
| 92 |
for b.Loop() { |
| 93 |
_ = FromLocationIndices(table, obj) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func BenchmarkSetLocation(b *testing.B) { |
| 98 |
testutil.SkipMemoryBench(b) |
| 99 |
for _, bb := range []struct { |
| 100 |
name string |
| 101 |
location Location |
| 102 |
|
| 103 |
runBefore func(*testing.B, LocationSlice) |
| 104 |
}{ |
| 105 |
{ |
| 106 |
name: "with a new location", |
| 107 |
location: NewLocation(), |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "with an existing location", |
| 111 |
location: func() Location { |
| 112 |
l := NewLocation() |
| 113 |
l.SetAddress(1) |
| 114 |
return l |
| 115 |
}(), |
| 116 |
|
| 117 |
runBefore: func(_ *testing.B, table LocationSlice) { |
| 118 |
l := table.AppendEmpty() |
| 119 |
l.SetAddress(1) |
| 120 |
}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: "with a duplicate location", |
| 124 |
location: NewLocation(), |
| 125 |
|
| 126 |
runBefore: func(_ *testing.B, table LocationSlice) { |
| 127 |
_, err := SetLocation(table, NewLocation()) |
| 128 |
require.NoError(b, err) |
| 129 |
}, |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "with a hundred locations to loop through", |
| 133 |
location: func() Location { |
| 134 |
l := NewLocation() |
| 135 |
l.SetMappingIndex(1) |
| 136 |
return l |
| 137 |
}(), |
| 138 |
|
| 139 |
runBefore: func(_ *testing.B, table LocationSlice) { |
| 140 |
for i := range 100 { |
| 141 |
l := table.AppendEmpty() |
| 142 |
l.SetAddress(uint64(i)) |
| 143 |
} |
| 144 |
|
| 145 |
l := table.AppendEmpty() |
| 146 |
l.SetMappingIndex(1) |
| 147 |
}, |
| 148 |
}, |
| 149 |
} { |
| 150 |
b.Run(bb.name, func(b *testing.B) { |
| 151 |
table := NewLocationSlice() |
| 152 |
|
| 153 |
if bb.runBefore != nil { |
| 154 |
bb.runBefore(b, table) |
| 155 |
} |
| 156 |
|
| 157 |
b.ResetTimer() |
| 158 |
b.ReportAllocs() |
| 159 |
|
| 160 |
for b.Loop() { |
| 161 |
_, _ = SetLocation(table, bb.location) |
| 162 |
} |
| 163 |
}) |
| 164 |
} |
| 165 |
} |