2019-10-29 22:27:22 +02:00
|
|
|
// Copyright 2019, 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_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
"go.opentelemetry.io/otel/api/key"
|
2019-11-06 20:54:36 +02:00
|
|
|
"go.opentelemetry.io/otel/api/metric"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-01 20:40:29 +02:00
|
|
|
sdk "go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/counter"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/ddsketch"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/gauge"
|
2019-11-27 00:07:58 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type benchFixture struct {
|
|
|
|
sdk *sdk.SDK
|
|
|
|
B *testing.B
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFixture(b *testing.B) *benchFixture {
|
|
|
|
b.ReportAllocs()
|
|
|
|
bf := &benchFixture{
|
|
|
|
B: b,
|
|
|
|
}
|
2019-11-22 06:46:05 +02:00
|
|
|
bf.sdk = sdk.New(bf, sdk.NewDefaultLabelEncoder())
|
2019-10-29 22:27:22 +02:00
|
|
|
return bf
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func (*benchFixture) AggregatorFor(descriptor *export.Descriptor) export.Aggregator {
|
|
|
|
switch descriptor.MetricKind() {
|
2019-11-05 23:08:55 +02:00
|
|
|
case export.CounterKind:
|
2019-10-29 22:27:22 +02:00
|
|
|
return counter.New()
|
2019-11-05 23:08:55 +02:00
|
|
|
case export.GaugeKind:
|
2019-10-29 22:27:22 +02:00
|
|
|
return gauge.New()
|
2019-11-05 23:08:55 +02:00
|
|
|
case export.MeasureKind:
|
2019-11-27 00:07:58 +02:00
|
|
|
if strings.HasSuffix(descriptor.Name(), "minmaxsumcount") {
|
|
|
|
return minmaxsumcount.New(descriptor)
|
2019-11-15 23:01:20 +02:00
|
|
|
} else if strings.HasSuffix(descriptor.Name(), "ddsketch") {
|
|
|
|
return ddsketch.New(ddsketch.NewDefaultConfig(), descriptor)
|
|
|
|
} else if strings.HasSuffix(descriptor.Name(), "array") {
|
|
|
|
return ddsketch.New(ddsketch.NewDefaultConfig(), descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func (*benchFixture) Process(context.Context, export.Record) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*benchFixture) CheckpointSet() export.CheckpointSet {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*benchFixture) FinishedCollection() {
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func makeLabelSets(n int) [][]core.KeyValue {
|
|
|
|
r := make([][]core.KeyValue, n)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
r[i] = makeLabels(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
func makeLabels(n int) []core.KeyValue {
|
|
|
|
used := map[string]bool{}
|
|
|
|
l := make([]core.KeyValue, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
var k string
|
|
|
|
for {
|
|
|
|
k = fmt.Sprint("k", rand.Intn(1000000000))
|
|
|
|
if !used[k] {
|
|
|
|
used[k] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l[i] = key.New(k).String(fmt.Sprint("v", rand.Intn(1000000000)))
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkLabels(b *testing.B, n int) {
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := makeLabels(n)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
fix.sdk.Labels(labs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabels_1(b *testing.B) {
|
|
|
|
benchmarkLabels(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabels_2(b *testing.B) {
|
|
|
|
benchmarkLabels(b, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabels_4(b *testing.B) {
|
|
|
|
benchmarkLabels(b, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabels_8(b *testing.B) {
|
|
|
|
benchmarkLabels(b, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLabels_16(b *testing.B) {
|
|
|
|
benchmarkLabels(b, 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: performance does not depend on label set size for the
|
|
|
|
// benchmarks below.
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func BenchmarkAcquireNewHandle(b *testing.B) {
|
2019-10-29 22:27:22 +02:00
|
|
|
fix := newFixture(b)
|
2019-11-06 20:54:36 +02:00
|
|
|
labelSets := makeLabelSets(b.N)
|
2019-10-29 22:27:22 +02:00
|
|
|
cnt := fix.sdk.NewInt64Counter("int64.counter")
|
2019-11-06 20:54:36 +02:00
|
|
|
labels := make([]metric.LabelSet, b.N)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
labels[i] = fix.sdk.Labels(labelSets[i]...)
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-06 20:54:36 +02:00
|
|
|
cnt.AcquireHandle(labels[i])
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func BenchmarkAcquireExistingHandle(b *testing.B) {
|
2019-10-29 22:27:22 +02:00
|
|
|
fix := newFixture(b)
|
2019-11-06 20:54:36 +02:00
|
|
|
labelSets := makeLabelSets(b.N)
|
2019-10-29 22:27:22 +02:00
|
|
|
cnt := fix.sdk.NewInt64Counter("int64.counter")
|
2019-11-06 20:54:36 +02:00
|
|
|
labels := make([]metric.LabelSet, b.N)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
labels[i] = fix.sdk.Labels(labelSets[i]...)
|
|
|
|
cnt.AcquireHandle(labels[i]).Release()
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-06 20:54:36 +02:00
|
|
|
cnt.AcquireHandle(labels[i])
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func BenchmarkAcquireReleaseExistingHandle(b *testing.B) {
|
|
|
|
fix := newFixture(b)
|
|
|
|
labelSets := makeLabelSets(b.N)
|
|
|
|
cnt := fix.sdk.NewInt64Counter("int64.counter")
|
|
|
|
labels := make([]metric.LabelSet, b.N)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
labels[i] = fix.sdk.Labels(labelSets[i]...)
|
|
|
|
cnt.AcquireHandle(labels[i]).Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
cnt.AcquireHandle(labels[i]).Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Counters
|
|
|
|
|
|
|
|
func BenchmarkInt64CounterAdd(b *testing.B) {
|
2019-10-29 22:27:22 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
cnt := fix.sdk.NewInt64Counter("int64.counter")
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-06 20:54:36 +02:00
|
|
|
cnt.Add(ctx, 1, labs)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func BenchmarkInt64CounterHandleAdd(b *testing.B) {
|
2019-10-29 22:27:22 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
2019-11-06 20:54:36 +02:00
|
|
|
cnt := fix.sdk.NewInt64Counter("int64.counter")
|
|
|
|
handle := cnt.AcquireHandle(labs)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-06 20:54:36 +02:00
|
|
|
handle.Add(ctx, 1)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 20:54:36 +02:00
|
|
|
func BenchmarkFloat64CounterAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
2019-10-29 22:27:22 +02:00
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
cnt := fix.sdk.NewFloat64Counter("float64.counter")
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2019-11-06 20:54:36 +02:00
|
|
|
cnt.Add(ctx, 1.1, labs)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64CounterHandleAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
cnt := fix.sdk.NewFloat64Counter("float64.counter")
|
|
|
|
handle := cnt.AcquireHandle(labs)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handle.Add(ctx, 1.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gauges
|
|
|
|
|
|
|
|
func BenchmarkInt64GaugeAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
gau := fix.sdk.NewInt64Gauge("int64.gauge")
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
gau.Set(ctx, int64(i), labs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInt64GaugeHandleAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
gau := fix.sdk.NewInt64Gauge("int64.gauge")
|
|
|
|
handle := gau.AcquireHandle(labs)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handle.Set(ctx, int64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64GaugeAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
gau := fix.sdk.NewFloat64Gauge("float64.gauge")
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
gau.Set(ctx, float64(i), labs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64GaugeHandleAdd(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
gau := fix.sdk.NewFloat64Gauge("float64.gauge")
|
|
|
|
handle := gau.AcquireHandle(labs)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handle.Set(ctx, float64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Measures
|
|
|
|
|
|
|
|
func benchmarkInt64MeasureAdd(b *testing.B, name string) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
mea := fix.sdk.NewInt64Measure(name)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mea.Record(ctx, int64(i), labs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkInt64MeasureHandleAdd(b *testing.B, name string) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
mea := fix.sdk.NewInt64Measure(name)
|
|
|
|
handle := mea.AcquireHandle(labs)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handle.Record(ctx, int64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkFloat64MeasureAdd(b *testing.B, name string) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
mea := fix.sdk.NewFloat64Measure(name)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
mea.Record(ctx, float64(i), labs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkFloat64MeasureHandleAdd(b *testing.B, name string) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fix := newFixture(b)
|
|
|
|
labs := fix.sdk.Labels(makeLabels(1)...)
|
|
|
|
mea := fix.sdk.NewFloat64Measure(name)
|
|
|
|
handle := mea.AcquireHandle(labs)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handle.Record(ctx, float64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxSumCount
|
|
|
|
|
|
|
|
func BenchmarkInt64MaxSumCountAdd(b *testing.B) {
|
2019-11-27 00:07:58 +02:00
|
|
|
benchmarkInt64MeasureAdd(b, "int64.minmaxsumcount")
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInt64MaxSumCountHandleAdd(b *testing.B) {
|
2019-11-27 00:07:58 +02:00
|
|
|
benchmarkInt64MeasureHandleAdd(b, "int64.minmaxsumcount")
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64MaxSumCountAdd(b *testing.B) {
|
2019-11-27 00:07:58 +02:00
|
|
|
benchmarkFloat64MeasureAdd(b, "float64.minmaxsumcount")
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64MaxSumCountHandleAdd(b *testing.B) {
|
2019-11-27 00:07:58 +02:00
|
|
|
benchmarkFloat64MeasureHandleAdd(b, "float64.minmaxsumcount")
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DDSketch
|
|
|
|
|
|
|
|
func BenchmarkInt64DDSketchAdd(b *testing.B) {
|
|
|
|
benchmarkInt64MeasureAdd(b, "int64.ddsketch")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInt64DDSketchHandleAdd(b *testing.B) {
|
|
|
|
benchmarkInt64MeasureHandleAdd(b, "int64.ddsketch")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64DDSketchAdd(b *testing.B) {
|
|
|
|
benchmarkFloat64MeasureAdd(b, "float64.ddsketch")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64DDSketchHandleAdd(b *testing.B) {
|
|
|
|
benchmarkFloat64MeasureHandleAdd(b, "float64.ddsketch")
|
|
|
|
}
|
2019-11-05 00:24:01 +02:00
|
|
|
|
|
|
|
// Array
|
|
|
|
|
|
|
|
func BenchmarkInt64ArrayAdd(b *testing.B) {
|
|
|
|
benchmarkInt64MeasureAdd(b, "int64.array")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInt64ArrayHandleAdd(b *testing.B) {
|
|
|
|
benchmarkInt64MeasureHandleAdd(b, "int64.array")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64ArrayAdd(b *testing.B) {
|
|
|
|
benchmarkFloat64MeasureAdd(b, "float64.array")
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFloat64ArrayHandleAdd(b *testing.B) {
|
|
|
|
benchmarkFloat64MeasureHandleAdd(b, "float64.array")
|
|
|
|
}
|