diff --git a/sdk/trace/span_test.go b/sdk/trace/span_test.go index 4556da8e91a..38bf2b2e8a9 100644 --- a/sdk/trace/span_test.go +++ b/sdk/trace/span_test.go @@ -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) { @@ -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() + } +} diff --git a/sdk/trace/trace_test.go b/sdk/trace/trace_test.go index 09dcb5eb3bf..87247d1f167 100644 --- a/sdk/trace/trace_test.go +++ b/sdk/trace/trace_test.go @@ -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() + } + }) } }