Skip to content

Commit ebb8b61

Browse files
committed
Add benchmark for creating Event objects
Create Event from various sources.
1 parent a84e29b commit ebb8b61

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""Benchmarks for event creation and conversion APIs."""
2+
3+
from typing import Any
4+
5+
import pytest
6+
from pytest_codspeed import BenchmarkFixture
7+
from reflex_base.event import Event, EventHandler, EventSpec
8+
9+
import reflex as rx
10+
11+
from .fixtures import BenchmarkState
12+
13+
14+
def test_console_log(benchmark: BenchmarkFixture):
15+
"""Benchmark creation of an ``rx.console_log`` event spec.
16+
17+
Args:
18+
benchmark: The codspeed benchmark fixture.
19+
"""
20+
benchmark(lambda: rx.console_log("hello"))
21+
22+
23+
@pytest.fixture(scope="module")
24+
def increment_handler() -> EventHandler:
25+
"""The ``increment`` EventHandler from ``BenchmarkState``.
26+
27+
Returns:
28+
The state-bound EventHandler used by all ``from_event_type`` benchmarks.
29+
"""
30+
return BenchmarkState.event_handlers["increment"]
31+
32+
33+
@pytest.fixture(scope="module")
34+
def increment_spec(increment_handler: EventHandler) -> EventSpec:
35+
"""An EventSpec produced by calling the increment EventHandler.
36+
37+
Args:
38+
increment_handler: The state-bound EventHandler.
39+
40+
Returns:
41+
The EventSpec produced by invoking the handler with no args.
42+
"""
43+
return increment_handler()
44+
45+
46+
@pytest.fixture(scope="module")
47+
def increment_event(increment_spec: EventSpec) -> Event:
48+
"""An Event built from the increment EventSpec.
49+
50+
Args:
51+
increment_spec: The EventSpec for the increment handler.
52+
53+
Returns:
54+
The runtime Event derived from the EventSpec.
55+
"""
56+
return Event.from_event_type(increment_spec)[0]
57+
58+
59+
@pytest.fixture(
60+
params=(
61+
"event",
62+
"event_spec",
63+
"event_handler",
64+
"lambda_event",
65+
"lambda_event_spec",
66+
"lambda_event_handler",
67+
)
68+
)
69+
def event_input(
70+
request: pytest.FixtureRequest,
71+
increment_event: Event,
72+
increment_spec: EventSpec,
73+
increment_handler: EventHandler,
74+
) -> Any:
75+
"""Parametrized event-like input accepted by ``Event.from_event_type``.
76+
77+
Args:
78+
request: The pytest fixture request carrying the param id.
79+
increment_event: The pre-built runtime Event.
80+
increment_spec: The pre-built EventSpec.
81+
increment_handler: The state-bound EventHandler.
82+
83+
Returns:
84+
One of the supported input shapes for ``Event.from_event_type``.
85+
86+
Raises:
87+
ValueError: If the parametrized value is unrecognized.
88+
"""
89+
inputs: dict[str, Any] = {
90+
"event": increment_event,
91+
"event_spec": increment_spec,
92+
"event_handler": increment_handler,
93+
"lambda_event": lambda: increment_event,
94+
"lambda_event_spec": lambda: increment_spec,
95+
"lambda_event_handler": lambda: increment_handler,
96+
}
97+
try:
98+
return inputs[request.param]
99+
except KeyError as e:
100+
msg = f"Unknown event_input param: {request.param}"
101+
raise ValueError(msg) from e
102+
103+
104+
def test_from_event_type(event_input: Any, benchmark: BenchmarkFixture):
105+
"""Benchmark ``Event.from_event_type`` for each supported input shape.
106+
107+
Covers existing Event, EventSpec (called EventHandler), EventHandler,
108+
and lambdas returning each of those — the common shapes encountered
109+
when normalizing user-returned event values.
110+
111+
Args:
112+
event_input: The parametrized event-like input.
113+
benchmark: The codspeed benchmark fixture.
114+
"""
115+
benchmark(lambda: Event.from_event_type(event_input))

0 commit comments

Comments
 (0)