Skip to content

Commit

Permalink
add benchmark from open-telemetry#2393
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacd committed Jun 27, 2022
1 parent c3794bf commit b405c54
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions sdk/metric/aggregator/exponential/benchmark_test.go
@@ -0,0 +1,55 @@
package exponential

import (
"fmt"
"math/rand"
"testing"

"go.opentelemetry.io/otel/sdk/metric/aggregator/exponential/mapping"
"go.opentelemetry.io/otel/sdk/metric/aggregator/exponential/mapping/exponent"
"go.opentelemetry.io/otel/sdk/metric/aggregator/exponential/mapping/logarithm"
)

func benchmarkMapping(b *testing.B, name string, mapper mapping.Mapping) {
b.Run(fmt.Sprintf("mapping_%s", name), func(b *testing.B) {
src := rand.New(rand.NewSource(54979))

for i := 0; i < b.N; i++ {
_ = mapper.MapToIndex(1 + src.Float64())
}
})
}

func benchmarkBoundary(b *testing.B, name string, mapper mapping.Mapping) {
b.Run(fmt.Sprintf("boundary_%s", name), func(b *testing.B) {
src := rand.New(rand.NewSource(54979))

for i := 0; i < b.N; i++ {
_, _ = mapper.LowerBoundary(int32(src.Int63()))
}
})
}

// An earlier draft of this benchmark included a lookup-table based
// implementation:
// https://github.com/open-telemetry/opentelemetry-go-contrib/pull/1353
// That mapping function uses O(2^scale) extra space and falls
// somewhere between the exponent and logarithm methods compared here.
// In the test, lookuptable was 40% faster than logarithm, which did
// not justify the significant extra complexity.

// Benchmarks the MapToIndex function.
func BenchmarkMapping(b *testing.B) {
em, _ := exponent.NewMapping(-1)
lm, _ := logarithm.NewMapping(1)
benchmarkMapping(b, "exponent", em)
benchmarkMapping(b, "logarithm", lm)
}

// Benchmarks the LowerBoundary function.
func BenchmarkReverseMapping(b *testing.B) {
em, _ := exponent.NewMapping(-1)
lm, _ := logarithm.NewMapping(1)
benchmarkBoundary(b, "exponent", em)
benchmarkBoundary(b, "logarithm", lm)
}

0 comments on commit b405c54

Please sign in to comment.