1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package histogram_test
16
17import (
18	"context"
19	"math/rand"
20	"testing"
21
22	"go.opentelemetry.io/otel/metric/number"
23	"go.opentelemetry.io/otel/metric/sdkapi"
24	"go.opentelemetry.io/otel/sdk/metric/aggregator/aggregatortest"
25	"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
26)
27
28const inputRange = 1e6
29
30func benchmarkHistogramSearchFloat64(b *testing.B, size int) {
31	boundaries := make([]float64, size)
32
33	for i := range boundaries {
34		boundaries[i] = rand.Float64() * inputRange
35	}
36
37	values := make([]float64, b.N)
38	for i := range values {
39		values[i] = rand.Float64() * inputRange
40	}
41	desc := aggregatortest.NewAggregatorTest(sdkapi.HistogramInstrumentKind, number.Float64Kind)
42	agg := &histogram.New(1, desc, histogram.WithExplicitBoundaries(boundaries))[0]
43	ctx := context.Background()
44
45	b.ReportAllocs()
46	b.ResetTimer()
47
48	for i := 0; i < b.N; i++ {
49		_ = agg.Update(ctx, number.NewFloat64Number(values[i]), desc)
50	}
51}
52
53func BenchmarkHistogramSearchFloat64_1(b *testing.B) {
54	benchmarkHistogramSearchFloat64(b, 1)
55}
56func BenchmarkHistogramSearchFloat64_8(b *testing.B) {
57	benchmarkHistogramSearchFloat64(b, 8)
58}
59func BenchmarkHistogramSearchFloat64_16(b *testing.B) {
60	benchmarkHistogramSearchFloat64(b, 16)
61}
62func BenchmarkHistogramSearchFloat64_32(b *testing.B) {
63	benchmarkHistogramSearchFloat64(b, 32)
64}
65func BenchmarkHistogramSearchFloat64_64(b *testing.B) {
66	benchmarkHistogramSearchFloat64(b, 64)
67}
68func BenchmarkHistogramSearchFloat64_128(b *testing.B) {
69	benchmarkHistogramSearchFloat64(b, 128)
70}
71func BenchmarkHistogramSearchFloat64_256(b *testing.B) {
72	benchmarkHistogramSearchFloat64(b, 256)
73}
74func BenchmarkHistogramSearchFloat64_512(b *testing.B) {
75	benchmarkHistogramSearchFloat64(b, 512)
76}
77func BenchmarkHistogramSearchFloat64_1024(b *testing.B) {
78	benchmarkHistogramSearchFloat64(b, 1024)
79}
80
81func benchmarkHistogramSearchInt64(b *testing.B, size int) {
82	boundaries := make([]float64, size)
83
84	for i := range boundaries {
85		boundaries[i] = rand.Float64() * inputRange
86	}
87
88	values := make([]int64, b.N)
89	for i := range values {
90		values[i] = int64(rand.Float64() * inputRange)
91	}
92	desc := aggregatortest.NewAggregatorTest(sdkapi.HistogramInstrumentKind, number.Int64Kind)
93	agg := &histogram.New(1, desc, histogram.WithExplicitBoundaries(boundaries))[0]
94	ctx := context.Background()
95
96	b.ReportAllocs()
97	b.ResetTimer()
98
99	for i := 0; i < b.N; i++ {
100		_ = agg.Update(ctx, number.NewInt64Number(values[i]), desc)
101	}
102}
103
104func BenchmarkHistogramSearchInt64_1(b *testing.B) {
105	benchmarkHistogramSearchInt64(b, 1)
106}
107func BenchmarkHistogramSearchInt64_8(b *testing.B) {
108	benchmarkHistogramSearchInt64(b, 8)
109}
110func BenchmarkHistogramSearchInt64_16(b *testing.B) {
111	benchmarkHistogramSearchInt64(b, 16)
112}
113func BenchmarkHistogramSearchInt64_32(b *testing.B) {
114	benchmarkHistogramSearchInt64(b, 32)
115}
116func BenchmarkHistogramSearchInt64_64(b *testing.B) {
117	benchmarkHistogramSearchInt64(b, 64)
118}
119func BenchmarkHistogramSearchInt64_128(b *testing.B) {
120	benchmarkHistogramSearchInt64(b, 128)
121}
122func BenchmarkHistogramSearchInt64_256(b *testing.B) {
123	benchmarkHistogramSearchInt64(b, 256)
124}
125func BenchmarkHistogramSearchInt64_512(b *testing.B) {
126	benchmarkHistogramSearchInt64(b, 512)
127}
128func BenchmarkHistogramSearchInt64_1024(b *testing.B) {
129	benchmarkHistogramSearchInt64(b, 1024)
130}
131