1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-02-03 13:11:53 +02:00

Add benchmark

This commit is contained in:
jmacd 2020-05-21 00:19:08 -07:00
parent 63df1b5e22
commit 0b5080372a
3 changed files with 110 additions and 23 deletions

View File

@ -92,6 +92,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.opentelemetry.io v0.1.0 h1:EANZoRCOP+A3faIlw/iN6YEWoYb1vleZRKm1EvH8T48=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

View File

@ -20,13 +20,11 @@ import (
"net/http"
"sync"
"go.opentelemetry.io/otel/api/metric"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/label"
"go.opentelemetry.io/otel/api/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregator"
"go.opentelemetry.io/otel/sdk/metric/controller/pull"
@ -203,7 +201,7 @@ func (c *collector) Describe(ch chan<- *prometheus.Desc) {
defer c.exp.lock.RUnlock()
_ = c.exp.Controller().ForEach(func(record export.Record) error {
ch <- c.toDesc(&record)
ch <- c.toDesc(record)
return nil
})
}
@ -222,9 +220,8 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
err := ctrl.ForEach(func(record export.Record) error {
agg := record.Aggregator()
numberKind := record.Descriptor().NumberKind()
// TODO: Use the resource value in this record.
labels := labelValues(record.Labels())
desc := c.toDesc(&record)
labels := labelValues(record)
desc := c.toDesc(record)
if hist, ok := agg.(aggregator.Histogram); ok {
if err := c.exportHistogram(ch, hist, numberKind, desc, labels); err != nil {
@ -346,30 +343,35 @@ func (c *collector) exportHistogram(ch chan<- prometheus.Metric, hist aggregator
return nil
}
func (c *collector) toDesc(record *export.Record) *prometheus.Desc {
func (c *collector) toDesc(record export.Record) *prometheus.Desc {
desc := record.Descriptor()
labels := labelsKeys(record.Labels())
labels := labelsKeys(record)
return prometheus.NewDesc(sanitize(desc.Name()), desc.Description(), labels, nil)
}
func labelsKeys(labels *label.Set) []string {
iter := labels.Iter()
keys := make([]string, 0, iter.Len())
for iter.Next() {
kv := iter.Label()
keys = append(keys, sanitize(string(kv.Key)))
func labelsKeys(record export.Record) []string {
iter1 := record.Resource().Iter()
iter2 := record.Labels().Iter()
keys := make([]string, 0, iter1.Len()+iter2.Len())
for iter1.Next() {
keys = append(keys, sanitize(string(iter1.Label().Key)))
}
for iter2.Next() {
keys = append(keys, sanitize(string(iter2.Label().Key)))
}
return keys
}
func labelValues(labels *label.Set) []string {
// TODO(paivagustavo): parse the labels.Encoded() instead of calling `Emit()` directly
// this would avoid unnecessary allocations.
iter := labels.Iter()
values := make([]string, 0, iter.Len())
for iter.Next() {
label := iter.Label()
values = append(values, label.Value.Emit())
func labelValues(record export.Record) []string {
iter1 := record.Resource().Iter()
iter2 := record.Labels().Iter()
values := make([]string, 0, iter1.Len()+iter2.Len())
for iter1.Next() {
values = append(values, iter1.Label().Value.Emit())
}
for iter2.Next() {
values = append(values, iter2.Label().Value.Emit())
}
return values
}

View File

@ -0,0 +1,84 @@
// 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 resource_test
import (
"fmt"
"math/rand"
"testing"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/sdk/resource"
)
const conflict = 0.5
func makeLabels(n int) (_, _ *resource.Resource) {
used := map[string]bool{}
l1 := make([]kv.KeyValue, n)
l2 := make([]kv.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
}
}
l1[i] = kv.String(k, fmt.Sprint("v", rand.Intn(1000000000)))
if rand.Float64() < conflict {
l2[i] = l1[i]
} else {
l2[i] = kv.String(k, fmt.Sprint("v", rand.Intn(1000000000)))
}
}
return resource.New(l1...), resource.New(l2...)
}
func benchmarkMergeResource(b *testing.B, size int) {
r1, r2 := makeLabels(size)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = resource.Merge(r1, r2)
}
}
func BenchmarkMergeResource_1(b *testing.B) {
benchmarkMergeResource(b, 1)
}
func BenchmarkMergeResource_2(b *testing.B) {
benchmarkMergeResource(b, 2)
}
func BenchmarkMergeResource_3(b *testing.B) {
benchmarkMergeResource(b, 3)
}
func BenchmarkMergeResource_4(b *testing.B) {
benchmarkMergeResource(b, 4)
}
func BenchmarkMergeResource_6(b *testing.B) {
benchmarkMergeResource(b, 6)
}
func BenchmarkMergeResource_8(b *testing.B) {
benchmarkMergeResource(b, 8)
}
func BenchmarkMergeResource_16(b *testing.B) {
benchmarkMergeResource(b, 16)
}