Skip to content

Commit

Permalink
Split the span start/end benchmarks and test start with links and att…
Browse files Browse the repository at this point in the history
…ributes (#5554)

I was looking at the trace benchmarks, and noticed this one, which says
it tests "span start", but ending the span is included within the data.
So this change removes ending the span from the computation, and adds a
new benchmark which only computes span end.

benchstat for span start:

```
pkg: go.opentelemetry.io/otel/sdk/trace
              │ bench-main  │            bench-branch            │
              │   sec/op    │   sec/op     vs base               │
TraceStart-10   725.6n ± 3%   667.2n ± 2%  -8.04% (p=0.000 n=10)

              │ bench-main │          bench-branch          │
              │    B/op    │    B/op     vs base            │
TraceStart-10   704.0 ± 0%   704.0 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

              │ bench-main │          bench-branch          │
              │ allocs/op  │ allocs/op   vs base            │
TraceStart-10   14.00 ± 0%   14.00 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal
```

Benchmark for span end:
```
BenchmarkSpanEnd-10     16486819               147.7 ns/op             0 B/op          0 allocs/op
```
  • Loading branch information
dmathieu committed Jun 28, 2024
1 parent 82fe9aa commit 4987a1d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
18 changes: 18 additions & 0 deletions sdk/trace/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

func TestSetStatus(t *testing.T) {
Expand Down Expand Up @@ -277,3 +278,20 @@ func BenchmarkRecordingSpanSetAttributes(b *testing.B) {
})
}
}

func BenchmarkSpanEnd(b *testing.B) {
tracer := NewTracerProvider().Tracer("")
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})

spans := make([]trace.Span, b.N)
for i := 0; i < b.N; i++ {
_, span := tracer.Start(ctx, "")
spans[i] = span
}

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
spans[i].End()
}
}
47 changes: 42 additions & 5 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2112,11 +2112,48 @@ func BenchmarkTraceStart(b *testing.B) {
tracer := NewTracerProvider().Tracer("")
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})

b.ReportAllocs()
b.ResetTimer()
l1 := trace.Link{SpanContext: trace.SpanContext{}, Attributes: []attribute.KeyValue{}}
l2 := trace.Link{SpanContext: trace.SpanContext{}, Attributes: []attribute.KeyValue{}}

for i := 0; i < b.N; i++ {
_, span := tracer.Start(ctx, "")
span.End()
links := []trace.Link{l1, l2}

for _, tt := range []struct {
name string
options []trace.SpanStartOption
}{
{
name: "with a simple span",
},
{
name: "with several links",
options: []trace.SpanStartOption{
trace.WithLinks(links...),
},
},
{
name: "with attributes",
options: []trace.SpanStartOption{
trace.WithAttributes(
attribute.String("key1", "value1"),
attribute.String("key2", "value2"),
),
},
},
} {
b.Run(tt.name, func(b *testing.B) {
spans := make([]trace.Span, b.N)
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, span := tracer.Start(ctx, "", tt.options...)
spans[i] = span
}

b.StopTimer()
for i := 0; i < b.N; i++ {
spans[i].End()
}
})
}
}

0 comments on commit 4987a1d

Please sign in to comment.