Skip to content

Commit

Permalink
add test for blank template with two pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ElijahAhianyo committed Mar 5, 2024
1 parent 37f0df0 commit 3eb36f2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 27 deletions.
2 changes: 1 addition & 1 deletion integration/benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os
import sys
from typing import List, Dict
from typing import Dict, List

import pytest
from helpers import insert_benchmarking_data
Expand Down
2 changes: 1 addition & 1 deletion integration/benchmarks/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
from datetime import datetime
from typing import List, Dict
from typing import Dict, List

import psycopg2

Expand Down
182 changes: 157 additions & 25 deletions integration/benchmarks/test_blank_app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
import os
import time

from typing import Generator

import pytest
from selenium.webdriver.common.by import By

from reflex.testing import DEFAULT_TIMEOUT, AppHarness, WebDriver, chdir
from reflex import constants
from reflex.testing import AppHarness, chdir
from reflex.utils import build, path_ops


def BlankTemplate():
Expand All @@ -20,6 +20,7 @@ def BlankTemplate():

class State(rx.State):
"""The app state."""

pass

def index() -> rx.Component:
Expand All @@ -44,6 +45,108 @@ def index() -> rx.Component:
app.add_page(index)


def BlankTemplate2():
"""Test that background tasks work as expected."""
from rxconfig import config

import reflex as rx

docs_url = "https://reflex.dev/docs/getting-started/introduction/"
filename = f"{config.app_name}/{config.app_name}.py"
college = [
"Stanford University",
"Arizona",
"Arizona state",
"Baylor",
"Boston College",
"Boston University",
]

class State(rx.State):
"""The app state."""

position: str
college: str
age: tuple[int, int] = (18, 50)
salary: tuple[int, int] = (0, 25000000)

def index() -> rx.Component:
return rx.center(
rx.theme_panel(),
rx.vstack(
rx.heading("Welcome to Reflex!", size="9"),
rx.text("Get started by editing ", rx.code(filename)),
rx.button(
"Check out our docs!",
on_click=lambda: rx.redirect(docs_url),
size="4",
),
align="center",
spacing="7",
font_size="2em",
),
height="100vh",
)

def selection() -> rx.Component:
return rx.vstack(
rx.hstack(
rx.vstack(
rx.select(
["C", "PF", "SF", "PG", "SG"],
placeholder="Select a position. (All)",
on_change=State.set_position,
size="3",
),
rx.select(
college,
placeholder="Select a college. (All)",
on_change=State.set_college,
size="3",
),
),
rx.vstack(
rx.vstack(
rx.hstack(
rx.badge("Min Age: ", State.age[0]),
rx.divider(orientation="vertical"),
rx.badge("Max Age: ", State.age[1]),
),
rx.slider(
default_value=[18, 50],
min=18,
max=50,
on_value_commit=State.set_age,
),
align_items="left",
width="100%",
),
rx.vstack(
rx.hstack(
rx.badge("Min Sal: ", State.salary[0] // 1000000, "M"),
rx.divider(orientation="vertical"),
rx.badge("Max Sal: ", State.salary[1] // 1000000, "M"),
),
rx.slider(
default_value=[0, 25000000],
min=0,
max=25000000,
on_value_commit=State.set_salary,
),
align_items="left",
width="100%",
),
),
spacing="4",
),
width="100%",
)

app = rx.App(state=rx.State)
app.add_page(index)
app.add_page(selection)


@pytest.fixture(scope="session")
def blank_template(
tmp_path_factory,
Expand All @@ -57,31 +160,60 @@ def blank_template(
running AppHarness instance
"""
root = tmp_path_factory.mktemp(f"blank_template")
with AppHarness.create(
root=root,
app_source=BlankTemplate, # type: ignore
) as harness:
yield harness

yield AppHarness.create(root=root, app_source=BlankTemplate) # type: ignore


@pytest.fixture(scope="session")
def blank_template_two_pages(
tmp_path_factory,
) -> Generator[AppHarness, None, None]:
"""Start Blank Template app at tmp_path via AppHarness.
Args:
tmp_path_factory: pytest tmp_path_factory fixture
Yields:
running AppHarness instance
"""
root = tmp_path_factory.mktemp(f"blank_template_two_pages")

yield AppHarness.create(root=root, app_source=BlankTemplate2) # type: ignore


@pytest.mark.benchmark(
group="blank template",
min_time=0.1,
max_time=0.5,
min_rounds=10,
timer=time.perf_counter,
disable_gc=True,
warmup=False
group="blank template", timer=time.perf_counter, disable_gc=True, warmup=False
)
def test_blank_template_app_start(benchmark, blank_template, mocker):
import copy
assert blank_template.app_instance is not None
from reflex.constants.base import Dirs
dirs = copy.deepcopy(Dirs)
def test_blank_template_compile_time(benchmark, blank_template):
def setup():
with chdir(blank_template.app_path):
blank_template._initialize_app()
build.setup_frontend(blank_template.app_path)

def benchmark_fn():
with chdir(blank_template.app_path):
blank_template.app_instance.compile_()

dirs.WEB = str( blank_template.app_path /".web")
dirs.UTILS = "/".join([str( blank_template.app_path),"utils"])
benchmark.pedantic(benchmark_fn, setup=setup, rounds=10)

mocker.patch("reflex.utils.prerequisites.constants.Dirs", dirs)
benchmark(blank_template.app_instance.compile_)

@pytest.mark.benchmark(
group="blank template", timer=time.perf_counter, disable_gc=True, warmup=False
)
def test_blank_template_two_pages_compile_time(benchmark, blank_template_two_pages):
def setup():
with chdir(blank_template_two_pages.app_path):
blank_template_two_pages._initialize_app()
build.setup_frontend(blank_template_two_pages.app_path)

def benchmark_fn():
with chdir(blank_template_two_pages.app_path):
blank_template_two_pages.app_instance.compile_()
path_ops.rm(
os.path.join(
constants.Dirs.WEB, "reflex.install_frontend_packages.cached"
)
)
path_ops.rm(os.path.join(constants.Dirs.WEB, "node_modules"))

benchmark.pedantic(benchmark_fn, setup=setup, rounds=10)

0 comments on commit 3eb36f2

Please sign in to comment.