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
This commit is contained in:
user
2026-02-15 03:39:36 +01:00
parent 57c78f5563
commit 57d2d87424
2 changed files with 17 additions and 1 deletions

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@ dist/
build/
.venv/
config/s5p.yaml
*.prof

View File

@@ -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: