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

New benchmark derived from otel-launcher-go #3395

Merged
merged 10 commits into from
Nov 1, 2022
152 changes: 152 additions & 0 deletions sdk/metric/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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 metric // import "go.opentelemetry.io/otel/sdk/metric"

import (
"context"
"testing"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/view"
)

func BenchmarkCounterAddNoAttrs(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")
jmacd marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i < b.N; i++ {
jmacd marked this conversation as resolved.
Show resolved Hide resolved
cntr.Add(ctx, 1)
}
}

func BenchmarkCounterAddOneAttr(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.String("K", "V"))
}
}

func BenchmarkCounterAddOneInvalidAttr(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.String("", "V"), attribute.String("K", "V"))
}
}

func BenchmarkCounterAddManyAttrs(b *testing.B) {
jmacd marked this conversation as resolved.
Show resolved Hide resolved
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.Int("K", i))
}
}

func BenchmarkCounterAddManyInvalidAttrs(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.Int("", i), attribute.Int("K", i))
}
}

func BenchmarkCounterAddManyFilteredAttrs(b *testing.B) {
vw, _ := view.New(view.WithFilterAttributes(attribute.Key("K")))

ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr, vw))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.Int("L", i), attribute.Int("K", i))
}
}

func BenchmarkCounterCollectOneAttr(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
cntr.Add(ctx, 1, attribute.Int("K", 1))

_, _ = rdr.Collect(ctx)
}
}

func BenchmarkCounterCollectTenAttrs(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
for j := 0; j < 10; j++ {
cntr.Add(ctx, 1, attribute.Int("K", j))
}
_, _ = rdr.Collect(ctx)
}
}

func BenchmarkCounterCollectTenAttrsTenTimes(b *testing.B) {
ctx := context.Background()
rdr := NewManualReader()
provider := NewMeterProvider(WithReader(rdr))
b.ReportAllocs()

cntr, _ := provider.Meter("test").SyncInt64().Counter("hello")

for i := 0; i < b.N; i++ {
for k := 0; k < 10; k++ {
for j := 0; j < 10; j++ {
cntr.Add(ctx, 1, attribute.Int("K", j))
}
_, _ = rdr.Collect(ctx)
}
}
}
jmacd marked this conversation as resolved.
Show resolved Hide resolved