ui: add startup profiling (#36482)

* ui: add startup profiling

* lil more
pull/36488/head
Adeeb Shihadeh 2 weeks ago committed by GitHub
parent e92e59ca78
commit 94ca077e69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 32
      system/ui/lib/application.py

@ -6,6 +6,7 @@ import signal
import sys import sys
import pyray as rl import pyray as rl
import threading import threading
from contextlib import contextmanager
from collections.abc import Callable from collections.abc import Callable
from collections import deque from collections import deque
from dataclasses import dataclass from dataclasses import dataclass
@ -170,6 +171,7 @@ class GuiApplication:
self._window_close_requested = True self._window_close_requested = True
def init_window(self, title: str, fps: int = _DEFAULT_FPS): def init_window(self, title: str, fps: int = _DEFAULT_FPS):
with self._startup_profile_context():
def _close(sig, frame): def _close(sig, frame):
self.close() self.close()
sys.exit(0) sys.exit(0)
@ -202,6 +204,36 @@ class GuiApplication:
if not PC: if not PC:
self._mouse.start() self._mouse.start()
@contextmanager
def _startup_profile_context(self):
if "PROFILE_STARTUP" not in os.environ:
yield
return
import cProfile
import io
import pstats
profiler = cProfile.Profile()
start_time = time.monotonic()
profiler.enable()
# do the init
yield
profiler.disable()
elapsed_ms = (time.monotonic() - start_time) * 1e3
stats_stream = io.StringIO()
pstats.Stats(profiler, stream=stats_stream).sort_stats("cumtime").print_stats(25)
print("\n=== Startup profile ===")
print(stats_stream.getvalue().rstrip())
green = "\033[92m"
reset = "\033[0m"
print(f"{green}UI window ready in {elapsed_ms:.1f} ms{reset}")
sys.exit(0)
def set_modal_overlay(self, overlay, callback: Callable | None = None): def set_modal_overlay(self, overlay, callback: Callable | None = None):
if self._modal_overlay.overlay is not None: if self._modal_overlay.overlay is not None:
if self._modal_overlay.callback is not None: if self._modal_overlay.callback is not None:

Loading…
Cancel
Save