| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofile |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/internal/testutil" |
| 14 |
) |
| 15 |
|
| 16 |
func TestStackEqual(t *testing.T) { |
| 17 |
for _, tt := range []struct { |
| 18 |
name string |
| 19 |
orig Stack |
| 20 |
dest Stack |
| 21 |
want bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "with empty stacks", |
| 25 |
orig: NewStack(), |
| 26 |
dest: NewStack(), |
| 27 |
want: true, |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "with non-empty equal stacks", |
| 31 |
orig: func() Stack { |
| 32 |
s := NewStack() |
| 33 |
s.LocationIndices().Append(1) |
| 34 |
return s |
| 35 |
}(), |
| 36 |
dest: func() Stack { |
| 37 |
s := NewStack() |
| 38 |
s.LocationIndices().Append(1) |
| 39 |
return s |
| 40 |
}(), |
| 41 |
want: true, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "with different location indices lengths", |
| 45 |
orig: func() Stack { |
| 46 |
s := NewStack() |
| 47 |
s.LocationIndices().Append(1) |
| 48 |
return s |
| 49 |
}(), |
| 50 |
dest: NewStack(), |
| 51 |
want: false, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "with non-equal location indices", |
| 55 |
orig: func() Stack { |
| 56 |
s := NewStack() |
| 57 |
s.LocationIndices().Append(2) |
| 58 |
return s |
| 59 |
}(), |
| 60 |
dest: func() Stack { |
| 61 |
s := NewStack() |
| 62 |
s.LocationIndices().Append(1) |
| 63 |
return s |
| 64 |
}(), |
| 65 |
want: false, |
| 66 |
}, |
| 67 |
} { |
| 68 |
t.Run(tt.name, func(t *testing.T) { |
| 69 |
if tt.want { |
| 70 |
assert.True(t, tt.orig.Equal(tt.dest)) |
| 71 |
} else { |
| 72 |
assert.False(t, tt.orig.Equal(tt.dest)) |
| 73 |
} |
| 74 |
}) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestStackSwitchDictionary(t *testing.T) { |
| 79 |
for _, tt := range []struct { |
| 80 |
name string |
| 81 |
stack Stack |
| 82 |
|
| 83 |
src ProfilesDictionary |
| 84 |
dst ProfilesDictionary |
| 85 |
|
| 86 |
wantStack Stack |
| 87 |
wantDictionary ProfilesDictionary |
| 88 |
wantErr error |
| 89 |
}{ |
| 90 |
{ |
| 91 |
name: "with an empty stack", |
| 92 |
stack: NewStack(), |
| 93 |
|
| 94 |
src: NewProfilesDictionary(), |
| 95 |
dst: NewProfilesDictionary(), |
| 96 |
|
| 97 |
wantStack: NewStack(), |
| 98 |
wantDictionary: NewProfilesDictionary(), |
| 99 |
}, |
| 100 |
{ |
| 101 |
name: "with an existing location", |
| 102 |
stack: func() Stack { |
| 103 |
s := NewStack() |
| 104 |
s.LocationIndices().Append(0) |
| 105 |
return s |
| 106 |
}(), |
| 107 |
|
| 108 |
src: func() ProfilesDictionary { |
| 109 |
d := NewProfilesDictionary() |
| 110 |
loc := d.LocationTable().AppendEmpty() |
| 111 |
loc.SetAddress(42) |
| 112 |
return d |
| 113 |
}(), |
| 114 |
dst: NewProfilesDictionary(), |
| 115 |
|
| 116 |
wantStack: func() Stack { |
| 117 |
s := NewStack() |
| 118 |
s.LocationIndices().Append(0) |
| 119 |
return s |
| 120 |
}(), |
| 121 |
wantDictionary: func() ProfilesDictionary { |
| 122 |
d := NewProfilesDictionary() |
| 123 |
loc := d.LocationTable().AppendEmpty() |
| 124 |
loc.SetAddress(42) |
| 125 |
return d |
| 126 |
}(), |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "with an existing location that needs a new indice", |
| 130 |
stack: func() Stack { |
| 131 |
s := NewStack() |
| 132 |
s.LocationIndices().Append(0) |
| 133 |
return s |
| 134 |
}(), |
| 135 |
|
| 136 |
src: func() ProfilesDictionary { |
| 137 |
d := NewProfilesDictionary() |
| 138 |
loc := d.LocationTable().AppendEmpty() |
| 139 |
loc.SetAddress(42) |
| 140 |
return d |
| 141 |
}(), |
| 142 |
dst: func() ProfilesDictionary { |
| 143 |
d := NewProfilesDictionary() |
| 144 |
loc := d.LocationTable().AppendEmpty() |
| 145 |
loc.SetAddress(2) |
| 146 |
return d |
| 147 |
}(), |
| 148 |
|
| 149 |
wantStack: func() Stack { |
| 150 |
s := NewStack() |
| 151 |
s.LocationIndices().Append(1) |
| 152 |
return s |
| 153 |
}(), |
| 154 |
wantDictionary: func() ProfilesDictionary { |
| 155 |
d := NewProfilesDictionary() |
| 156 |
loc := d.LocationTable().AppendEmpty() |
| 157 |
loc.SetAddress(2) |
| 158 |
loc = d.LocationTable().AppendEmpty() |
| 159 |
loc.SetAddress(42) |
| 160 |
return d |
| 161 |
}(), |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "with a location index that does not match anything", |
| 165 |
stack: func() Stack { |
| 166 |
s := NewStack() |
| 167 |
s.LocationIndices().Append(2) |
| 168 |
return s |
| 169 |
}(), |
| 170 |
|
| 171 |
src: NewProfilesDictionary(), |
| 172 |
dst: NewProfilesDictionary(), |
| 173 |
|
| 174 |
wantStack: func() Stack { |
| 175 |
s := NewStack() |
| 176 |
s.LocationIndices().Append(2) |
| 177 |
return s |
| 178 |
}(), |
| 179 |
wantDictionary: NewProfilesDictionary(), |
| 180 |
|
| 181 |
wantErr: errors.New("invalid location index 2"), |
| 182 |
}, |
| 183 |
} { |
| 184 |
t.Run(tt.name, func(t *testing.T) { |
| 185 |
stack := tt.stack |
| 186 |
dst := tt.dst |
| 187 |
err := stack.switchDictionary(tt.src, dst) |
| 188 |
|
| 189 |
if tt.wantErr == nil { |
| 190 |
require.NoError(t, err) |
| 191 |
} else { |
| 192 |
require.Equal(t, tt.wantErr, err) |
| 193 |
} |
| 194 |
|
| 195 |
assert.Equal(t, tt.wantStack, stack) |
| 196 |
assert.Equal(t, tt.wantDictionary, dst) |
| 197 |
}) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func BenchmarkStackSwitchDictionary(b *testing.B) { |
| 202 |
testutil.SkipMemoryBench(b) |
| 203 |
|
| 204 |
s := NewStack() |
| 205 |
s.LocationIndices().Append(1, 2) |
| 206 |
|
| 207 |
src := NewProfilesDictionary() |
| 208 |
src.LocationTable().AppendEmpty() |
| 209 |
src.LocationTable().AppendEmpty().SetAddress(42) |
| 210 |
src.LocationTable().AppendEmpty().SetAddress(43) |
| 211 |
|
| 212 |
b.ReportAllocs() |
| 213 |
|
| 214 |
for b.Loop() { |
| 215 |
b.StopTimer() |
| 216 |
dst := NewProfilesDictionary() |
| 217 |
dst.LocationTable().AppendEmpty() |
| 218 |
dst.LocationTable().AppendEmpty().SetAddress(43) |
| 219 |
b.StartTimer() |
| 220 |
|
| 221 |
_ = s.switchDictionary(src, dst) |
| 222 |
} |
| 223 |
} |