|
| 1 | +""" |
| 2 | +Benchmarks a locally evaluating SDK client against environment documents tiered by size. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import typing |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | +import responses |
| 11 | +from flag_engine.engine import ContextValue |
| 12 | + |
| 13 | +from flagsmith import Flagsmith |
| 14 | + |
| 15 | +API_URL = "http://flagsmith.benchmark/api/v1/" |
| 16 | +ENVIRONMENTS_DIR = Path(os.environ["SDK_BENCHMARKS_ENVIRONMENTS_DIR"]) |
| 17 | +IDENTITY_OVERRIDES_IDENTIFIER = "identity-override-1" |
| 18 | +IDENTIFIER = "user-1" |
| 19 | +TRAITS: typing.Mapping[str, ContextValue] = { |
| 20 | + "plan": "plan-7", |
| 21 | + "age": 30, |
| 22 | + "country": "GB", |
| 23 | + "version": "2.5.1", |
| 24 | + "email": "jane@example.com", |
| 25 | + "user_id": 12, |
| 26 | + "beta": True, |
| 27 | + "score": 95, |
| 28 | + "tier": "pro", |
| 29 | + "device": "iPhone 15", |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module", params=["small", "medium", "large"]) |
| 34 | +def client(request: pytest.FixtureRequest) -> Flagsmith: |
| 35 | + """ |
| 36 | + Build a locally evaluating client serving the environment document from memory. |
| 37 | + """ |
| 38 | + environment_document = (ENVIRONMENTS_DIR / f"{request.param}.json").read_bytes() |
| 39 | + |
| 40 | + with responses.RequestsMock() as mocked_responses: |
| 41 | + mocked_responses.get( |
| 42 | + f"{API_URL}environment-document/", |
| 43 | + body=environment_document, |
| 44 | + content_type="application/json", |
| 45 | + ) |
| 46 | + client = Flagsmith( |
| 47 | + environment_key="ser.benchmark", |
| 48 | + api_url=API_URL, |
| 49 | + enable_local_evaluation=True, |
| 50 | + ) |
| 51 | + client.environment_data_polling_manager_thread.stop() # Refreshed once, at startup only |
| 52 | + client.environment_data_polling_manager_thread.join() |
| 53 | + |
| 54 | + return client |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.benchmark |
| 58 | +def test_get_environment_flags(client: Flagsmith) -> None: |
| 59 | + """ |
| 60 | + Get a feature value from the environment flags. |
| 61 | + """ |
| 62 | + client.get_environment_flags().get_feature_value("feature_1") |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.benchmark |
| 66 | +def test_get_identity_flags_with_identity_overrides(client: Flagsmith) -> None: |
| 67 | + """ |
| 68 | + Check a feature for an identity with identity overrides. |
| 69 | + """ |
| 70 | + client.get_identity_flags(IDENTITY_OVERRIDES_IDENTIFIER).is_feature_enabled( |
| 71 | + "feature_8" |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.benchmark |
| 76 | +def test_get_identity_flags_no_traits(client: Flagsmith) -> None: |
| 77 | + """ |
| 78 | + Get a feature value for an identity without traits. |
| 79 | + """ |
| 80 | + client.get_identity_flags(IDENTIFIER).get_feature_value("feature_1") |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.benchmark |
| 84 | +def test_get_identity_flags_with_traits(client: Flagsmith) -> None: |
| 85 | + """ |
| 86 | + Get a feature value for an identity with traits. |
| 87 | + """ |
| 88 | + client.get_identity_flags(IDENTIFIER, TRAITS).get_feature_value("feature_1") |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.benchmark |
| 92 | +def test_get_identity_segments_with_traits(client: Flagsmith) -> None: |
| 93 | + """ |
| 94 | + List the segments of an identity with traits. |
| 95 | + """ |
| 96 | + client.get_identity_segments(IDENTIFIER, TRAITS) |
0 commit comments