Skip to content

Commit c3cd760

Browse files
committed
Add benchmarks for steady state
1 parent 66ae24e commit c3cd760

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
15+
# pylint: disable=invalid-name
16+
import itertools
17+
18+
from opentelemetry.sdk.metrics import MeterProvider
19+
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
20+
from opentelemetry.sdk.metrics.view import (
21+
ExplicitBucketHistogramAggregation,
22+
View,
23+
)
24+
25+
MAX_BOUND_VALUE = 10000
26+
27+
28+
def _generate_bounds(bound_count):
29+
bounds = []
30+
for i in range(bound_count):
31+
bounds.append(i * MAX_BOUND_VALUE / bound_count)
32+
return bounds
33+
34+
35+
hist_view_10 = View(
36+
instrument_name="test_histogram_10_bound",
37+
aggregation=ExplicitBucketHistogramAggregation(_generate_bounds(10)),
38+
)
39+
hist_view_49 = View(
40+
instrument_name="test_histogram_49_bound",
41+
aggregation=ExplicitBucketHistogramAggregation(_generate_bounds(49)),
42+
)
43+
hist_view_50 = View(
44+
instrument_name="test_histogram_50_bound",
45+
aggregation=ExplicitBucketHistogramAggregation(_generate_bounds(50)),
46+
)
47+
hist_view_1000 = View(
48+
instrument_name="test_histogram_1000_bound",
49+
aggregation=ExplicitBucketHistogramAggregation(_generate_bounds(1000)),
50+
)
51+
reader = InMemoryMetricReader()
52+
provider = MeterProvider(
53+
metric_readers=[reader],
54+
views=[
55+
hist_view_10,
56+
hist_view_49,
57+
hist_view_50,
58+
hist_view_1000,
59+
],
60+
)
61+
meter = provider.get_meter("sdk_meter_provider")
62+
hist = meter.create_histogram("test_histogram_default")
63+
hist10 = meter.create_histogram("test_histogram_10_bound")
64+
hist49 = meter.create_histogram("test_histogram_49_bound")
65+
hist50 = meter.create_histogram("test_histogram_50_bound")
66+
hist1000 = meter.create_histogram("test_histogram_1000_bound")
67+
68+
69+
def test_histogram_record(benchmark):
70+
values = itertools.cycle(_generate_bounds(10))
71+
72+
def benchmark_histogram_record():
73+
hist.record(next(values))
74+
75+
benchmark(benchmark_histogram_record)
76+
77+
78+
def test_histogram_record_10(benchmark):
79+
values = itertools.cycle(_generate_bounds(10))
80+
81+
def benchmark_histogram_record_10():
82+
hist10.record(next(values))
83+
84+
benchmark(benchmark_histogram_record_10)
85+
86+
87+
def test_histogram_record_49(benchmark):
88+
values = itertools.cycle(_generate_bounds(49))
89+
90+
def benchmark_histogram_record_49():
91+
hist49.record(next(values))
92+
93+
benchmark(benchmark_histogram_record_49)
94+
95+
96+
def test_histogram_record_50(benchmark):
97+
values = itertools.cycle(_generate_bounds(50))
98+
99+
def benchmark_histogram_record_50():
100+
hist50.record(next(values))
101+
102+
benchmark(benchmark_histogram_record_50)
103+
104+
105+
def test_histogram_record_1000(benchmark):
106+
values = itertools.cycle(_generate_bounds(1000))
107+
108+
def benchmark_histogram_record_1000():
109+
hist1000.record(next(values))
110+
111+
benchmark(benchmark_histogram_record_1000)

0 commit comments

Comments
 (0)