Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix benchmark error, with always/never sample and add benchmark setAttribute vs setAttributes #325

Merged
merged 2 commits into from
Nov 20, 2019
Merged
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
92 changes: 56 additions & 36 deletions sdk/trace/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
)

func BenchmarkStartEndSpan(b *testing.B) {
t := getTracer(b, "Benchmark StartEndSpan")

traceBenchmark(b, func(b *testing.B) {
traceBenchmark(b, "Benchmark StartEndSpan", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -38,9 +36,7 @@ func BenchmarkStartEndSpan(b *testing.B) {
}

func BenchmarkSpanWithAttributes_4(b *testing.B) {
t := getTracer(b, "Benchmark Start With 4 Attributes")

traceBenchmark(b, func(b *testing.B) {
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()

Expand All @@ -57,10 +53,41 @@ func BenchmarkSpanWithAttributes_4(b *testing.B) {
})
}

func BenchmarkSpanWithAttributes_8(b *testing.B) {
t := getTracer(b, "Benchmark Start With 8 Attributes")
func BenchmarkSpan_SetAttributes(b *testing.B) {
b.Run("SetAttribute", func(b *testing.B) {
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, span := t.Start(ctx, "/foo")
span.SetAttribute(key.New("key1").Bool(false))
span.SetAttribute(key.New("key2").String("hello"))
span.SetAttribute(key.New("key3").Uint64(123))
span.SetAttribute(key.New("key4").Float64(123.456))
span.End()
}
})
})

traceBenchmark(b, func(b *testing.B) {
b.Run("SetAttributes", func(b *testing.B) {
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, span := t.Start(ctx, "/foo")
span.SetAttributes(key.New("key1").Bool(false))
span.SetAttributes(key.New("key2").String("hello"))
span.SetAttributes(key.New("key3").Uint64(123))
span.SetAttributes(key.New("key4").Float64(123.456))
span.End()
}
})
})
}

func BenchmarkSpanWithAttributes_8(b *testing.B) {
traceBenchmark(b, "Benchmark Start With 8 Attributes", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()

Expand All @@ -82,9 +109,7 @@ func BenchmarkSpanWithAttributes_8(b *testing.B) {
}

func BenchmarkSpanWithAttributes_all(b *testing.B) {
t := getTracer(b, "Benchmark Start With all Attribute types")

traceBenchmark(b, func(b *testing.B) {
traceBenchmark(b, "Benchmark Start With all Attribute types", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()

Expand All @@ -108,8 +133,7 @@ func BenchmarkSpanWithAttributes_all(b *testing.B) {
}

func BenchmarkSpanWithAttributes_all_2x(b *testing.B) {
t := getTracer(b, "Benchmark Start With all Attributes types twice")
traceBenchmark(b, func(b *testing.B) {
traceBenchmark(b, "Benchmark Start With all Attributes types twice", func(b *testing.B, t apitrace.Tracer) {
ctx := context.Background()
b.ResetTimer()

Expand Down Expand Up @@ -143,44 +167,40 @@ func BenchmarkSpanWithAttributes_all_2x(b *testing.B) {
}

func BenchmarkTraceID_DotString(b *testing.B) {
traceBenchmark(b, func(b *testing.B) {
t, _ := core.TraceIDFromHex("0000000000000001000000000000002a")
sc := core.SpanContext{TraceID: t}
t, _ := core.TraceIDFromHex("0000000000000001000000000000002a")
sc := core.SpanContext{TraceID: t}

want := "0000000000000001000000000000002a"
for i := 0; i < b.N; i++ {
if got := sc.TraceIDString(); got != want {
b.Fatalf("got = %q want = %q", got, want)
}
want := "0000000000000001000000000000002a"
for i := 0; i < b.N; i++ {
if got := sc.TraceIDString(); got != want {
b.Fatalf("got = %q want = %q", got, want)
}
})
}
}

func BenchmarkSpanID_DotString(b *testing.B) {
traceBenchmark(b, func(b *testing.B) {
sc := core.SpanContext{SpanID: core.SpanID{1}}
want := "1000000000000000"
for i := 0; i < b.N; i++ {
if got := sc.SpanIDString(); got != want {
b.Fatalf("got = %q want = %q", got, want)
}
sc := core.SpanContext{SpanID: core.SpanID{1}}
want := "0100000000000000"
for i := 0; i < b.N; i++ {
if got := sc.SpanIDString(); got != want {
b.Fatalf("got = %q want = %q", got, want)
}
})
}
}

func traceBenchmark(b *testing.B, fn func(*testing.B)) {
func traceBenchmark(b *testing.B, name string, fn func(*testing.B, apitrace.Tracer)) {
b.Run("AlwaysSample", func(b *testing.B) {
b.ReportAllocs()
fn(b)
fn(b, getTracer(b, name, sdktrace.AlwaysSample()))
})
b.Run("NeverSample", func(b *testing.B) {
b.ReportAllocs()
fn(b)
fn(b, getTracer(b, name, sdktrace.NeverSample()))
})
}

func getTracer(b *testing.B, name string) apitrace.Tracer {
tp, err := sdktrace.NewProvider(sdktrace.WithConfig(testConfig))
func getTracer(b *testing.B, name string, sampler sdktrace.Sampler) apitrace.Tracer {
tp, err := sdktrace.NewProvider(sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sampler}))
if err != nil {
b.Fatalf("Failed to create trace provider for test %s\n", name)
}
Expand Down