mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 09:53:32 +01:00
Cmds: Support --write-profile <filename>
Add an option which makes the program write profiling statistics in pstats format to a specified file. Visualize e.g. with: gprof2dot -f pstats <filename> -o <dotfile> dot -Tpng | display <dotfile> Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
d2ec56336e
commit
958c077da3
1 changed files with 24 additions and 10 deletions
|
|
@ -9,6 +9,8 @@ import asyncio
|
|||
from argparse import ArgumentParser
|
||||
from typing import Optional
|
||||
|
||||
import cProfile
|
||||
|
||||
import jwutils
|
||||
from jwutils.log import *
|
||||
|
||||
|
|
@ -68,6 +70,7 @@ class Cmds: # export
|
|||
self.__parser.add_argument('--log-flags', help='Log flags', default=log_flags)
|
||||
self.__parser.add_argument('--log-level', help='Log level', default=log_level)
|
||||
self.__parser.add_argument('--backtrace', help='Show exception backtraces', action='store_true', default=False)
|
||||
self.__parser.add_argument('--write-profile', help='Profile code and store output to file', default=None)
|
||||
if self.__modules == None:
|
||||
self.__modules = [ '__main__' ]
|
||||
subcmds = set()
|
||||
|
|
@ -99,6 +102,7 @@ class Cmds: # export
|
|||
set_flags(self.args.log_flags)
|
||||
set_level(self.args.log_level)
|
||||
self.__back_trace = self.args.backtrace
|
||||
exit_status = 0
|
||||
|
||||
# This is the toplevel parser, i.e. no func member has been added to the args via
|
||||
#
|
||||
|
|
@ -112,17 +116,27 @@ class Cmds: # export
|
|||
self.__parser.print_help()
|
||||
return None
|
||||
|
||||
if self.__back_trace:
|
||||
pr = None if self.args.write_profile is None else cProfile.Profile()
|
||||
if pr is not None:
|
||||
pr.enable()
|
||||
|
||||
try:
|
||||
ret = await self.args.func(self.args)
|
||||
else:
|
||||
try:
|
||||
ret = await self.args.func(self.args)
|
||||
except Exception as e:
|
||||
if hasattr(e, 'message'):
|
||||
slog(ERR, e.message)
|
||||
else:
|
||||
slog(ERR, f'Exception: {type(e)}: {e}')
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
if hasattr(e, 'message'):
|
||||
slog(ERR, e.message)
|
||||
else:
|
||||
slog(ERR, f'Exception: {type(e)}: {e}')
|
||||
exit_status = 1
|
||||
if self.__back_trace:
|
||||
raise
|
||||
finally:
|
||||
if pr is not None:
|
||||
pr.disable()
|
||||
slog(NOTICE, f'Writing profile statistics to {self.args.write_profile}')
|
||||
pr.dump_stats(self.args.write_profile)
|
||||
|
||||
sys.exit(exit_status)
|
||||
|
||||
def __del__(self):
|
||||
if self.__own_eloop:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue