| 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 TestLocationEqual(t *testing.T) { |
| 17 |
for _, tt := range []struct { |
| 18 |
name string |
| 19 |
orig Location |
| 20 |
dest Location |
| 21 |
want bool |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "empty locations", |
| 25 |
orig: NewLocation(), |
| 26 |
dest: NewLocation(), |
| 27 |
want: true, |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "non-empty locations", |
| 31 |
orig: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 32 |
dest: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 33 |
want: true, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "with non-equal mapping index", |
| 37 |
orig: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 38 |
dest: buildLocation(2, 2, []int32{3}, buildLine(1, 2, 3)), |
| 39 |
want: false, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "with non-equal address", |
| 43 |
orig: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 44 |
dest: buildLocation(1, 3, []int32{3}, buildLine(1, 2, 3)), |
| 45 |
want: false, |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "with non-equal attribute indices", |
| 49 |
orig: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 50 |
dest: buildLocation(1, 2, []int32{5}, buildLine(1, 2, 3)), |
| 51 |
want: false, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "with non-equal lines", |
| 55 |
orig: buildLocation(1, 2, []int32{3}, buildLine(4, 5, 6)), |
| 56 |
dest: buildLocation(1, 2, []int32{3}, buildLine(1, 2, 3)), |
| 57 |
want: false, |
| 58 |
}, |
| 59 |
} { |
| 60 |
t.Run(tt.name, func(t *testing.T) { |
| 61 |
if tt.want { |
| 62 |
assert.True(t, tt.orig.Equal(tt.dest)) |
| 63 |
} else { |
| 64 |
assert.False(t, tt.orig.Equal(tt.dest)) |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestLocationSwitchDictionary(t *testing.T) { |
| 71 |
for _, tt := range []struct { |
| 72 |
name string |
| 73 |
location Location |
| 74 |
|
| 75 |
src ProfilesDictionary |
| 76 |
dst ProfilesDictionary |
| 77 |
|
| 78 |
wantLocation Location |
| 79 |
wantDictionary ProfilesDictionary |
| 80 |
wantErr error |
| 81 |
}{ |
| 82 |
{ |
| 83 |
name: "with an empty location", |
| 84 |
location: NewLocation(), |
| 85 |
|
| 86 |
src: NewProfilesDictionary(), |
| 87 |
dst: NewProfilesDictionary(), |
| 88 |
|
| 89 |
wantLocation: NewLocation(), |
| 90 |
wantDictionary: NewProfilesDictionary(), |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "with an existing mapping", |
| 94 |
location: func() Location { |
| 95 |
l := NewLocation() |
| 96 |
l.SetMappingIndex(1) |
| 97 |
return l |
| 98 |
}(), |
| 99 |
|
| 100 |
src: func() ProfilesDictionary { |
| 101 |
d := NewProfilesDictionary() |
| 102 |
d.StringTable().Append("", "test") |
| 103 |
|
| 104 |
d.MappingTable().AppendEmpty() |
| 105 |
m := d.MappingTable().AppendEmpty() |
| 106 |
m.SetFilenameStrindex(1) |
| 107 |
return d |
| 108 |
}(), |
| 109 |
dst: func() ProfilesDictionary { |
| 110 |
d := NewProfilesDictionary() |
| 111 |
d.StringTable().Append("", "foo") |
| 112 |
|
| 113 |
d.MappingTable().AppendEmpty() |
| 114 |
d.MappingTable().AppendEmpty() |
| 115 |
return d |
| 116 |
}(), |
| 117 |
|
| 118 |
wantLocation: func() Location { |
| 119 |
l := NewLocation() |
| 120 |
l.SetMappingIndex(2) |
| 121 |
return l |
| 122 |
}(), |
| 123 |
wantDictionary: func() ProfilesDictionary { |
| 124 |
d := NewProfilesDictionary() |
| 125 |
d.StringTable().Append("", "foo", "test") |
| 126 |
|
| 127 |
d.MappingTable().AppendEmpty() |
| 128 |
d.MappingTable().AppendEmpty() |
| 129 |
m := d.MappingTable().AppendEmpty() |
| 130 |
m.SetFilenameStrindex(2) |
| 131 |
return d |
| 132 |
}(), |
| 133 |
}, |
| 134 |
{ |
| 135 |
name: "with a mapping that cannot be found", |
| 136 |
location: func() Location { |
| 137 |
l := NewLocation() |
| 138 |
l.SetMappingIndex(1) |
| 139 |
return l |
| 140 |
}(), |
| 141 |
|
| 142 |
src: NewProfilesDictionary(), |
| 143 |
dst: NewProfilesDictionary(), |
| 144 |
|
| 145 |
wantLocation: func() Location { |
| 146 |
l := NewLocation() |
| 147 |
l.SetMappingIndex(1) |
| 148 |
return l |
| 149 |
}(), |
| 150 |
wantDictionary: NewProfilesDictionary(), |
| 151 |
wantErr: errors.New("invalid mapping index 1"), |
| 152 |
}, |
| 153 |
{ |
| 154 |
name: "with an existing attribute", |
| 155 |
location: func() Location { |
| 156 |
l := NewLocation() |
| 157 |
l.AttributeIndices().Append(1) |
| 158 |
return l |
| 159 |
}(), |
| 160 |
|
| 161 |
src: func() ProfilesDictionary { |
| 162 |
d := NewProfilesDictionary() |
| 163 |
d.StringTable().Append("", "test") |
| 164 |
|
| 165 |
d.AttributeTable().AppendEmpty() |
| 166 |
a := d.AttributeTable().AppendEmpty() |
| 167 |
a.SetKeyStrindex(1) |
| 168 |
|
| 169 |
return d |
| 170 |
}(), |
| 171 |
dst: func() ProfilesDictionary { |
| 172 |
d := NewProfilesDictionary() |
| 173 |
d.StringTable().Append("", "foo") |
| 174 |
|
| 175 |
d.AttributeTable().AppendEmpty() |
| 176 |
d.AttributeTable().AppendEmpty() |
| 177 |
return d |
| 178 |
}(), |
| 179 |
|
| 180 |
wantLocation: func() Location { |
| 181 |
l := NewLocation() |
| 182 |
l.AttributeIndices().Append(2) |
| 183 |
return l |
| 184 |
}(), |
| 185 |
wantDictionary: func() ProfilesDictionary { |
| 186 |
d := NewProfilesDictionary() |
| 187 |
d.StringTable().Append("", "foo", "test") |
| 188 |
|
| 189 |
d.AttributeTable().AppendEmpty() |
| 190 |
d.AttributeTable().AppendEmpty() |
| 191 |
a := d.AttributeTable().AppendEmpty() |
| 192 |
a.SetKeyStrindex(2) |
| 193 |
return d |
| 194 |
}(), |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "with an attribute index that does not match anything", |
| 198 |
location: func() Location { |
| 199 |
l := NewLocation() |
| 200 |
l.AttributeIndices().Append(1) |
| 201 |
return l |
| 202 |
}(), |
| 203 |
|
| 204 |
src: NewProfilesDictionary(), |
| 205 |
dst: NewProfilesDictionary(), |
| 206 |
|
| 207 |
wantLocation: func() Location { |
| 208 |
l := NewLocation() |
| 209 |
l.AttributeIndices().Append(1) |
| 210 |
return l |
| 211 |
}(), |
| 212 |
wantDictionary: NewProfilesDictionary(), |
| 213 |
wantErr: errors.New("invalid attribute index 1"), |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "with an existing line", |
| 217 |
location: func() Location { |
| 218 |
l := NewLocation() |
| 219 |
l.Lines().AppendEmpty().SetFunctionIndex(1) |
| 220 |
return l |
| 221 |
}(), |
| 222 |
|
| 223 |
src: func() ProfilesDictionary { |
| 224 |
d := NewProfilesDictionary() |
| 225 |
d.StringTable().Append("", "test") |
| 226 |
|
| 227 |
d.FunctionTable().AppendEmpty() |
| 228 |
f := d.FunctionTable().AppendEmpty() |
| 229 |
f.SetNameStrindex(1) |
| 230 |
|
| 231 |
return d |
| 232 |
}(), |
| 233 |
dst: func() ProfilesDictionary { |
| 234 |
d := NewProfilesDictionary() |
| 235 |
d.StringTable().Append("", "foo") |
| 236 |
|
| 237 |
d.FunctionTable().AppendEmpty() |
| 238 |
d.FunctionTable().AppendEmpty() |
| 239 |
return d |
| 240 |
}(), |
| 241 |
|
| 242 |
wantLocation: func() Location { |
| 243 |
l := NewLocation() |
| 244 |
l.Lines().AppendEmpty().SetFunctionIndex(2) |
| 245 |
return l |
| 246 |
}(), |
| 247 |
wantDictionary: func() ProfilesDictionary { |
| 248 |
d := NewProfilesDictionary() |
| 249 |
d.StringTable().Append("", "foo", "test") |
| 250 |
|
| 251 |
d.FunctionTable().AppendEmpty() |
| 252 |
d.FunctionTable().AppendEmpty() |
| 253 |
f := d.FunctionTable().AppendEmpty() |
| 254 |
f.SetNameStrindex(2) |
| 255 |
return d |
| 256 |
}(), |
| 257 |
}, |
| 258 |
} { |
| 259 |
t.Run(tt.name, func(t *testing.T) { |
| 260 |
l := tt.location |
| 261 |
dst := tt.dst |
| 262 |
err := l.switchDictionary(tt.src, dst) |
| 263 |
|
| 264 |
if tt.wantErr == nil { |
| 265 |
require.NoError(t, err) |
| 266 |
} else { |
| 267 |
require.Equal(t, tt.wantErr, err) |
| 268 |
} |
| 269 |
|
| 270 |
assert.Equal(t, tt.wantLocation, l) |
| 271 |
assert.Equal(t, tt.wantDictionary, dst) |
| 272 |
}) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func BenchmarkLocationSwitchDictionary(b *testing.B) { |
| 277 |
testutil.SkipMemoryBench(b) |
| 278 |
|
| 279 |
l := NewLocation() |
| 280 |
l.AttributeIndices().Append(1, 2) |
| 281 |
|
| 282 |
src := NewProfilesDictionary() |
| 283 |
src.StringTable().Append("", "test") |
| 284 |
src.AttributeTable().AppendEmpty() |
| 285 |
src.AttributeTable().AppendEmpty().SetKeyStrindex(1) |
| 286 |
src.AttributeTable().AppendEmpty().SetKeyStrindex(2) |
| 287 |
|
| 288 |
b.ReportAllocs() |
| 289 |
|
| 290 |
for b.Loop() { |
| 291 |
b.StopTimer() |
| 292 |
dst := NewProfilesDictionary() |
| 293 |
dst.StringTable().Append("", "foo") |
| 294 |
dst.AttributeTable().AppendEmpty() |
| 295 |
dst.AttributeTable().AppendEmpty().SetKeyStrindex(1) |
| 296 |
b.StartTimer() |
| 297 |
|
| 298 |
_ = l.switchDictionary(src, dst) |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
func buildLocation(mapIdx int32, addr uint64, attrIdxs []int32, line Line) Location { |
| 303 |
l := NewLocation() |
| 304 |
l.SetMappingIndex(mapIdx) |
| 305 |
l.SetAddress(addr) |
| 306 |
l.AttributeIndices().FromRaw(attrIdxs) |
| 307 |
line.MoveTo(l.Lines().AppendEmpty()) |
| 308 |
return l |
| 309 |
} |