Skip to content

Commit 5aea57d

Browse files
committed
add basic set of benchmarks
1 parent 7d5ea5d commit 5aea57d

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest-benchmark==4.0.0
2+
-r test-requirements.txt
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import sys
16+
from pathlib import Path
17+
18+
_pkg_root = str(Path(__file__).resolve().parent.parent)
19+
20+
21+
def pytest_configure(config):
22+
"""Add the package root to sys.path so 'from tests import ...' works."""
23+
if _pkg_root not in sys.path:
24+
sys.path.insert(0, _pkg_root)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import pytest
16+
17+
from opentelemetry._logs import SeverityNumber
18+
from opentelemetry.exporter.otlp.json.common._log_encoder import encode_logs
19+
from tests import TIME, make_log, make_log_context
20+
21+
22+
@pytest.mark.parametrize("batch_size", [1, 10, 100])
23+
def test_benchmark_encode_logs(benchmark, batch_size):
24+
ctx = make_log_context()
25+
logs = [
26+
make_log(
27+
body=f"log message {i}",
28+
context=ctx,
29+
timestamp=TIME + i,
30+
observed_timestamp=TIME + i + 1000,
31+
severity_text="INFO",
32+
severity_number=SeverityNumber.INFO,
33+
)
34+
for i in range(batch_size)
35+
]
36+
37+
benchmark(encode_logs, logs)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
from opentelemetry.exporter.otlp.json.common.metrics_encoder import (
16+
encode_metrics,
17+
)
18+
from opentelemetry.sdk.metrics import Exemplar
19+
from tests import (
20+
SPAN_ID,
21+
TIME,
22+
TRACE_ID,
23+
make_exponential_histogram,
24+
make_gauge,
25+
make_histogram,
26+
make_metrics_data,
27+
make_sum,
28+
)
29+
30+
31+
def test_benchmark_encode_sum(benchmark):
32+
data = make_metrics_data([make_sum()])
33+
benchmark(encode_metrics, data)
34+
35+
36+
def test_benchmark_encode_gauge(benchmark):
37+
data = make_metrics_data([make_gauge()])
38+
benchmark(encode_metrics, data)
39+
40+
41+
def test_benchmark_encode_histogram(benchmark):
42+
data = make_metrics_data([
43+
make_histogram(
44+
exemplars=[
45+
Exemplar({"sampled": "true"}, 298.0, TIME, SPAN_ID, TRACE_ID),
46+
],
47+
)
48+
])
49+
benchmark(encode_metrics, data)
50+
51+
52+
def test_benchmark_encode_exponential_histogram(benchmark):
53+
data = make_metrics_data([make_exponential_histogram()])
54+
benchmark(encode_metrics, data)
55+
56+
57+
def test_benchmark_encode_mixed_metrics(benchmark):
58+
data = make_metrics_data([
59+
make_sum(name="counter"),
60+
make_gauge(name="gauge"),
61+
make_histogram(
62+
name="histogram",
63+
exemplars=[
64+
Exemplar({"sampled": "true"}, 298.0, TIME, SPAN_ID, TRACE_ID),
65+
],
66+
),
67+
make_exponential_histogram(name="exp_histogram"),
68+
])
69+
benchmark(encode_metrics, data)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import pytest
16+
17+
from opentelemetry.exporter.otlp.json.common.trace_encoder import encode_spans
18+
from opentelemetry.sdk.resources import Resource
19+
from opentelemetry.sdk.trace import Event, SpanContext
20+
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
21+
from opentelemetry.trace import Link
22+
from opentelemetry.trace.status import Status, StatusCode
23+
24+
from tests import BASE_TIME, TRACE_ID, make_span
25+
26+
27+
def test_benchmark_encode_span_with_events_and_links(benchmark):
28+
link_ctx = SpanContext(TRACE_ID, 0x2222222222222222, is_remote=False)
29+
span = make_span(
30+
events=tuple(
31+
Event(
32+
name=f"event_{i}",
33+
timestamp=BASE_TIME + i * 10**6,
34+
attributes={"event_key": f"val_{i}"},
35+
)
36+
for i in range(5)
37+
),
38+
links=tuple(
39+
Link(context=link_ctx, attributes={"link_key": True})
40+
for _ in range(3)
41+
),
42+
resource=Resource({"service.name": "bench-svc"}),
43+
instrumentation_scope=InstrumentationScope("bench_lib", "1.0"),
44+
)
45+
span._status = Status(StatusCode.ERROR, "benchmark error")
46+
47+
benchmark(encode_spans, [span])
48+
49+
50+
@pytest.mark.parametrize("batch_size", [1, 10, 100])
51+
def test_benchmark_encode_spans(benchmark, batch_size):
52+
spans = [
53+
make_span(name=f"span-{i}", span_id=0x1000 + i)
54+
for i in range(batch_size)
55+
]
56+
57+
benchmark(encode_spans, spans)

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ envlist =
5555
py3{9,10,11,12,13,14,14t}-test-opentelemetry-exporter-otlp-json-common
5656
pypy3-test-opentelemetry-exporter-otlp-json-common
5757
lint-opentelemetry-exporter-otlp-json-common
58+
benchmark-opentelemetry-exporter-otlp-json-common
5859

5960
; opentelemetry-exporter-otlp
6061
py3{9,10,11,12,13,14}-test-opentelemetry-exporter-otlp-combined
@@ -138,6 +139,7 @@ deps =
138139
exporter-otlp-proto-common: -r {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-common/test-requirements.txt
139140

140141
exporter-otlp-json-common: -r {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common/test-requirements.txt
142+
benchmark-exporter-otlp-json-common: -r {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common/benchmark-requirements.txt
141143

142144
exporter-otlp-combined: -r {toxinidir}/exporter/opentelemetry-exporter-otlp/test-requirements.txt
143145

@@ -234,6 +236,7 @@ commands =
234236

235237
test-opentelemetry-exporter-otlp-json-common: pytest {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common/tests {posargs}
236238
lint-opentelemetry-exporter-otlp-json-common: sh -c "cd exporter && pylint --prefer-stubs yes --rcfile ../.pylintrc {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common"
239+
benchmark-opentelemetry-exporter-otlp-json-common: pytest {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common/benchmarks --benchmark-json=exporter-otlp-json-common-benchmark.json {posargs}
237240

238241
test-opentelemetry-exporter-otlp-combined: pytest {toxinidir}/exporter/opentelemetry-exporter-otlp/tests {posargs}
239242
lint-opentelemetry-exporter-otlp-combined: sh -c "cd exporter && pylint --rcfile ../.pylintrc {toxinidir}/exporter/opentelemetry-exporter-otlp"

0 commit comments

Comments
 (0)