Skip to content

Commit

Permalink
benchmark experimentation (#4811)
Browse files Browse the repository at this point in the history
* benchmark experimentation

* do the same for test_evaluate_page

* import templates beforehands

* add auto reload

* disable extensions
  • Loading branch information
adhami3310 authored Feb 12, 2025
1 parent dd5b817 commit d79366d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
3 changes: 1 addition & 2 deletions reflex/compiler/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ class ReflexJinjaEnvironment(Environment):

def __init__(self) -> None:
"""Set default environment."""
extensions = ["jinja2.ext.debug"]
super().__init__(
extensions=extensions,
trim_blocks=True,
lstrip_blocks=True,
auto_reload=False,
)
self.filters["json_dumps"] = json_dumps
self.filters["react_setter"] = lambda state: f"set{state.capitalize()}"
Expand Down
4 changes: 2 additions & 2 deletions tests/benchmarks/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ def _stateful_page():


@pytest.fixture(params=[_simple_page, _complicated_page, _stateful_page])
def unevaluated_page(request):
def unevaluated_page(request: pytest.FixtureRequest):
return request.param


@pytest.fixture(params=[_simple_page, _complicated_page, _stateful_page])
def evaluated_page(request):
def evaluated_page(request: pytest.FixtureRequest):
return request.param()
27 changes: 17 additions & 10 deletions tests/benchmarks/test_compilation.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import pytest
from pytest_codspeed import BenchmarkFixture

from reflex.compiler.compiler import _compile_page, _compile_stateful_components
from reflex.components.component import Component


@pytest.mark.benchmark
def test_compile_page(evaluated_page):
_compile_page(evaluated_page, None)
def import_templates():
# Importing the templates module to avoid the import time in the benchmark
import reflex.compiler.templates # noqa: F401


@pytest.mark.benchmark
def test_compile_stateful(evaluated_page):
_compile_stateful_components([evaluated_page])
def test_compile_page(evaluated_page: Component, benchmark: BenchmarkFixture):
import_templates()

benchmark(lambda: _compile_page(evaluated_page, None))

@pytest.mark.benchmark
def test_get_all_imports(evaluated_page):
evaluated_page._get_all_imports()

def test_compile_stateful(evaluated_page: Component, benchmark: BenchmarkFixture):
import_templates()

benchmark(lambda: _compile_stateful_components([evaluated_page]))


def test_get_all_imports(evaluated_page: Component, benchmark: BenchmarkFixture):
benchmark(lambda: evaluated_page._get_all_imports())
13 changes: 9 additions & 4 deletions tests/benchmarks/test_evaluate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest
from typing import Callable

from pytest_codspeed import BenchmarkFixture

@pytest.mark.benchmark
def test_evaluate_page(unevaluated_page):
unevaluated_page()
from reflex.components.component import Component


def test_evaluate_page(
unevaluated_page: Callable[[], Component], benchmark: BenchmarkFixture
):
benchmark(unevaluated_page)

0 comments on commit d79366d

Please sign in to comment.