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:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,3 +7,4 @@ dist/
|
||||
build/
|
||||
.venv/
|
||||
config/s5p.yaml
|
||||
*.prof
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user