1
0

test_compile_benchmark.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Benchmark the time it takes to compile a reflex app."""
  2. import importlib
  3. import reflex
  4. rx = reflex
  5. class State(rx.State):
  6. """A simple state class with a count variable."""
  7. count: int = 0
  8. def increment(self):
  9. """Increment the count."""
  10. self.count += 1
  11. def decrement(self):
  12. """Decrement the count."""
  13. self.count -= 1
  14. class SliderVariation(State):
  15. """A simple state class with a count variable."""
  16. value: int = 50
  17. def set_end(self, value: int):
  18. """Increment the count.
  19. Args:
  20. value: The value of the slider.
  21. """
  22. self.value = value
  23. def sample_small_page() -> rx.Component:
  24. """A simple page with a button that increments the count.
  25. Returns:
  26. A reflex component.
  27. """
  28. return rx.vstack(
  29. *[rx.button(State.count, font_size="2em") for i in range(100)],
  30. gap="1em",
  31. )
  32. def sample_large_page() -> rx.Component:
  33. """A large page with a slider that increments the count.
  34. Returns:
  35. A reflex component.
  36. """
  37. return rx.vstack(
  38. *[
  39. rx.vstack(
  40. rx.heading(SliderVariation.value),
  41. rx.slider(on_change_end=SliderVariation.set_end),
  42. width="100%",
  43. )
  44. for i in range(100)
  45. ],
  46. gap="1em",
  47. )
  48. def add_small_pages(app: rx.App):
  49. """Add 10 small pages to the app.
  50. Args:
  51. app: The reflex app to add the pages to.
  52. """
  53. for i in range(10):
  54. app.add_page(sample_small_page, route=f"/{i}")
  55. def add_large_pages(app: rx.App):
  56. """Add 10 large pages to the app.
  57. Args:
  58. app: The reflex app to add the pages to.
  59. """
  60. for i in range(10):
  61. app.add_page(sample_large_page, route=f"/{i}")
  62. def test_mean_import_time(benchmark):
  63. """Test that the mean import time is less than 1 second.
  64. Args:
  65. benchmark: The benchmark fixture.
  66. """
  67. def import_reflex():
  68. importlib.reload(reflex)
  69. # Benchmark the import
  70. benchmark(import_reflex)
  71. def test_mean_add_small_page_time(benchmark):
  72. """Test that the mean add page time is less than 1 second.
  73. Args:
  74. benchmark: The benchmark fixture.
  75. """
  76. app = rx.App(state=State)
  77. benchmark(add_small_pages, app)
  78. def test_mean_add_large_page_time(benchmark):
  79. """Test that the mean add page time is less than 1 second.
  80. Args:
  81. benchmark: The benchmark fixture.
  82. """
  83. app = rx.App(state=State)
  84. results = benchmark(add_large_pages, app)
  85. print(results)