2020-03-24 07:41:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
package internal_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/api/global"
|
|
|
|
"go.opentelemetry.io/otel/api/global/internal"
|
2020-05-14 01:06:03 +02:00
|
|
|
"go.opentelemetry.io/otel/api/kv"
|
2019-12-24 09:03:04 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2020-01-02 23:20:38 +02:00
|
|
|
"go.opentelemetry.io/otel/api/trace"
|
2019-12-24 09:03:04 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
|
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
2020-06-23 21:00:15 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/test"
|
2020-01-02 23:20:38 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-12-24 09:03:04 +02:00
|
|
|
)
|
|
|
|
|
2020-03-11 20:57:57 +02:00
|
|
|
var Must = metric.Must
|
|
|
|
|
2019-12-24 09:03:04 +02:00
|
|
|
// benchFixture is copied from sdk/metric/benchmark_test.go.
|
|
|
|
// TODO refactor to share this code.
|
|
|
|
type benchFixture struct {
|
2020-06-23 19:51:15 +02:00
|
|
|
export.AggregatorSelector
|
2020-05-11 19:23:06 +02:00
|
|
|
accumulator *sdk.Accumulator
|
|
|
|
meter metric.Meter
|
|
|
|
B *testing.B
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ metric.Provider = &benchFixture{}
|
|
|
|
|
|
|
|
func newFixture(b *testing.B) *benchFixture {
|
|
|
|
b.ReportAllocs()
|
|
|
|
bf := &benchFixture{
|
2020-06-23 19:51:15 +02:00
|
|
|
B: b,
|
|
|
|
AggregatorSelector: test.AggregatorSelector(),
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
2020-03-24 19:54:08 +02:00
|
|
|
|
2020-05-11 19:23:06 +02:00
|
|
|
bf.accumulator = sdk.NewAccumulator(bf)
|
|
|
|
bf.meter = metric.WrapMeterImpl(bf.accumulator, "test")
|
2019-12-24 09:03:04 +02:00
|
|
|
return bf
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:16:33 +02:00
|
|
|
func (*benchFixture) Process(export.Accumulation) error {
|
2019-12-24 09:03:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 18:11:17 +02:00
|
|
|
func (fix *benchFixture) Meter(_ string, _ ...metric.MeterOption) metric.Meter {
|
2020-03-19 21:02:46 +02:00
|
|
|
return fix.meter
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGlobalInt64CounterAddNoSDK(b *testing.B) {
|
|
|
|
internal.ResetForTest()
|
|
|
|
ctx := context.Background()
|
2020-03-11 17:23:32 +02:00
|
|
|
sdk := global.Meter("test")
|
2020-05-14 01:06:03 +02:00
|
|
|
labs := []kv.KeyValue{kv.String("A", "B")}
|
2020-03-11 20:57:57 +02:00
|
|
|
cnt := Must(sdk).NewInt64Counter("int64.counter")
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-03-27 23:06:48 +02:00
|
|
|
cnt.Add(ctx, 1, labs...)
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGlobalInt64CounterAddWithSDK(b *testing.B) {
|
|
|
|
// Comapare with BenchmarkInt64CounterAdd() in ../../sdk/meter/benchmark_test.go
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
|
2020-03-11 17:23:32 +02:00
|
|
|
sdk := global.Meter("test")
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
global.SetMeterProvider(fix)
|
|
|
|
|
2020-05-14 01:06:03 +02:00
|
|
|
labs := []kv.KeyValue{kv.String("A", "B")}
|
2020-03-11 20:57:57 +02:00
|
|
|
cnt := Must(sdk).NewInt64Counter("int64.counter")
|
2019-12-24 09:03:04 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-03-27 23:06:48 +02:00
|
|
|
cnt.Add(ctx, 1, labs...)
|
2019-12-24 09:03:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-02 23:20:38 +02:00
|
|
|
|
|
|
|
func BenchmarkStartEndSpan(b *testing.B) {
|
|
|
|
// Comapare with BenchmarkStartEndSpan() in ../../sdk/trace/benchmark_test.go
|
|
|
|
traceBenchmark(b, func(b *testing.B) {
|
2020-03-11 17:23:32 +02:00
|
|
|
t := global.Tracer("Benchmark StartEndSpan")
|
2020-01-02 23:20:38 +02:00
|
|
|
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
|
|
|
|
}
|