2020-03-23 22:41:10 -07:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-08-02 13:52:55 -07:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-10-22 13:19:11 -07:00
|
|
|
package trace_test
|
2019-08-02 13:52:55 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-08 19:58:56 -07:00
|
|
|
"go.opentelemetry.io/otel"
|
2020-08-17 20:25:03 -07:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2019-11-01 11:40:29 -07:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-08-02 13:52:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkStartEndSpan(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
traceBenchmark(b, "Benchmark StartEndSpan", func(b *testing.B, t otel.Tracer) {
|
2019-08-02 13:52:55 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, span := t.Start(ctx, "/foo")
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSpanWithAttributes_4(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
traceBenchmark(b, "Benchmark Start With 4 Attributes", func(b *testing.B, t otel.Tracer) {
|
2019-08-02 13:52:55 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, span := t.Start(ctx, "/foo")
|
|
|
|
span.SetAttributes(
|
2020-08-17 20:25:03 -07:00
|
|
|
label.Bool("key1", false),
|
|
|
|
label.String("key2", "hello"),
|
|
|
|
label.Uint64("key3", 123),
|
|
|
|
label.Float64("key4", 123.456),
|
2019-08-02 13:52:55 -07:00
|
|
|
)
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-20 16:34:43 -04:00
|
|
|
func BenchmarkSpanWithAttributes_8(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
traceBenchmark(b, "Benchmark Start With 8 Attributes", func(b *testing.B, t otel.Tracer) {
|
2019-08-02 13:52:55 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, span := t.Start(ctx, "/foo")
|
|
|
|
span.SetAttributes(
|
2020-08-17 20:25:03 -07:00
|
|
|
label.Bool("key1", false),
|
|
|
|
label.String("key2", "hello"),
|
|
|
|
label.Uint64("key3", 123),
|
|
|
|
label.Float64("key4", 123.456),
|
|
|
|
label.Bool("key21", false),
|
|
|
|
label.String("key22", "hello"),
|
|
|
|
label.Uint64("key23", 123),
|
|
|
|
label.Float64("key24", 123.456),
|
2019-08-02 13:52:55 -07:00
|
|
|
)
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSpanWithAttributes_all(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
traceBenchmark(b, "Benchmark Start With all Attribute types", func(b *testing.B, t otel.Tracer) {
|
2019-08-02 13:52:55 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, span := t.Start(ctx, "/foo")
|
|
|
|
span.SetAttributes(
|
2020-08-17 20:25:03 -07:00
|
|
|
label.Bool("key1", false),
|
|
|
|
label.String("key2", "hello"),
|
|
|
|
label.Int64("key3", 123),
|
|
|
|
label.Uint64("key4", 123),
|
|
|
|
label.Int32("key5", 123),
|
|
|
|
label.Uint32("key6", 123),
|
|
|
|
label.Float64("key7", 123.456),
|
|
|
|
label.Float32("key8", 123.456),
|
|
|
|
label.Int("key9", 123),
|
|
|
|
label.Uint("key10", 123),
|
2019-08-02 13:52:55 -07:00
|
|
|
)
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSpanWithAttributes_all_2x(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
traceBenchmark(b, "Benchmark Start With all Attributes types twice", func(b *testing.B, t otel.Tracer) {
|
2019-08-02 13:52:55 -07:00
|
|
|
ctx := context.Background()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, span := t.Start(ctx, "/foo")
|
|
|
|
span.SetAttributes(
|
2020-08-17 20:25:03 -07:00
|
|
|
label.Bool("key1", false),
|
|
|
|
label.String("key2", "hello"),
|
|
|
|
label.Int64("key3", 123),
|
|
|
|
label.Uint64("key4", 123),
|
|
|
|
label.Int32("key5", 123),
|
|
|
|
label.Uint32("key6", 123),
|
|
|
|
label.Float64("key7", 123.456),
|
|
|
|
label.Float32("key8", 123.456),
|
|
|
|
label.Int("key10", 123),
|
|
|
|
label.Uint("key11", 123),
|
|
|
|
label.Bool("key21", false),
|
|
|
|
label.String("key22", "hello"),
|
|
|
|
label.Int64("key23", 123),
|
|
|
|
label.Uint64("key24", 123),
|
|
|
|
label.Int32("key25", 123),
|
|
|
|
label.Uint32("key26", 123),
|
|
|
|
label.Float64("key27", 123.456),
|
|
|
|
label.Float32("key28", 123.456),
|
|
|
|
label.Int("key210", 123),
|
|
|
|
label.Uint("key211", 123),
|
2019-08-02 13:52:55 -07:00
|
|
|
)
|
2019-09-27 10:48:10 -07:00
|
|
|
span.End()
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTraceID_DotString(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
t, _ := otel.TraceIDFromHex("0000000000000001000000000000002a")
|
|
|
|
sc := otel.SpanContext{TraceID: t}
|
2019-08-02 13:52:55 -07:00
|
|
|
|
2019-11-20 16:34:43 -04:00
|
|
|
want := "0000000000000001000000000000002a"
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-17 04:12:48 +05:30
|
|
|
if got := sc.TraceID.String(); got != want {
|
2019-11-20 16:34:43 -04:00
|
|
|
b.Fatalf("got = %q want = %q", got, want)
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
2019-11-20 16:34:43 -04:00
|
|
|
}
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSpanID_DotString(b *testing.B) {
|
2020-10-08 19:58:56 -07:00
|
|
|
sc := otel.SpanContext{SpanID: otel.SpanID{1}}
|
2019-11-20 16:34:43 -04:00
|
|
|
want := "0100000000000000"
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-17 04:12:48 +05:30
|
|
|
if got := sc.SpanID.String(); got != want {
|
2019-11-20 16:34:43 -04:00
|
|
|
b.Fatalf("got = %q want = %q", got, want)
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
2019-11-20 16:34:43 -04:00
|
|
|
}
|
2019-08-02 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
2020-10-08 19:58:56 -07:00
|
|
|
func traceBenchmark(b *testing.B, name string, fn func(*testing.B, otel.Tracer)) {
|
2019-08-02 13:52:55 -07:00
|
|
|
b.Run("AlwaysSample", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2019-11-25 18:46:07 +01:00
|
|
|
fn(b, tracer(b, name, sdktrace.AlwaysSample()))
|
2019-08-02 13:52:55 -07:00
|
|
|
})
|
|
|
|
b.Run("NeverSample", func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
2019-11-25 18:46:07 +01:00
|
|
|
fn(b, tracer(b, name, sdktrace.NeverSample()))
|
2019-08-02 13:52:55 -07:00
|
|
|
})
|
|
|
|
}
|
2019-10-22 13:19:11 -07:00
|
|
|
|
2020-10-08 19:58:56 -07:00
|
|
|
func tracer(b *testing.B, name string, sampler sdktrace.Sampler) otel.Tracer {
|
2020-09-23 15:16:13 -07:00
|
|
|
tp := sdktrace.NewTracerProvider(sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sampler}))
|
2019-11-25 18:46:07 +01:00
|
|
|
return tp.Tracer(name)
|
2019-10-28 14:05:06 -03:00
|
|
|
}
|