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