From 57d2d8742446fdb9a79e39fd1559041b0f4581c5 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 15 Feb 2026 03:39:36 +0100 Subject: [PATCH] feat: add --cprofile flag for performance profiling Dumps cProfile stats to a file (default: s5p.prof) on exit. View with: python -m pstats s5p.prof --- .gitignore | 1 + src/s5p/cli.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 783156b..7603d7d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ dist/ build/ .venv/ config/s5p.yaml +*.prof diff --git a/src/s5p/cli.py b/src/s5p/cli.py index a6d30eb..a4fde99 100644 --- a/src/s5p/cli.py +++ b/src/s5p/cli.py @@ -42,6 +42,10 @@ def _parse_args(argv: list[str] | None = None) -> argparse.Namespace: ) p.add_argument("-v", "--verbose", action="store_true", help="debug logging") p.add_argument("-q", "--quiet", action="store_true", help="errors only") + p.add_argument( + "--cprofile", metavar="FILE", nargs="?", const="s5p.prof", + help="enable cProfile, dump stats to FILE (default: s5p.prof)", + ) return p.parse_args(argv) @@ -73,7 +77,18 @@ def main(argv: list[str] | None = None) -> int: _setup_logging(config.log_level) try: - asyncio.run(serve(config)) + if args.cprofile: + import cProfile + prof = cProfile.Profile() + prof.enable() + try: + asyncio.run(serve(config)) + finally: + prof.disable() + prof.dump_stats(args.cprofile) + logging.getLogger("s5p").info("profile saved to %s", args.cprofile) + else: + asyncio.run(serve(config)) except KeyboardInterrupt: return 0 except Exception as e: