-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Codspeed benchmark for backend state event processing #6290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+135
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| """Benchmark for the event processing pipeline. | ||
|
|
||
| Measures the time from calling the ``process`` function (the core of | ||
| ``on_event``) to collecting all emitted ``StateUpdate`` deltas via | ||
| ``contextlib.aclosing`` over the async generator. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import contextlib | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| import pytest_asyncio | ||
| from pytest_codspeed import BenchmarkFixture | ||
| from reflex_base.utils.format import format_event_handler | ||
|
|
||
| from reflex.app import App, process | ||
| from reflex.event import Event | ||
| from reflex.istate.manager.memory import StateManagerMemory | ||
| from reflex.state import State | ||
|
|
||
| from .fixtures import BenchmarkState | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def app_module_mock(monkeypatch) -> mock.Mock: | ||
| """Mock the app module so state machinery can resolve the app. | ||
|
|
||
| Args: | ||
| monkeypatch: pytest monkeypatch fixture. | ||
|
|
||
| Returns: | ||
| The mock for the main app module. | ||
| """ | ||
| from reflex.utils import prerequisites | ||
|
|
||
| app_module_mock = mock.Mock() | ||
| get_app_mock = mock.Mock(return_value=app_module_mock) | ||
| monkeypatch.setattr(prerequisites, "get_app", get_app_mock) | ||
| return app_module_mock | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture | ||
| async def event_processing_harness(app_module_mock: mock.Mock): | ||
| """Set up the full event processing pipeline for benchmarking. | ||
|
|
||
| Creates an App wired to a real StateManagerMemory. The ``process`` | ||
| function is called directly (bypassing Socket.IO) and StateUpdates | ||
| are collected and counted to verify correctness. | ||
|
|
||
| Args: | ||
| app_module_mock: The mocked app module. | ||
|
|
||
| Yields: | ||
| An async callable that processes the given events and asserts | ||
| the expected number of deltas were produced. | ||
| """ | ||
| app = app_module_mock.app = App() | ||
| state_manager = StateManagerMemory(state=State) | ||
| app._state_manager = state_manager | ||
| # Disable event namespace so process() doesn't try to emit "reload" | ||
| # via Socket.IO for brand-new states. | ||
| app._event_namespace = None | ||
|
|
||
| token = "benchmark-token" | ||
| sid = "benchmark-sid" | ||
| headers: dict = {} | ||
| client_ip = "127.0.0.1" | ||
|
|
||
| handler_name = format_event_handler(BenchmarkState.event_handlers["increment"]) | ||
|
|
||
| event = Event( | ||
| token=token, | ||
| name=handler_name, | ||
| router_data={ | ||
| "query": {}, | ||
| "path": "/", | ||
| }, | ||
| payload={}, | ||
| ) | ||
|
|
||
| delta_count = 0 | ||
| expected_deltas = 0 | ||
|
|
||
| async def run_events(num_events: int, num_expected_deltas: int) -> None: | ||
| """Process events through the pipeline and wait for deltas. | ||
|
|
||
| Args: | ||
| num_events: Number of increment events to process. | ||
| num_expected_deltas: How many StateUpdates to wait for. | ||
| """ | ||
| nonlocal delta_count, expected_deltas | ||
| delta_count = 0 | ||
| expected_deltas = num_expected_deltas | ||
|
|
||
| for _ in range(num_events): | ||
| async with contextlib.aclosing( | ||
| process(app, event, sid, headers, client_ip) | ||
| ) as updates: | ||
| async for _update in updates: | ||
| delta_count += 1 | ||
masenf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| assert delta_count == expected_deltas, ( | ||
| f"Expected {expected_deltas} StateUpdate(s), got {delta_count}" | ||
| ) | ||
|
|
||
| yield run_events | ||
|
|
||
| await state_manager.close() | ||
|
|
||
|
|
||
| def test_process_event( | ||
| event_processing_harness, | ||
| benchmark: BenchmarkFixture, | ||
| ): | ||
| """Benchmark processing 3 increment events through the full pipeline. | ||
|
|
||
| The first event creates fresh state (cold path), the next two reuse | ||
| the existing state (warm path). All machinery is set up outside the | ||
| benchmark; only the event processing is timed. | ||
|
|
||
| Args: | ||
| event_processing_harness: The run_events async callable. | ||
| benchmark: The codspeed benchmark fixture. | ||
| """ | ||
| run_events = event_processing_harness | ||
| loop = asyncio.get_event_loop() | ||
masenf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Each call to process() for a non-background event yields StateUpdates. | ||
| # The _process_event generator yields one update per yield/return plus a | ||
| # final update. For a simple handler like increment() with no yield, | ||
| # we get 1 StateUpdate per event = 3 total. | ||
| @benchmark | ||
| def _(): | ||
| loop.run_until_complete(run_events(num_events=3, num_expected_deltas=3)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.