Skip to content

Commit 8422e8f

Browse files
committed
add benchmark test for baggage
Signed-off-by: emdneto <[email protected]>
1 parent e01fa0c commit 8422e8f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import pytest
2+
3+
from opentelemetry import trace
4+
from opentelemetry.baggage import (
5+
clear,
6+
get_all,
7+
get_baggage,
8+
remove_baggage,
9+
set_baggage,
10+
)
11+
12+
tracer = trace.get_tracer(__name__)
13+
14+
15+
@pytest.fixture(params=[10, 100, 1000, 10000])
16+
def baggage_size(request):
17+
return request.param
18+
19+
20+
def set_baggage_operation(baggage_size=10):
21+
with tracer.start_span(name="root span"):
22+
ctx = get_all()
23+
for i in range(baggage_size):
24+
ctx = set_baggage(f"foo{i}", f"bar{i}", context=ctx)
25+
return ctx
26+
27+
28+
def test_set_baggage(benchmark, baggage_size):
29+
ctx = benchmark(set_baggage_operation, baggage_size)
30+
result = get_all(ctx)
31+
assert len(result) == baggage_size
32+
33+
34+
def test_get_baggage(benchmark, baggage_size):
35+
ctx = set_baggage_operation(baggage_size)
36+
37+
def get_baggage_operation():
38+
return [get_baggage(f"foo{i}", ctx) for i in range(baggage_size)]
39+
40+
result = benchmark(get_baggage_operation)
41+
assert result == [f"bar{i}" for i in range(baggage_size)]
42+
43+
44+
def test_remove_baggage(benchmark, baggage_size):
45+
ctx = set_baggage_operation(baggage_size)
46+
47+
def remove_operation():
48+
tmp_ctx = ctx
49+
for i in range(baggage_size):
50+
tmp_ctx = remove_baggage(f"foo{i}", tmp_ctx)
51+
return tmp_ctx
52+
53+
cleared_context = benchmark(remove_operation)
54+
result = get_all(cleared_context)
55+
# After removing all baggage items, it should be empty.
56+
assert len(result) == 0
57+
58+
59+
def test_clear_baggage(benchmark, baggage_size):
60+
ctx = set_baggage_operation(baggage_size)
61+
62+
def clear_operation():
63+
return clear(ctx)
64+
65+
cleared_context = benchmark(clear_operation)
66+
result = get_all(cleared_context)
67+
# After clearing the baggage should be empty.
68+
assert len(result) == 0

0 commit comments

Comments
 (0)