Skip to content

Commit

Permalink
tracing: udpate benchmarks to include event listeners
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
adityamaru committed Apr 25, 2022
1 parent c5c238e commit c0262e6
Showing 1 changed file with 84 additions and 43 deletions.
127 changes: 84 additions & 43 deletions pkg/util/tracing/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,69 @@ func BenchmarkTracer_StartSpanCtx(b *testing.B) {
staticLogTags := logtags.Buffer{}
staticLogTags.Add("foo", "bar")

startSpanCtx := func(b *testing.B, hasParent bool, hasEventListener bool, spanOpts []SpanOption) {
tr := NewTracerWithOpt(ctx,
WithTracingMode(TracingModeActiveSpansRegistry),
WithSpanReusePercent(100))
b.ResetTimer()

var parent *Span
var numOpts = len(spanOpts)
if hasParent {
parent = tr.StartSpan("one-off")
defer parent.Finish()

if hasEventListener {
ev := mockEventListener{}
parent.RegisterEventListener("bench-listener", &ev)
}
numOpts++
}
opts := make([]SpanOption, numOpts)
copy(opts, spanOpts)

b.ReportAllocs()
for i := 0; i < b.N; i++ {
if parent != nil {
// The WithParent option needs to be re-created every time; it cannot be reused.
opts[len(opts)-1] = WithParent(parent)
}
newCtx, sp := tr.StartSpanCtx(ctx, "benching", opts...)
_ = newCtx
sp.Finish() // clean up
}
}

// Benchmarks with a root span.
for _, tc := range []struct {
name string
defaultMode TracingMode
parent bool
opts []SpanOption
}{
{"none", TracingModeOnDemand, false, nil},
{"real", TracingModeActiveSpansRegistry, false, nil},
{"real,logtag", TracingModeActiveSpansRegistry, false, []SpanOption{WithLogTags(&staticLogTags)}},
{"real,autoparent", TracingModeActiveSpansRegistry, true, nil},
{"real,manualparent", TracingModeActiveSpansRegistry, true, []SpanOption{WithDetachedRecording()}},
{"none", TracingModeOnDemand, nil},
{"real", TracingModeActiveSpansRegistry, nil},
{"real,logtag", TracingModeActiveSpansRegistry, []SpanOption{WithLogTags(&staticLogTags)}},
} {
b.Run(fmt.Sprintf("opts=%s", tc.name), func(b *testing.B) {
tr := NewTracerWithOpt(ctx,
WithTracingMode(TracingModeActiveSpansRegistry),
WithSpanReusePercent(100))
b.ResetTimer()

var parent *Span
var numOpts = len(tc.opts)
if tc.parent {
parent = tr.StartSpan("one-off")
defer parent.Finish()
numOpts++
}
opts := make([]SpanOption, numOpts)
copy(opts, tc.opts)

b.ReportAllocs()
for i := 0; i < b.N; i++ {
if parent != nil {
// The WithParent option needs to be re-created every time; it cannot be reused.
opts[len(opts)-1] = WithParent(parent)
}
newCtx, sp := tr.StartSpanCtx(ctx, "benching", opts...)
_ = newCtx
sp.Finish() // clean up
}
startSpanCtx(b, false /* hasParent */, false /* hasEventListener */, tc.opts)
})
}

// Benchmarks with a root and child span.
for _, tc := range []struct {
name string
defaultMode TracingMode
opts []SpanOption
}{
{"real,autoparent", TracingModeActiveSpansRegistry, nil},
{"real,manualparent", TracingModeActiveSpansRegistry, []SpanOption{WithDetachedRecording()}},
} {
for _, hasEventListener := range []bool{true, false} {
b.Run(fmt.Sprintf("opts=%s/eventListener=%t", tc.name, hasEventListener), func(b *testing.B) {
startSpanCtx(b, true /* hasParent */, hasEventListener, tc.opts)
})
}
}
}

// BenchmarkSpan_GetRecording microbenchmarks GetRecording.
Expand Down Expand Up @@ -106,19 +128,38 @@ func BenchmarkSpan_GetRecording(b *testing.B) {

func BenchmarkRecordingWithStructuredEvent(b *testing.B) {
skip.UnderDeadlock(b, "span reuse triggers false-positives in the deadlock detector")
tr := NewTracerWithOpt(context.Background(),
WithTracingMode(TracingModeActiveSpansRegistry),
WithSpanReusePercent(100))

ev := &types.Int32Value{Value: 5}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
root := tr.StartSpan("foo", WithRecording(RecordingStructured))
root.RecordStructured(ev)
child := tr.StartSpan("bar", WithParent(root))
child.RecordStructured(ev)
child.Finish()
_ = root.FinishAndGetRecording(RecordingStructured)
mockListener := mockEventListener{}

for _, tc := range []struct {
name string
withEventListener bool
}{
{name: "with-event-listener", withEventListener: true},
{name: "without-event-listener", withEventListener: false},
} {
b.Run(tc.name, func(b *testing.B) {
tr := NewTracerWithOpt(context.Background(),
WithTracingMode(TracingModeActiveSpansRegistry),
WithSpanReusePercent(100))

b.ReportAllocs()
for i := 0; i < b.N; i++ {
root := tr.StartSpan("foo", WithRecording(RecordingStructured))
// Register an event listener with the root span.
if tc.withEventListener {
root.RegisterEventListener("mock-event-listener", &mockListener)
}

root.RecordStructured(ev)

// The child span will also inherit the root span's event listener.
child := tr.StartSpan("bar", WithParent(root))
child.RecordStructured(ev)
child.Finish()
_ = root.FinishAndGetRecording(RecordingStructured)
}
})
}
}

Expand Down

0 comments on commit c0262e6

Please sign in to comment.