| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package attribute_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/attribute" |
| 10 |
) |
| 11 |
|
| 12 |
// Store results in a file scope var to ensure compiler does not optimize the |
| 13 |
// test away. |
| 14 |
var ( |
| 15 |
outV attribute.Value |
| 16 |
outKV attribute.KeyValue |
| 17 |
|
| 18 |
outBool bool |
| 19 |
outBoolSlice []bool |
| 20 |
outInt64 int64 |
| 21 |
outInt64Slice []int64 |
| 22 |
outFloat64 float64 |
| 23 |
outFloat64Slice []float64 |
| 24 |
outStr string |
| 25 |
outStrSlice []string |
| 26 |
) |
| 27 |
|
| 28 |
func benchmarkEmit(kv attribute.KeyValue) func(*testing.B) { |
| 29 |
return func(b *testing.B) { |
| 30 |
b.ReportAllocs() |
| 31 |
for i := 0; i < b.N; i++ { |
| 32 |
outStr = kv.Value.Emit() |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func BenchmarkBool(b *testing.B) { |
| 38 |
k, v := "bool", true |
| 39 |
kv := attribute.Bool(k, v) |
| 40 |
|
| 41 |
b.Run("Value", func(b *testing.B) { |
| 42 |
b.ReportAllocs() |
| 43 |
for i := 0; i < b.N; i++ { |
| 44 |
outV = attribute.BoolValue(v) |
| 45 |
} |
| 46 |
}) |
| 47 |
b.Run("KeyValue", func(b *testing.B) { |
| 48 |
b.ReportAllocs() |
| 49 |
for i := 0; i < b.N; i++ { |
| 50 |
outKV = attribute.Bool(k, v) |
| 51 |
} |
| 52 |
}) |
| 53 |
b.Run("AsBool", func(b *testing.B) { |
| 54 |
b.ReportAllocs() |
| 55 |
for i := 0; i < b.N; i++ { |
| 56 |
outBool = kv.Value.AsBool() |
| 57 |
} |
| 58 |
}) |
| 59 |
b.Run("Emit", benchmarkEmit(kv)) |
| 60 |
} |
| 61 |
|
| 62 |
func BenchmarkBoolSlice(b *testing.B) { |
| 63 |
for _, bench := range []struct { |
| 64 |
name string |
| 65 |
v []bool |
| 66 |
}{ |
| 67 |
{name: "Len2", v: []bool{true, false}}, |
| 68 |
{name: "Len8", v: []bool{true, false, true, false, true, false, true, false}}, |
| 69 |
} { |
| 70 |
b.Run(bench.name, func(b *testing.B) { |
| 71 |
k, v := "bool slice", bench.v |
| 72 |
kv := attribute.BoolSlice(k, v) |
| 73 |
|
| 74 |
b.Run("Value", func(b *testing.B) { |
| 75 |
b.ReportAllocs() |
| 76 |
for i := 0; i < b.N; i++ { |
| 77 |
outV = attribute.BoolSliceValue(v) |
| 78 |
} |
| 79 |
}) |
| 80 |
b.Run("KeyValue", func(b *testing.B) { |
| 81 |
b.ReportAllocs() |
| 82 |
for i := 0; i < b.N; i++ { |
| 83 |
outKV = attribute.BoolSlice(k, v) |
| 84 |
} |
| 85 |
}) |
| 86 |
b.Run("AsBoolSlice", func(b *testing.B) { |
| 87 |
b.ReportAllocs() |
| 88 |
for i := 0; i < b.N; i++ { |
| 89 |
outBoolSlice = kv.Value.AsBoolSlice() |
| 90 |
} |
| 91 |
}) |
| 92 |
b.Run("Emit", benchmarkEmit(kv)) |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func BenchmarkInt(b *testing.B) { |
| 98 |
k, v := "int", int(42) |
| 99 |
kv := attribute.Int(k, v) |
| 100 |
|
| 101 |
b.Run("Value", func(b *testing.B) { |
| 102 |
b.ReportAllocs() |
| 103 |
for i := 0; i < b.N; i++ { |
| 104 |
outV = attribute.IntValue(v) |
| 105 |
} |
| 106 |
}) |
| 107 |
b.Run("KeyValue", func(b *testing.B) { |
| 108 |
b.ReportAllocs() |
| 109 |
for i := 0; i < b.N; i++ { |
| 110 |
outKV = attribute.Int(k, v) |
| 111 |
} |
| 112 |
}) |
| 113 |
b.Run("Emit", benchmarkEmit(kv)) |
| 114 |
} |
| 115 |
|
| 116 |
func BenchmarkIntSlice(b *testing.B) { |
| 117 |
for _, bench := range []struct { |
| 118 |
name string |
| 119 |
v []int |
| 120 |
}{ |
| 121 |
{name: "Len2", v: []int{42, -3}}, |
| 122 |
{name: "Len8", v: []int{42, -3, 12, 7, 9, 11, -5, 0}}, |
| 123 |
} { |
| 124 |
b.Run(bench.name, func(b *testing.B) { |
| 125 |
k, v := "int slice", bench.v |
| 126 |
kv := attribute.IntSlice(k, v) |
| 127 |
|
| 128 |
b.Run("Value", func(b *testing.B) { |
| 129 |
b.ReportAllocs() |
| 130 |
for i := 0; i < b.N; i++ { |
| 131 |
outV = attribute.IntSliceValue(v) |
| 132 |
} |
| 133 |
}) |
| 134 |
b.Run("KeyValue", func(b *testing.B) { |
| 135 |
b.ReportAllocs() |
| 136 |
for i := 0; i < b.N; i++ { |
| 137 |
outKV = attribute.IntSlice(k, v) |
| 138 |
} |
| 139 |
}) |
| 140 |
b.Run("Emit", benchmarkEmit(kv)) |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func BenchmarkInt64(b *testing.B) { |
| 146 |
k, v := "int64", int64(42) |
| 147 |
kv := attribute.Int64(k, v) |
| 148 |
|
| 149 |
b.Run("Value", func(b *testing.B) { |
| 150 |
b.ReportAllocs() |
| 151 |
for i := 0; i < b.N; i++ { |
| 152 |
outV = attribute.Int64Value(v) |
| 153 |
} |
| 154 |
}) |
| 155 |
b.Run("KeyValue", func(b *testing.B) { |
| 156 |
b.ReportAllocs() |
| 157 |
for i := 0; i < b.N; i++ { |
| 158 |
outKV = attribute.Int64(k, v) |
| 159 |
} |
| 160 |
}) |
| 161 |
b.Run("AsInt64", func(b *testing.B) { |
| 162 |
b.ReportAllocs() |
| 163 |
for i := 0; i < b.N; i++ { |
| 164 |
outInt64 = kv.Value.AsInt64() |
| 165 |
} |
| 166 |
}) |
| 167 |
b.Run("Emit", benchmarkEmit(kv)) |
| 168 |
} |
| 169 |
|
| 170 |
func BenchmarkInt64Slice(b *testing.B) { |
| 171 |
for _, bench := range []struct { |
| 172 |
name string |
| 173 |
v []int64 |
| 174 |
}{ |
| 175 |
{name: "Len2", v: []int64{42, -3}}, |
| 176 |
{name: "Len8", v: []int64{42, -3, 12, 7, 9, 11, -5, 0}}, |
| 177 |
} { |
| 178 |
b.Run(bench.name, func(b *testing.B) { |
| 179 |
k, v := "int64 slice", bench.v |
| 180 |
kv := attribute.Int64Slice(k, v) |
| 181 |
|
| 182 |
b.Run("Value", func(b *testing.B) { |
| 183 |
b.ReportAllocs() |
| 184 |
for i := 0; i < b.N; i++ { |
| 185 |
outV = attribute.Int64SliceValue(v) |
| 186 |
} |
| 187 |
}) |
| 188 |
b.Run("KeyValue", func(b *testing.B) { |
| 189 |
b.ReportAllocs() |
| 190 |
for i := 0; i < b.N; i++ { |
| 191 |
outKV = attribute.Int64Slice(k, v) |
| 192 |
} |
| 193 |
}) |
| 194 |
b.Run("AsInt64Slice", func(b *testing.B) { |
| 195 |
b.ReportAllocs() |
| 196 |
for i := 0; i < b.N; i++ { |
| 197 |
outInt64Slice = kv.Value.AsInt64Slice() |
| 198 |
} |
| 199 |
}) |
| 200 |
b.Run("Emit", benchmarkEmit(kv)) |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func BenchmarkFloat64(b *testing.B) { |
| 206 |
k, v := "float64", float64(42) |
| 207 |
kv := attribute.Float64(k, v) |
| 208 |
|
| 209 |
b.Run("Value", func(b *testing.B) { |
| 210 |
b.ReportAllocs() |
| 211 |
for i := 0; i < b.N; i++ { |
| 212 |
outV = attribute.Float64Value(v) |
| 213 |
} |
| 214 |
}) |
| 215 |
b.Run("KeyValue", func(b *testing.B) { |
| 216 |
b.ReportAllocs() |
| 217 |
for i := 0; i < b.N; i++ { |
| 218 |
outKV = attribute.Float64(k, v) |
| 219 |
} |
| 220 |
}) |
| 221 |
b.Run("AsFloat64", func(b *testing.B) { |
| 222 |
b.ReportAllocs() |
| 223 |
for i := 0; i < b.N; i++ { |
| 224 |
outFloat64 = kv.Value.AsFloat64() |
| 225 |
} |
| 226 |
}) |
| 227 |
b.Run("Emit", benchmarkEmit(kv)) |
| 228 |
} |
| 229 |
|
| 230 |
func BenchmarkFloat64Slice(b *testing.B) { |
| 231 |
for _, bench := range []struct { |
| 232 |
name string |
| 233 |
v []float64 |
| 234 |
}{ |
| 235 |
{name: "Len2", v: []float64{42, -3}}, |
| 236 |
{name: "Len8", v: []float64{42, -3, 12, 7, 9, 11, -5, 0}}, |
| 237 |
} { |
| 238 |
b.Run(bench.name, func(b *testing.B) { |
| 239 |
k, v := "float64 slice", bench.v |
| 240 |
kv := attribute.Float64Slice(k, v) |
| 241 |
|
| 242 |
b.Run("Value", func(b *testing.B) { |
| 243 |
b.ReportAllocs() |
| 244 |
for i := 0; i < b.N; i++ { |
| 245 |
outV = attribute.Float64SliceValue(v) |
| 246 |
} |
| 247 |
}) |
| 248 |
b.Run("KeyValue", func(b *testing.B) { |
| 249 |
b.ReportAllocs() |
| 250 |
for i := 0; i < b.N; i++ { |
| 251 |
outKV = attribute.Float64Slice(k, v) |
| 252 |
} |
| 253 |
}) |
| 254 |
b.Run("AsFloat64Slice", func(b *testing.B) { |
| 255 |
b.ReportAllocs() |
| 256 |
for i := 0; i < b.N; i++ { |
| 257 |
outFloat64Slice = kv.Value.AsFloat64Slice() |
| 258 |
} |
| 259 |
}) |
| 260 |
b.Run("Emit", benchmarkEmit(kv)) |
| 261 |
}) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func BenchmarkString(b *testing.B) { |
| 266 |
k, v := "string", "42" |
| 267 |
kv := attribute.String(k, v) |
| 268 |
|
| 269 |
b.Run("Value", func(b *testing.B) { |
| 270 |
b.ReportAllocs() |
| 271 |
for i := 0; i < b.N; i++ { |
| 272 |
outV = attribute.StringValue(v) |
| 273 |
} |
| 274 |
}) |
| 275 |
b.Run("KeyValue", func(b *testing.B) { |
| 276 |
b.ReportAllocs() |
| 277 |
for i := 0; i < b.N; i++ { |
| 278 |
outKV = attribute.String(k, v) |
| 279 |
} |
| 280 |
}) |
| 281 |
b.Run("AsString", func(b *testing.B) { |
| 282 |
b.ReportAllocs() |
| 283 |
for i := 0; i < b.N; i++ { |
| 284 |
outStr = kv.Value.AsString() |
| 285 |
} |
| 286 |
}) |
| 287 |
b.Run("Emit", benchmarkEmit(kv)) |
| 288 |
} |
| 289 |
|
| 290 |
func BenchmarkStringSlice(b *testing.B) { |
| 291 |
for _, bench := range []struct { |
| 292 |
name string |
| 293 |
v []string |
| 294 |
}{ |
| 295 |
{name: "Len2", v: []string{"forty-two", "negative three"}}, |
| 296 |
{name: "Len8", v: []string{"forty-two", "negative three", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen"}}, |
| 297 |
} { |
| 298 |
b.Run(bench.name, func(b *testing.B) { |
| 299 |
k, v := "string slice", bench.v |
| 300 |
kv := attribute.StringSlice(k, v) |
| 301 |
|
| 302 |
b.Run("Value", func(b *testing.B) { |
| 303 |
b.ReportAllocs() |
| 304 |
for i := 0; i < b.N; i++ { |
| 305 |
outV = attribute.StringSliceValue(v) |
| 306 |
} |
| 307 |
}) |
| 308 |
b.Run("KeyValue", func(b *testing.B) { |
| 309 |
b.ReportAllocs() |
| 310 |
for i := 0; i < b.N; i++ { |
| 311 |
outKV = attribute.StringSlice(k, v) |
| 312 |
} |
| 313 |
}) |
| 314 |
b.Run("AsStringSlice", func(b *testing.B) { |
| 315 |
b.ReportAllocs() |
| 316 |
for i := 0; i < b.N; i++ { |
| 317 |
outStrSlice = kv.Value.AsStringSlice() |
| 318 |
} |
| 319 |
}) |
| 320 |
b.Run("Emit", benchmarkEmit(kv)) |
| 321 |
}) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func BenchmarkSetEquals(b *testing.B) { |
| 326 |
b.Run("Empty", func(b *testing.B) { |
| 327 |
benchmarkSetEquals(b, attribute.EmptySet()) |
| 328 |
}) |
| 329 |
b.Run("1 string attribute", func(b *testing.B) { |
| 330 |
set := attribute.NewSet(attribute.String("string", "42")) |
| 331 |
benchmarkSetEquals(b, &set) |
| 332 |
}) |
| 333 |
b.Run("10 string attributes", func(b *testing.B) { |
| 334 |
set := attribute.NewSet( |
| 335 |
attribute.String("a", "42"), |
| 336 |
attribute.String("b", "42"), |
| 337 |
attribute.String("c", "42"), |
| 338 |
attribute.String("d", "42"), |
| 339 |
attribute.String("e", "42"), |
| 340 |
attribute.String("f", "42"), |
| 341 |
attribute.String("g", "42"), |
| 342 |
attribute.String("h", "42"), |
| 343 |
attribute.String("i", "42"), |
| 344 |
attribute.String("j", "42"), |
| 345 |
) |
| 346 |
benchmarkSetEquals(b, &set) |
| 347 |
}) |
| 348 |
b.Run("1 int attribute", func(b *testing.B) { |
| 349 |
set := attribute.NewSet(attribute.Int("string", 42)) |
| 350 |
benchmarkSetEquals(b, &set) |
| 351 |
}) |
| 352 |
b.Run("10 int attributes", func(b *testing.B) { |
| 353 |
set := attribute.NewSet( |
| 354 |
attribute.Int("a", 42), |
| 355 |
attribute.Int("b", 42), |
| 356 |
attribute.Int("c", 42), |
| 357 |
attribute.Int("d", 42), |
| 358 |
attribute.Int("e", 42), |
| 359 |
attribute.Int("f", 42), |
| 360 |
attribute.Int("g", 42), |
| 361 |
attribute.Int("h", 42), |
| 362 |
attribute.Int("i", 42), |
| 363 |
attribute.Int("j", 42), |
| 364 |
) |
| 365 |
benchmarkSetEquals(b, &set) |
| 366 |
}) |
| 367 |
} |
| 368 |
|
| 369 |
func benchmarkSetEquals(b *testing.B, set *attribute.Set) { |
| 370 |
b.ResetTimer() |
| 371 |
for range b.N { |
| 372 |
if !set.Equals(set) { |
| 373 |
b.Fatal("not equal") |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
// BenchmarkEquivalentMapAccess measures how expensive it is to use |
| 379 |
// Equivalent() as a map key. This is on the hot path for making synchronous |
| 380 |
// measurements on the metrics API/SDK. It will likely be on the hot path for |
| 381 |
// the trace and logs API/SDK in the future. |
| 382 |
func BenchmarkEquivalentMapAccess(b *testing.B) { |
| 383 |
b.Run("Empty", func(b *testing.B) { |
| 384 |
benchmarkEquivalentMapAccess(b, attribute.EmptySet()) |
| 385 |
}) |
| 386 |
b.Run("1 string attribute", func(b *testing.B) { |
| 387 |
set := attribute.NewSet(attribute.String("string", "42")) |
| 388 |
benchmarkEquivalentMapAccess(b, &set) |
| 389 |
}) |
| 390 |
b.Run("10 string attributes", func(b *testing.B) { |
| 391 |
set := attribute.NewSet( |
| 392 |
attribute.String("a", "42"), |
| 393 |
attribute.String("b", "42"), |
| 394 |
attribute.String("c", "42"), |
| 395 |
attribute.String("d", "42"), |
| 396 |
attribute.String("e", "42"), |
| 397 |
attribute.String("f", "42"), |
| 398 |
attribute.String("g", "42"), |
| 399 |
attribute.String("h", "42"), |
| 400 |
attribute.String("i", "42"), |
| 401 |
attribute.String("j", "42"), |
| 402 |
) |
| 403 |
benchmarkEquivalentMapAccess(b, &set) |
| 404 |
}) |
| 405 |
b.Run("1 int attribute", func(b *testing.B) { |
| 406 |
set := attribute.NewSet(attribute.Int("string", 42)) |
| 407 |
benchmarkEquivalentMapAccess(b, &set) |
| 408 |
}) |
| 409 |
b.Run("10 int attributes", func(b *testing.B) { |
| 410 |
set := attribute.NewSet( |
| 411 |
attribute.Int("a", 42), |
| 412 |
attribute.Int("b", 42), |
| 413 |
attribute.Int("c", 42), |
| 414 |
attribute.Int("d", 42), |
| 415 |
attribute.Int("e", 42), |
| 416 |
attribute.Int("f", 42), |
| 417 |
attribute.Int("g", 42), |
| 418 |
attribute.Int("h", 42), |
| 419 |
attribute.Int("i", 42), |
| 420 |
attribute.Int("j", 42), |
| 421 |
) |
| 422 |
benchmarkEquivalentMapAccess(b, &set) |
| 423 |
}) |
| 424 |
} |
| 425 |
|
| 426 |
func benchmarkEquivalentMapAccess(b *testing.B, set *attribute.Set) { |
| 427 |
values := map[attribute.Distinct]int{} |
| 428 |
b.ResetTimer() |
| 429 |
for range b.N { |
| 430 |
values[set.Equivalent()]++ |
| 431 |
} |
| 432 |
} |