forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add benchmark from open-telemetry#2393
- Loading branch information
Joshua MacDonald
committed
Jun 27, 2022
1 parent
c3794bf
commit b405c54
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |