From dc2f6d5fc434194ea84ea146febc28d2cccfa207 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 5 Nov 2025 15:56:20 -0600 Subject: [PATCH] Add profiling for simulation ticks and include snakeviz as a development dependency --- core/simulation_engine.py | 16 ++++++++++++++-- pyproject.toml | 1 + uv.lock | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core/simulation_engine.py b/core/simulation_engine.py index 06e0034..1326120 100644 --- a/core/simulation_engine.py +++ b/core/simulation_engine.py @@ -14,6 +14,9 @@ from core.input_handler import InputHandler from core.renderer import Renderer from ui.hud import HUD +import cProfile +import pstats + class SimulationEngine: def __init__(self): @@ -23,6 +26,13 @@ class SimulationEngine: self._init_simulation() self.running = True + def _profile_single_tick(self): + profiler = cProfile.Profile() + profiler.enable() + self.world.tick_all() + profiler.disable() + profiler.dump_stats('profile_tick.prof') # Save to file + def _init_window(self): info = pygame.display.Info() self.window_width = int(info.current_w // 1.5) @@ -51,6 +61,8 @@ class SimulationEngine: self.input_handler = InputHandler(self.camera, self.world, self.sim_view_rect) self.renderer = Renderer(self.sim_view) + self._profile_single_tick() # Profile a single tick for performance analysis + def _update_simulation_view(self): viewport_rect = self.hud.get_viewport_rect() self.sim_view_width = viewport_rect.width @@ -85,7 +97,7 @@ class SimulationEngine: y = random.randint(-half_height // 2, half_height // 2) world.add_object(FoodObject(Position(x=x, y=y))) - for _ in range(350): + for _ in range(50): new_cell = DefaultCell( Position(x=random.randint(-half_width // 2, half_width // 2), y=random.randint(-half_height // 2, half_height // 2)), Rotation(angle=0) @@ -220,7 +232,7 @@ class SimulationEngine: self.hud.render_fps(self.screen, self.clock) self.hud.render_tps(self.screen, self.actual_tps) # self.hud.render_tick_count(self.screen, self.total_ticks) - # self.hud.render_selected_objects_info(self.screen, self.input_handler.selected_objects) + self.hud.render_selected_objects_info(self.screen, self.input_handler.selected_objects) self.hud.render_legend(self.screen, self.input_handler.show_legend) self.hud.render_pause_indicator(self.screen, self.input_handler.is_paused) if self.input_handler.selected_objects: diff --git a/pyproject.toml b/pyproject.toml index 285da37..359e9c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,4 +16,5 @@ dependencies = [ dev = [ "psutil>=7.0.0", "ruff>=0.11.12", + "snakeviz>=2.2.2", ] diff --git a/uv.lock b/uv.lock index 5925d6a..72c17c6 100644 --- a/uv.lock +++ b/uv.lock @@ -55,6 +55,7 @@ dependencies = [ dev = [ { name = "psutil" }, { name = "ruff" }, + { name = "snakeviz" }, ] [package.metadata] @@ -71,6 +72,7 @@ requires-dist = [ dev = [ { name = "psutil", specifier = ">=7.0.0" }, { name = "ruff", specifier = ">=0.11.12" }, + { name = "snakeviz", specifier = ">=2.2.2" }, ] [[package]] @@ -459,6 +461,37 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/44/42/d58086ec20f52d2b0140752ae54b355ea2be2ed46f914231136dd1effcc7/ruff-0.11.12-py3-none-win_arm64.whl", hash = "sha256:65194e37853158d368e333ba282217941029a28ea90913c67e558c611d04daa5", size = 10697770, upload-time = "2025-05-29T13:31:38.009Z" }, ] +[[package]] +name = "snakeviz" +version = "2.2.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "tornado" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/04/06/82f56563b16d33c2586ac2615a3034a83a4ff1969b84c8d79339e5d07d73/snakeviz-2.2.2.tar.gz", hash = "sha256:08028c6f8e34a032ff14757a38424770abb8662fb2818985aeea0d9bc13a7d83", size = 182039, upload-time = "2024-11-09T22:03:58.99Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cd/f7/83b00cdf4f114f10750a18b64c27dc34636d0ac990ccac98282f5c0fbb43/snakeviz-2.2.2-py3-none-any.whl", hash = "sha256:77e7b9c82f6152edc330040319b97612351cd9b48c706434c535c2df31d10ac5", size = 183477, upload-time = "2024-11-09T22:03:57.049Z" }, +] + +[[package]] +name = "tornado" +version = "6.5.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/51/89/c72771c81d25d53fe33e3dca61c233b665b2780f21820ba6fd2c6793c12b/tornado-6.5.1.tar.gz", hash = "sha256:84ceece391e8eb9b2b95578db65e920d2a61070260594819589609ba9bc6308c", size = 509934, upload-time = "2025-05-22T18:15:38.788Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/77/89/f4532dee6843c9e0ebc4e28d4be04c67f54f60813e4bf73d595fe7567452/tornado-6.5.1-cp39-abi3-macosx_10_9_universal2.whl", hash = "sha256:d50065ba7fd11d3bd41bcad0825227cc9a95154bad83239357094c36708001f7", size = 441948, upload-time = "2025-05-22T18:15:20.862Z" }, + { url = "https://files.pythonhosted.org/packages/15/9a/557406b62cffa395d18772e0cdcf03bed2fff03b374677348eef9f6a3792/tornado-6.5.1-cp39-abi3-macosx_10_9_x86_64.whl", hash = "sha256:9e9ca370f717997cb85606d074b0e5b247282cf5e2e1611568b8821afe0342d6", size = 440112, upload-time = "2025-05-22T18:15:22.591Z" }, + { url = "https://files.pythonhosted.org/packages/55/82/7721b7319013a3cf881f4dffa4f60ceff07b31b394e459984e7a36dc99ec/tornado-6.5.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b77e9dfa7ed69754a54c89d82ef746398be82f749df69c4d3abe75c4d1ff4888", size = 443672, upload-time = "2025-05-22T18:15:24.027Z" }, + { url = "https://files.pythonhosted.org/packages/7d/42/d11c4376e7d101171b94e03cef0cbce43e823ed6567ceda571f54cf6e3ce/tornado-6.5.1-cp39-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:253b76040ee3bab8bcf7ba9feb136436a3787208717a1fb9f2c16b744fba7331", size = 443019, upload-time = "2025-05-22T18:15:25.735Z" }, + { url = "https://files.pythonhosted.org/packages/7d/f7/0c48ba992d875521ac761e6e04b0a1750f8150ae42ea26df1852d6a98942/tornado-6.5.1-cp39-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:308473f4cc5a76227157cdf904de33ac268af770b2c5f05ca6c1161d82fdd95e", size = 443252, upload-time = "2025-05-22T18:15:27.499Z" }, + { url = "https://files.pythonhosted.org/packages/89/46/d8d7413d11987e316df4ad42e16023cd62666a3c0dfa1518ffa30b8df06c/tornado-6.5.1-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:caec6314ce8a81cf69bd89909f4b633b9f523834dc1a352021775d45e51d9401", size = 443930, upload-time = "2025-05-22T18:15:29.299Z" }, + { url = "https://files.pythonhosted.org/packages/78/b2/f8049221c96a06df89bed68260e8ca94beca5ea532ffc63b1175ad31f9cc/tornado-6.5.1-cp39-abi3-musllinux_1_2_i686.whl", hash = "sha256:13ce6e3396c24e2808774741331638ee6c2f50b114b97a55c5b442df65fd9692", size = 443351, upload-time = "2025-05-22T18:15:31.038Z" }, + { url = "https://files.pythonhosted.org/packages/76/ff/6a0079e65b326cc222a54720a748e04a4db246870c4da54ece4577bfa702/tornado-6.5.1-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:5cae6145f4cdf5ab24744526cc0f55a17d76f02c98f4cff9daa08ae9a217448a", size = 443328, upload-time = "2025-05-22T18:15:32.426Z" }, + { url = "https://files.pythonhosted.org/packages/49/18/e3f902a1d21f14035b5bc6246a8c0f51e0eef562ace3a2cea403c1fb7021/tornado-6.5.1-cp39-abi3-win32.whl", hash = "sha256:e0a36e1bc684dca10b1aa75a31df8bdfed656831489bc1e6a6ebed05dc1ec365", size = 444396, upload-time = "2025-05-22T18:15:34.205Z" }, + { url = "https://files.pythonhosted.org/packages/7b/09/6526e32bf1049ee7de3bebba81572673b19a2a8541f795d887e92af1a8bc/tornado-6.5.1-cp39-abi3-win_amd64.whl", hash = "sha256:908e7d64567cecd4c2b458075589a775063453aeb1d2a1853eedb806922f568b", size = 444840, upload-time = "2025-05-22T18:15:36.1Z" }, + { url = "https://files.pythonhosted.org/packages/55/a7/535c44c7bea4578e48281d83c615219f3ab19e6abc67625ef637c73987be/tornado-6.5.1-cp39-abi3-win_arm64.whl", hash = "sha256:02420a0eb7bf617257b9935e2b754d1b63897525d8a289c9d65690d580b4dcf7", size = 443596, upload-time = "2025-05-22T18:15:37.433Z" }, +] + [[package]] name = "typing-extensions" version = "4.14.0"