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: