Skip to content

Commit 4987a1d

Browse files
authored
Split the span start/end benchmarks and test start with links and attributes (#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 ```
1 parent 82fe9aa commit 4987a1d

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

sdk/trace/span_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"go.opentelemetry.io/otel/attribute"
1515
"go.opentelemetry.io/otel/codes"
16+
"go.opentelemetry.io/otel/trace"
1617
)
1718

1819
func TestSetStatus(t *testing.T) {
@@ -277,3 +278,20 @@ func BenchmarkRecordingSpanSetAttributes(b *testing.B) {
277278
})
278279
}
279280
}
281+
282+
func BenchmarkSpanEnd(b *testing.B) {
283+
tracer := NewTracerProvider().Tracer("")
284+
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})
285+
286+
spans := make([]trace.Span, b.N)
287+
for i := 0; i < b.N; i++ {
288+
_, span := tracer.Start(ctx, "")
289+
spans[i] = span
290+
}
291+
292+
b.ReportAllocs()
293+
b.ResetTimer()
294+
for i := 0; i < b.N; i++ {
295+
spans[i].End()
296+
}
297+
}

sdk/trace/trace_test.go

+42-5
Original file line numberDiff line numberDiff line change
@@ -2112,11 +2112,48 @@ func BenchmarkTraceStart(b *testing.B) {
21122112
tracer := NewTracerProvider().Tracer("")
21132113
ctx := trace.ContextWithSpanContext(context.Background(), trace.SpanContext{})
21142114

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

2118-
for i := 0; i < b.N; i++ {
2119-
_, span := tracer.Start(ctx, "")
2120-
span.End()
2118+
links := []trace.Link{l1, l2}
2119+
2120+
for _, tt := range []struct {
2121+
name string
2122+
options []trace.SpanStartOption
2123+
}{
2124+
{
2125+
name: "with a simple span",
2126+
},
2127+
{
2128+
name: "with several links",
2129+
options: []trace.SpanStartOption{
2130+
trace.WithLinks(links...),
2131+
},
2132+
},
2133+
{
2134+
name: "with attributes",
2135+
options: []trace.SpanStartOption{
2136+
trace.WithAttributes(
2137+
attribute.String("key1", "value1"),
2138+
attribute.String("key2", "value2"),
2139+
),
2140+
},
2141+
},
2142+
} {
2143+
b.Run(tt.name, func(b *testing.B) {
2144+
spans := make([]trace.Span, b.N)
2145+
b.ReportAllocs()
2146+
b.ResetTimer()
2147+
2148+
for i := 0; i < b.N; i++ {
2149+
_, span := tracer.Start(ctx, "", tt.options...)
2150+
spans[i] = span
2151+
}
2152+
2153+
b.StopTimer()
2154+
for i := 0; i < b.N; i++ {
2155+
spans[i].End()
2156+
}
2157+
})
21212158
}
21222159
}

0 commit comments

Comments
 (0)