| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/go-logr/logr" |
| 13 |
"github.com/go-logr/logr/funcr" |
| 14 |
|
| 15 |
"go.opentelemetry.io/otel/attribute" |
| 16 |
"go.opentelemetry.io/otel/internal/global" |
| 17 |
sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 18 |
"go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 19 |
"go.opentelemetry.io/otel/trace" |
| 20 |
) |
| 21 |
|
| 22 |
func benchmarkSpanLimits(b *testing.B, limits sdktrace.SpanLimits) { |
| 23 |
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanLimits(limits)) |
| 24 |
tracer := tp.Tracer(b.Name()) |
| 25 |
ctx := b.Context() |
| 26 |
|
| 27 |
const count = 8 |
| 28 |
|
| 29 |
attrs := []attribute.KeyValue{ |
| 30 |
attribute.Bool("bool", true), |
| 31 |
attribute.BoolSlice("boolSlice", []bool{true, false}), |
| 32 |
attribute.Int("int", 42), |
| 33 |
attribute.IntSlice("intSlice", []int{42, -1}), |
| 34 |
attribute.Int64("int64", 42), |
| 35 |
attribute.Int64Slice("int64Slice", []int64{42, -1}), |
| 36 |
attribute.Float64("float64", 42), |
| 37 |
attribute.Float64Slice("float64Slice", []float64{42, -1}), |
| 38 |
attribute.String("string", "value"), |
| 39 |
attribute.StringSlice("stringSlice", []string{"value", "value-1"}), |
| 40 |
} |
| 41 |
|
| 42 |
links := make([]trace.Link, count) |
| 43 |
for i := range links { |
| 44 |
links[i] = trace.Link{ |
| 45 |
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{ |
| 46 |
TraceID: [16]byte{0x01}, |
| 47 |
SpanID: [8]byte{0x01}, |
| 48 |
}), |
| 49 |
Attributes: attrs, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
events := make([]struct { |
| 54 |
name string |
| 55 |
attr []attribute.KeyValue |
| 56 |
}, count) |
| 57 |
for i := range events { |
| 58 |
events[i] = struct { |
| 59 |
name string |
| 60 |
attr []attribute.KeyValue |
| 61 |
}{ |
| 62 |
name: fmt.Sprintf("event-%d", i), |
| 63 |
attr: attrs, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
b.ReportAllocs() |
| 68 |
b.ResetTimer() |
| 69 |
|
| 70 |
for i := 0; i < b.N; i++ { |
| 71 |
_, span := tracer.Start(ctx, "span-name", trace.WithLinks(links...)) |
| 72 |
span.SetAttributes(attrs...) |
| 73 |
for _, e := range events { |
| 74 |
span.AddEvent(e.name, trace.WithAttributes(e.attr...)) |
| 75 |
} |
| 76 |
span.End() |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func BenchmarkSpanLimits(b *testing.B) { |
| 81 |
b.Run("AttributeValueLengthLimit", func(b *testing.B) { |
| 82 |
limits := sdktrace.NewSpanLimits() |
| 83 |
limits.AttributeValueLengthLimit = 2 |
| 84 |
benchmarkSpanLimits(b, limits) |
| 85 |
}) |
| 86 |
|
| 87 |
b.Run("AttributeCountLimit", func(b *testing.B) { |
| 88 |
limits := sdktrace.NewSpanLimits() |
| 89 |
limits.AttributeCountLimit = 1 |
| 90 |
benchmarkSpanLimits(b, limits) |
| 91 |
}) |
| 92 |
|
| 93 |
b.Run("EventCountLimit", func(b *testing.B) { |
| 94 |
limits := sdktrace.NewSpanLimits() |
| 95 |
limits.EventCountLimit = 1 |
| 96 |
benchmarkSpanLimits(b, limits) |
| 97 |
}) |
| 98 |
|
| 99 |
b.Run("LinkCountLimit", func(b *testing.B) { |
| 100 |
limits := sdktrace.NewSpanLimits() |
| 101 |
limits.LinkCountLimit = 1 |
| 102 |
benchmarkSpanLimits(b, limits) |
| 103 |
}) |
| 104 |
|
| 105 |
b.Run("AttributePerEventCountLimit", func(b *testing.B) { |
| 106 |
limits := sdktrace.NewSpanLimits() |
| 107 |
limits.AttributePerEventCountLimit = 1 |
| 108 |
benchmarkSpanLimits(b, limits) |
| 109 |
}) |
| 110 |
|
| 111 |
b.Run("AttributePerLinkCountLimit", func(b *testing.B) { |
| 112 |
limits := sdktrace.NewSpanLimits() |
| 113 |
limits.AttributePerLinkCountLimit = 1 |
| 114 |
benchmarkSpanLimits(b, limits) |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func BenchmarkSpanSetAttributesOverCapacity(b *testing.B) { |
| 119 |
limits := sdktrace.NewSpanLimits() |
| 120 |
limits.AttributeCountLimit = 1 |
| 121 |
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanLimits(limits)) |
| 122 |
tracer := tp.Tracer("BenchmarkSpanSetAttributesOverCapacity") |
| 123 |
ctx := b.Context() |
| 124 |
attrs := make([]attribute.KeyValue, 128) |
| 125 |
for i := range attrs { |
| 126 |
key := fmt.Sprintf("key-%d", i) |
| 127 |
attrs[i] = attribute.Bool(key, true) |
| 128 |
} |
| 129 |
|
| 130 |
b.ReportAllocs() |
| 131 |
b.ResetTimer() |
| 132 |
|
| 133 |
for i := 0; i < b.N; i++ { |
| 134 |
_, span := tracer.Start(ctx, "/foo") |
| 135 |
span.SetAttributes(attrs...) |
| 136 |
span.End() |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func BenchmarkStartEndSpan(b *testing.B) { |
| 141 |
traceBenchmark(b, "Benchmark StartEndSpan", func(b *testing.B, t trace.Tracer) { |
| 142 |
ctx := b.Context() |
| 143 |
b.ResetTimer() |
| 144 |
for i := 0; i < b.N; i++ { |
| 145 |
_, span := t.Start(ctx, "/foo") |
| 146 |
span.End() |
| 147 |
} |
| 148 |
}) |
| 149 |
} |
| 150 |
|
| 151 |
func BenchmarkSpanWithAttributes_4(b *testing.B) { |
| 152 |
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t trace.Tracer) { |
| 153 |
ctx := b.Context() |
| 154 |
b.ResetTimer() |
| 155 |
|
| 156 |
for i := 0; i < b.N; i++ { |
| 157 |
_, span := t.Start(ctx, "/foo") |
| 158 |
span.SetAttributes( |
| 159 |
attribute.Bool("key1", false), |
| 160 |
attribute.String("key2", "hello"), |
| 161 |
attribute.Int64("key3", 123), |
| 162 |
attribute.Float64("key4", 123.456), |
| 163 |
) |
| 164 |
span.End() |
| 165 |
} |
| 166 |
}) |
| 167 |
} |
| 168 |
|
| 169 |
func BenchmarkSpanWithAttributes_8(b *testing.B) { |
| 170 |
traceBenchmark(b, "Benchmark Start With 8 Attributes", func(b *testing.B, t trace.Tracer) { |
| 171 |
ctx := b.Context() |
| 172 |
b.ResetTimer() |
| 173 |
|
| 174 |
for i := 0; i < b.N; i++ { |
| 175 |
_, span := t.Start(ctx, "/foo") |
| 176 |
span.SetAttributes( |
| 177 |
attribute.Bool("key1", false), |
| 178 |
attribute.String("key2", "hello"), |
| 179 |
attribute.Int64("key3", 123), |
| 180 |
attribute.Float64("key4", 123.456), |
| 181 |
attribute.Bool("key21", false), |
| 182 |
attribute.String("key22", "hello"), |
| 183 |
attribute.Int64("key23", 123), |
| 184 |
attribute.Float64("key24", 123.456), |
| 185 |
) |
| 186 |
span.End() |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
func BenchmarkSpanWithAttributes_all(b *testing.B) { |
| 192 |
traceBenchmark(b, "Benchmark Start With all Attribute types", func(b *testing.B, t trace.Tracer) { |
| 193 |
ctx := b.Context() |
| 194 |
b.ResetTimer() |
| 195 |
|
| 196 |
for i := 0; i < b.N; i++ { |
| 197 |
_, span := t.Start(ctx, "/foo") |
| 198 |
span.SetAttributes( |
| 199 |
attribute.Bool("key1", false), |
| 200 |
attribute.String("key2", "hello"), |
| 201 |
attribute.Int64("key3", 123), |
| 202 |
attribute.Float64("key7", 123.456), |
| 203 |
attribute.Int("key9", 123), |
| 204 |
) |
| 205 |
span.End() |
| 206 |
} |
| 207 |
}) |
| 208 |
} |
| 209 |
|
| 210 |
func BenchmarkSpanWithAttributes_all_2x(b *testing.B) { |
| 211 |
traceBenchmark(b, "Benchmark Start With all Attributes types twice", func(b *testing.B, t trace.Tracer) { |
| 212 |
ctx := b.Context() |
| 213 |
b.ResetTimer() |
| 214 |
|
| 215 |
for i := 0; i < b.N; i++ { |
| 216 |
_, span := t.Start(ctx, "/foo") |
| 217 |
span.SetAttributes( |
| 218 |
attribute.Bool("key1", false), |
| 219 |
attribute.String("key2", "hello"), |
| 220 |
attribute.Int64("key3", 123), |
| 221 |
attribute.Float64("key7", 123.456), |
| 222 |
attribute.Int("key10", 123), |
| 223 |
attribute.Bool("key21", false), |
| 224 |
attribute.String("key22", "hello"), |
| 225 |
attribute.Int64("key23", 123), |
| 226 |
attribute.Float64("key27", 123.456), |
| 227 |
attribute.Int("key210", 123), |
| 228 |
) |
| 229 |
span.End() |
| 230 |
} |
| 231 |
}) |
| 232 |
} |
| 233 |
|
| 234 |
func BenchmarkSpanWithEvents_4(b *testing.B) { |
| 235 |
traceBenchmark(b, "Benchmark Start With 4 Events", func(b *testing.B, t trace.Tracer) { |
| 236 |
ctx := b.Context() |
| 237 |
b.ResetTimer() |
| 238 |
|
| 239 |
for i := 0; i < b.N; i++ { |
| 240 |
_, span := t.Start(ctx, "/foo") |
| 241 |
span.AddEvent("event1") |
| 242 |
span.AddEvent("event2") |
| 243 |
span.AddEvent("event3") |
| 244 |
span.AddEvent("event4") |
| 245 |
span.End() |
| 246 |
} |
| 247 |
}) |
| 248 |
} |
| 249 |
|
| 250 |
func BenchmarkSpanWithEvents_8(b *testing.B) { |
| 251 |
traceBenchmark(b, "Benchmark Start With 4 Events", func(b *testing.B, t trace.Tracer) { |
| 252 |
ctx := b.Context() |
| 253 |
b.ResetTimer() |
| 254 |
|
| 255 |
for i := 0; i < b.N; i++ { |
| 256 |
_, span := t.Start(ctx, "/foo") |
| 257 |
span.AddEvent("event1") |
| 258 |
span.AddEvent("event2") |
| 259 |
span.AddEvent("event3") |
| 260 |
span.AddEvent("event4") |
| 261 |
span.AddEvent("event5") |
| 262 |
span.AddEvent("event6") |
| 263 |
span.AddEvent("event7") |
| 264 |
span.AddEvent("event8") |
| 265 |
span.End() |
| 266 |
} |
| 267 |
}) |
| 268 |
} |
| 269 |
|
| 270 |
func BenchmarkSpanWithEvents_WithStackTrace(b *testing.B) { |
| 271 |
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t trace.Tracer) { |
| 272 |
ctx := b.Context() |
| 273 |
b.ResetTimer() |
| 274 |
|
| 275 |
for i := 0; i < b.N; i++ { |
| 276 |
_, span := t.Start(ctx, "/foo") |
| 277 |
span.AddEvent("event1", trace.WithStackTrace(true)) |
| 278 |
span.End() |
| 279 |
} |
| 280 |
}) |
| 281 |
} |
| 282 |
|
| 283 |
func BenchmarkSpanWithEvents_WithTimestamp(b *testing.B) { |
| 284 |
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t trace.Tracer) { |
| 285 |
ctx := b.Context() |
| 286 |
b.ResetTimer() |
| 287 |
|
| 288 |
for i := 0; i < b.N; i++ { |
| 289 |
_, span := t.Start(ctx, "/foo") |
| 290 |
span.AddEvent("event1", trace.WithTimestamp(time.Unix(0, 0))) |
| 291 |
span.End() |
| 292 |
} |
| 293 |
}) |
| 294 |
} |
| 295 |
|
| 296 |
func BenchmarkTraceIDFromHex(b *testing.B) { |
| 297 |
want := trace.TraceID{ |
| 298 |
0xde, |
| 299 |
0xad, |
| 300 |
0xbe, |
| 301 |
0xef, |
| 302 |
0x01, |
| 303 |
0x23, |
| 304 |
0x45, |
| 305 |
0x67, |
| 306 |
0x89, |
| 307 |
0xab, |
| 308 |
0xcd, |
| 309 |
0xef, |
| 310 |
0x01, |
| 311 |
0x23, |
| 312 |
0x45, |
| 313 |
0x67, |
| 314 |
} |
| 315 |
b.ReportAllocs() |
| 316 |
for i := 0; i < b.N; i++ { |
| 317 |
got, _ := trace.TraceIDFromHex("deadbeef0123456789abcdef01234567") |
| 318 |
if got != want { |
| 319 |
b.Fatalf("got = %q want = %q", got.String(), want) |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func BenchmarkSpanIDFromHex(b *testing.B) { |
| 325 |
want := trace.SpanID{0xde, 0xad, 0xbe, 0xef, 0x01, 0x23, 0x45, 0x67} |
| 326 |
b.ReportAllocs() |
| 327 |
for i := 0; i < b.N; i++ { |
| 328 |
got, _ := trace.SpanIDFromHex("deadbeef01234567") |
| 329 |
if got != want { |
| 330 |
b.Fatalf("got = %q want = %q", got.String(), want) |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func BenchmarkTraceID_DotString(b *testing.B) { |
| 336 |
t, _ := trace.TraceIDFromHex("0000000000000001000000000000002a") |
| 337 |
sc := trace.NewSpanContext(trace.SpanContextConfig{TraceID: t}) |
| 338 |
|
| 339 |
want := "0000000000000001000000000000002a" |
| 340 |
for i := 0; i < b.N; i++ { |
| 341 |
if got := sc.TraceID().String(); got != want { |
| 342 |
b.Fatalf("got = %q want = %q", got, want) |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
func BenchmarkSpanID_DotString(b *testing.B) { |
| 348 |
sc := trace.NewSpanContext(trace.SpanContextConfig{SpanID: trace.SpanID{1}}) |
| 349 |
want := "0100000000000000" |
| 350 |
for i := 0; i < b.N; i++ { |
| 351 |
if got := sc.SpanID().String(); got != want { |
| 352 |
b.Fatalf("got = %q want = %q", got, want) |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
func traceBenchmark(b *testing.B, name string, fn func(*testing.B, trace.Tracer)) { |
| 358 |
b.Run("AlwaysSample", func(b *testing.B) { |
| 359 |
b.ReportAllocs() |
| 360 |
fn(b, tracer(b, name, sdktrace.AlwaysSample())) |
| 361 |
}) |
| 362 |
b.Run("NeverSample", func(b *testing.B) { |
| 363 |
b.ReportAllocs() |
| 364 |
fn(b, tracer(b, name, sdktrace.NeverSample())) |
| 365 |
}) |
| 366 |
} |
| 367 |
|
| 368 |
func tracer(_ *testing.B, name string, sampler sdktrace.Sampler) trace.Tracer { |
| 369 |
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sampler)) |
| 370 |
return tp.Tracer(name) |
| 371 |
} |
| 372 |
|
| 373 |
func BenchmarkSpanProcessorOnEnd(b *testing.B) { |
| 374 |
for _, bb := range []struct { |
| 375 |
batchSize int |
| 376 |
spansCount int |
| 377 |
}{ |
| 378 |
{batchSize: 10, spansCount: 10}, |
| 379 |
{batchSize: 10, spansCount: 100}, |
| 380 |
{batchSize: 100, spansCount: 10}, |
| 381 |
{batchSize: 100, spansCount: 100}, |
| 382 |
} { |
| 383 |
b.Run(fmt.Sprintf("batch: %d, spans: %d", bb.batchSize, bb.spansCount), func(b *testing.B) { |
| 384 |
bsp := sdktrace.NewBatchSpanProcessor( |
| 385 |
tracetest.NewNoopExporter(), |
| 386 |
sdktrace.WithMaxExportBatchSize(bb.batchSize), |
| 387 |
) |
| 388 |
b.Cleanup(func() { |
| 389 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 390 |
_ = bsp.Shutdown(context.Background()) |
| 391 |
}) |
| 392 |
snap := tracetest.SpanStub{}.Snapshot() |
| 393 |
|
| 394 |
b.ResetTimer() |
| 395 |
b.ReportAllocs() |
| 396 |
for i := 0; i < b.N; i++ { |
| 397 |
// Ensure the export happens for every run |
| 398 |
for j := 0; j < bb.spansCount; j++ { |
| 399 |
bsp.OnEnd(snap) |
| 400 |
} |
| 401 |
} |
| 402 |
}) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
func BenchmarkSpanProcessorVerboseLogging(b *testing.B) { |
| 407 |
b.Cleanup(func(l logr.Logger) func() { |
| 408 |
return func() { global.SetLogger(l) } |
| 409 |
}(global.GetLogger())) |
| 410 |
global.SetLogger(funcr.New(func(string, string) {}, funcr.Options{Verbosity: 5})) |
| 411 |
tp := sdktrace.NewTracerProvider( |
| 412 |
sdktrace.WithBatcher( |
| 413 |
tracetest.NewNoopExporter(), |
| 414 |
sdktrace.WithMaxExportBatchSize(10), |
| 415 |
)) |
| 416 |
b.Cleanup(func() { |
| 417 |
//nolint:usetesting // required to avoid getting a canceled context at cleanup. |
| 418 |
_ = tp.Shutdown(context.Background()) |
| 419 |
}) |
| 420 |
tracer := tp.Tracer("bench") |
| 421 |
ctx := b.Context() |
| 422 |
|
| 423 |
b.ResetTimer() |
| 424 |
b.ReportAllocs() |
| 425 |
|
| 426 |
for i := 0; i < b.N; i++ { |
| 427 |
for range 10 { |
| 428 |
_, span := tracer.Start(ctx, "bench") |
| 429 |
span.End() |
| 430 |
} |
| 431 |
} |
| 432 |
} |