Added --profile CLI flag for running the Python profiler

This commit is contained in:
Sybren A. Stüvel 2018-03-07 15:50:04 +01:00
parent e2123f8090
commit c5a02423cb
2 changed files with 16 additions and 2 deletions

View File

@ -8,6 +8,7 @@ from . import common, pack, list_deps
def cli_main(): def cli_main():
parser = argparse.ArgumentParser(description='BAT: Blender Asset Tracer') parser = argparse.ArgumentParser(description='BAT: Blender Asset Tracer')
common.add_flag(parser, 'profile', help='Run the profiler, write to bam.prof')
# func is set by subparsers to indicate which function to run. # func is set by subparsers to indicate which function to run.
parser.set_defaults(func=None, parser.set_defaults(func=None,
@ -36,6 +37,18 @@ def cli_main():
if not args.func: if not args.func:
parser.error('No subcommand was given') parser.error('No subcommand was given')
if args.profile:
import cProfile
prof_fname = 'bam.prof'
cProfile.runctx('args.func(args)',
globals=globals(),
locals=locals(),
filename=prof_fname)
print('Profiler exported data to', prof_fname)
print('Run "pyprof2calltree -i %r -k" to convert and open in KCacheGrind' % prof_fname)
else:
return args.func(args) return args.func(args)

View File

@ -2,7 +2,8 @@
-e . -e .
pytest==3.4.1 pytest==3.4.1
pytest-cov==2.5.1 pytest-cov==2.5.1
radon==2.2.0 radon==2.2.0 # for the 'radon cc' command
pyprof2calltree==1.4.3 # for converting profiler output to KCacheGrind input
# Secondary requirements # Secondary requirements
attrs==17.4.0 attrs==17.4.0