Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions tracing/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tracing

import (
"context"
"fmt"
"testing"
)

var traceHandlers = map[string]TraceHandle{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider this approach which makes what's happening obvious as opposed to hiding it in runTraceHandleBenchmarks:

Suggested change
var traceHandlers = map[string]TraceHandle{
func BenchmarkTrace(b *testing.B) {
traceHandlers = []struct {
prefix string
traceHandle TraceHandle
} {
{
prefix: "OTel",
traceHandle: NewOTELTracer(),
},
{
prefix: "Noop",
traceHandle: NewNoopTracer(),
},
}
for prefix, th := range traceHandlers {
b.Run(fmt.Sprintf("BenchmarkStartSpan_%s", prefix), func(b *testing.B) {
ctx := context.Background()
for b.Loop() {
_, span := th.StartSpan(ctx, "TestSpanName")
th.EndSpan(span)
}
})
b.Run(fmt.Sprintf("BenchmarkStartServerSpan_%s", prefix), func(b *testing.B) {
ctx := context.Background()
for b.Loop() {
_, span := th.StartServerSpan(ctx, "TestSpanName")
th.EndSpan(span)
}
})
...
}
)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are going for DAMP vs DRY ?

"otel": NewOTELTracer(),
"noop": NewNoopTracer(),
}

func runTraceHandleBenchmarks(b *testing.B, benchFn func(b *testing.B, th TraceHandle)) {
for name, th := range traceHandlers {
b.Run(name, func(b *testing.B) {
benchFn(b, th)
})
}
}

func BenchmarkStartSpan(b *testing.B) {
ctx := context.Background()
runTraceHandleBenchmarks(b, func(b *testing.B, th TraceHandle) {
for b.Loop() {
_, span := th.StartSpan(ctx, "TestSpanName")
th.EndSpan(span)
}
})
}

func BenchmarkStartServerSpan(b *testing.B) {
ctx := context.Background()
runTraceHandleBenchmarks(b, func(b *testing.B, th TraceHandle) {
for b.Loop() {
_, span := th.StartServerSpan(ctx, "TestSpanName")
th.EndSpan(span)
}
})
}

func BenchmarkRecordError(b *testing.B) {
ctx := context.Background()
err := fmt.Errorf("test error")
runTraceHandleBenchmarks(b, func(b *testing.B, th TraceHandle) {
_, span := th.StartSpan(ctx, "TestSpanName")
for b.Loop() {
th.RecordError(span, err)
}
th.EndSpan(span)
})
}

func BenchmarkSetCacheReadAttributes(b *testing.B) {
ctx := context.Background()
runTraceHandleBenchmarks(b, func(b *testing.B, th TraceHandle) {
_, span := th.StartSpan(ctx, "TestSpanName")
for b.Loop() {
th.SetCacheReadAttributes(span, true, 100)
}
th.EndSpan(span)
})
}

func BenchmarkPropagateTraceContext(b *testing.B) {
ctx := context.Background()
runTraceHandleBenchmarks(b, func(b *testing.B, th TraceHandle) {
for b.Loop() {
_ = th.PropagateTraceContext(ctx, ctx)
}
})
}
Loading