mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
2966505271
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// 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 internal_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
"go.opentelemetry.io/otel/api/global/internal"
|
|
"go.opentelemetry.io/otel/api/kv"
|
|
"go.opentelemetry.io/otel/api/metric"
|
|
"go.opentelemetry.io/otel/api/trace"
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/test"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
)
|
|
|
|
var Must = metric.Must
|
|
|
|
// benchFixture is copied from sdk/metric/benchmark_test.go.
|
|
// TODO refactor to share this code.
|
|
type benchFixture struct {
|
|
export.AggregatorSelector
|
|
accumulator *sdk.Accumulator
|
|
meter metric.Meter
|
|
B *testing.B
|
|
}
|
|
|
|
var _ metric.Provider = &benchFixture{}
|
|
|
|
func newFixture(b *testing.B) *benchFixture {
|
|
b.ReportAllocs()
|
|
bf := &benchFixture{
|
|
B: b,
|
|
AggregatorSelector: test.AggregatorSelector(),
|
|
}
|
|
|
|
bf.accumulator = sdk.NewAccumulator(bf)
|
|
bf.meter = metric.WrapMeterImpl(bf.accumulator, "test")
|
|
return bf
|
|
}
|
|
|
|
func (*benchFixture) Process(export.Accumulation) error {
|
|
return nil
|
|
}
|
|
|
|
func (fix *benchFixture) Meter(_ string, _ ...metric.MeterOption) metric.Meter {
|
|
return fix.meter
|
|
}
|
|
|
|
func BenchmarkGlobalInt64CounterAddNoSDK(b *testing.B) {
|
|
internal.ResetForTest()
|
|
ctx := context.Background()
|
|
sdk := global.Meter("test")
|
|
labs := []kv.KeyValue{kv.String("A", "B")}
|
|
cnt := Must(sdk).NewInt64Counter("int64.counter")
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
cnt.Add(ctx, 1, labs...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
|
|
// Comapare with BenchmarkInt64CounterAdd() in ../../sdk/meter/benchmark_test.go
|
|
ctx := context.Background()
|
|
fix := newFixture(b)
|
|
|
|
sdk := global.Meter("test")
|
|
|
|
global.SetMeterProvider(fix)
|
|
|
|
labs := []kv.KeyValue{kv.String("A", "B")}
|
|
cnt := Must(sdk).NewInt64Counter("int64.counter")
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
cnt.Add(ctx, 1, labs...)
|
|
}
|
|
}
|
|
|
|
func BenchmarkStartEndSpan(b *testing.B) {
|
|
// Comapare with BenchmarkStartEndSpan() in ../../sdk/trace/benchmark_test.go
|
|
traceBenchmark(b, func(b *testing.B) {
|
|
t := global.Tracer("Benchmark StartEndSpan")
|
|
ctx := context.Background()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, span := t.Start(ctx, "/foo")
|
|
span.End()
|
|
}
|
|
})
|
|
}
|
|
|
|
func traceBenchmark(b *testing.B, fn func(*testing.B)) {
|
|
internal.ResetForTest()
|
|
b.Run("No SDK", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
fn(b)
|
|
})
|
|
b.Run("Default SDK (AlwaysSample)", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
global.SetTraceProvider(traceProvider(b, sdktrace.AlwaysSample()))
|
|
fn(b)
|
|
})
|
|
b.Run("Default SDK (NeverSample)", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
global.SetTraceProvider(traceProvider(b, sdktrace.NeverSample()))
|
|
fn(b)
|
|
})
|
|
}
|
|
|
|
func traceProvider(b *testing.B, sampler sdktrace.Sampler) trace.Provider {
|
|
tp, err := sdktrace.NewProvider(sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sampler}))
|
|
if err != nil {
|
|
b.Fatalf("Failed to create trace provider with sampler: %v", err)
|
|
}
|
|
return tp
|
|
}
|