1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/api/key/benchmark_test.go
Joshua MacDonald 4c9a29d2d7
Add a key benchmark, use reflection in key.Infer() (#679)
* Add a key benchmark, optimize SDK SetAttribute

* Use reflect in key.Infer

* Move to separate benchmark file; remove pointer test; remove dead comment

* Run go mod tidy

* Add license header

* Use the reflect scalar accessors

Co-authored-by: Liz Fong-Jones <lizf@honeycomb.io>
2020-04-30 13:06:00 -07:00

76 lines
1.7 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 key_test
import (
"context"
"testing"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/api/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// Note: The tests below load a real SDK to ensure the compiler isn't optimizing
// the test based on global analysis limited to the NoopSpan implementation.
func getSpan() trace.Span {
_, sp := global.Tracer("Test").Start(context.Background(), "Span")
tr, _ := sdktrace.NewProvider()
global.SetTraceProvider(tr)
return sp
}
func BenchmarkKeyInfer(b *testing.B) {
for i := 0; i < b.N; i++ {
key.Infer("Attr", int(256))
}
}
func BenchmarkMultiNoKeyInference(b *testing.B) {
sp := getSpan()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sp.SetAttributes(key.Int("Attr", 1))
}
}
func BenchmarkMultiWithKeyInference(b *testing.B) {
sp := getSpan()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sp.SetAttributes(key.Infer("Attr", 1))
}
}
func BenchmarkSingleWithKeyInferenceValue(b *testing.B) {
sp := getSpan()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sp.SetAttribute("Attr", 1)
}
b.StopTimer()
}