From 8422e8f76e46626c283ecdf68601856669df4b2f Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Mon, 10 Mar 2025 11:47:41 -0300 Subject: [PATCH 1/2] add benchmark test for baggage Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- opentelemetry-sdk/benchmarks/test_baggage.py | 68 ++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 opentelemetry-sdk/benchmarks/test_baggage.py diff --git a/opentelemetry-sdk/benchmarks/test_baggage.py b/opentelemetry-sdk/benchmarks/test_baggage.py new file mode 100644 index 00000000000..fad3d8ce50c --- /dev/null +++ b/opentelemetry-sdk/benchmarks/test_baggage.py @@ -0,0 +1,68 @@ +import pytest + +from opentelemetry import trace +from opentelemetry.baggage import ( + clear, + get_all, + get_baggage, + remove_baggage, + set_baggage, +) + +tracer = trace.get_tracer(__name__) + + +@pytest.fixture(params=[10, 100, 1000, 10000]) +def baggage_size(request): + return request.param + + +def set_baggage_operation(baggage_size=10): + with tracer.start_span(name="root span"): + ctx = get_all() + for i in range(baggage_size): + ctx = set_baggage(f"foo{i}", f"bar{i}", context=ctx) + return ctx + + +def test_set_baggage(benchmark, baggage_size): + ctx = benchmark(set_baggage_operation, baggage_size) + result = get_all(ctx) + assert len(result) == baggage_size + + +def test_get_baggage(benchmark, baggage_size): + ctx = set_baggage_operation(baggage_size) + + def get_baggage_operation(): + return [get_baggage(f"foo{i}", ctx) for i in range(baggage_size)] + + result = benchmark(get_baggage_operation) + assert result == [f"bar{i}" for i in range(baggage_size)] + + +def test_remove_baggage(benchmark, baggage_size): + ctx = set_baggage_operation(baggage_size) + + def remove_operation(): + tmp_ctx = ctx + for i in range(baggage_size): + tmp_ctx = remove_baggage(f"foo{i}", tmp_ctx) + return tmp_ctx + + cleared_context = benchmark(remove_operation) + result = get_all(cleared_context) + # After removing all baggage items, it should be empty. + assert len(result) == 0 + + +def test_clear_baggage(benchmark, baggage_size): + ctx = set_baggage_operation(baggage_size) + + def clear_operation(): + return clear(ctx) + + cleared_context = benchmark(clear_operation) + result = get_all(cleared_context) + # After clearing the baggage should be empty. + assert len(result) == 0 From 17b9b63f492264e0f52b065fee4f29630ffe80df Mon Sep 17 00:00:00 2001 From: emdneto <9735060+emdneto@users.noreply.github.com> Date: Mon, 10 Mar 2025 12:05:34 -0300 Subject: [PATCH 2/2] ignore pylint Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --- opentelemetry-sdk/benchmarks/test_baggage.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opentelemetry-sdk/benchmarks/test_baggage.py b/opentelemetry-sdk/benchmarks/test_baggage.py index fad3d8ce50c..4ec331a5b8b 100644 --- a/opentelemetry-sdk/benchmarks/test_baggage.py +++ b/opentelemetry-sdk/benchmarks/test_baggage.py @@ -1,3 +1,4 @@ +# pylint: disable=redefined-outer-name, invalid-name import pytest from opentelemetry import trace @@ -17,10 +18,10 @@ def baggage_size(request): return request.param -def set_baggage_operation(baggage_size=10): +def set_baggage_operation(size=10): with tracer.start_span(name="root span"): ctx = get_all() - for i in range(baggage_size): + for i in range(size): ctx = set_baggage(f"foo{i}", f"bar{i}", context=ctx) return ctx