Skip to content

Commit

Permalink
Add tracing benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
bw19 committed May 11, 2024
1 parent 1b5ffb3 commit 30cfa83
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions connector/traceselector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
"time"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/sdk/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)

type exporter struct {
Callback func(ctx context.Context, spans []trace.ReadOnlySpan) error
Callback func(ctx context.Context, spans []sdktrace.ReadOnlySpan) error
}

func (e *exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
func (e *exporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
if e.Callback != nil {
return e.Callback(ctx, spans)
}
Expand All @@ -39,7 +39,7 @@ func TestConnector_TracingExport(t *testing.T) {
countExported := 0
exportedSpans := map[string]bool{}
ts := newTraceSelector(&exporter{
Callback: func(ctx context.Context, spans []trace.ReadOnlySpan) error {
Callback: func(ctx context.Context, spans []sdktrace.ReadOnlySpan) error {
countExported += len(spans)
for _, span := range spans {
exportedSpans[span.SpanContext().SpanID().String()] = true
Expand Down Expand Up @@ -248,3 +248,29 @@ func TestConnector_TracingBufferCapacityRollover(t *testing.T) {
assert.Equal(t, int32(i+1), ts.insertionPoint.Load())
}
}

func BenchmarkConnector_TracingOnEnd(b *testing.B) {
ctx := context.Background()

ts := newTraceSelector(&exporter{})

traceProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(ts),
)
tracer := traceProvider.Tracer("")

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

b.ResetTimer()
for i := 0; i < b.N; i++ {
arr[i].End()
}
// N=5078120
// 301.4 ns/op
// 400 B/op
// 2 allocs/op
}

0 comments on commit 30cfa83

Please sign in to comment.