| 1 |
#!/usr/bin/python |
| 2 |
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 3 |
# ex: set sts=4 ts=4 sw=4 et: |
| 4 |
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 5 |
# |
| 6 |
# See COPYING file distributed along with the NiBabel package for the |
| 7 |
# copyright and license terms. |
| 8 |
# |
| 9 |
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 10 |
"""""" |
| 11 |
|
| 12 |
__docformat__ = 'restructuredtext' |
| 13 |
|
| 14 |
import os |
| 15 |
import sys |
| 16 |
from os import path |
| 17 |
|
| 18 |
if __name__ == '__main__': |
| 19 |
|
| 20 |
usage = ( |
| 21 |
"""Usage: %s [options] <python module> ... |
| 22 |
""" |
| 23 |
% sys.argv[0] |
| 24 |
) |
| 25 |
|
| 26 |
# default options |
| 27 |
convert2kcache = True |
| 28 |
displaykcachegrinder = True |
| 29 |
printstats = False |
| 30 |
pfilename = None |
| 31 |
pstatsfilename = None |
| 32 |
profilelines = True |
| 33 |
profilelevel = 10 # how many most hungry to list in stats |
| 34 |
run = True # either to run profiling at all |
| 35 |
|
| 36 |
removed = sys.argv.pop(0) |
| 37 |
|
| 38 |
if not len(sys.argv): |
| 39 |
print(usage) |
| 40 |
sys.exit(1) |
| 41 |
|
| 42 |
while sys.argv[0].startswith('-'): |
| 43 |
if sys.argv[0] in ['-l', '--level']: |
| 44 |
profilelevel = int(sys.argv[1]) |
| 45 |
sys.argv.pop(0) |
| 46 |
elif sys.argv[0] in ['-o', '--output-file']: |
| 47 |
pfilename = sys.argv[1] |
| 48 |
sys.argv.pop(0) |
| 49 |
elif sys.argv[0] in ['-O', '--output-statsfile']: |
| 50 |
pstatsfilename = sys.argv[1] |
| 51 |
sys.argv.pop(0) |
| 52 |
elif sys.argv[0] in ['-s', '--stats']: |
| 53 |
printstats = True |
| 54 |
convert2kcache = False |
| 55 |
displaykcachegrinder = False |
| 56 |
elif sys.argv[0] in ['-n', '--no-run']: |
| 57 |
run = False |
| 58 |
elif sys.argv[0] in ['-P', '--no-profilelines']: |
| 59 |
profilelines = False |
| 60 |
elif sys.argv[0] in ['-K', '--no-kcache']: |
| 61 |
convert2kcache = False |
| 62 |
displaykcachegrinder = False |
| 63 |
else: |
| 64 |
print(usage) |
| 65 |
sys.exit(1) |
| 66 |
sys.argv.pop(0) |
| 67 |
|
| 68 |
cmdname = sys.argv[0] |
| 69 |
dirname = path.dirname(cmdname) |
| 70 |
(root, ext) = path.splitext(path.basename(cmdname)) |
| 71 |
|
| 72 |
sys.path.append(dirname) |
| 73 |
|
| 74 |
# now do profiling |
| 75 |
try: |
| 76 |
import hotshot |
| 77 |
except ImportError: |
| 78 |
raise RuntimeError('No hotshot') |
| 79 |
|
| 80 |
if pfilename is None: |
| 81 |
pfilename = cmdname + '.prof' |
| 82 |
|
| 83 |
if run: |
| 84 |
exec(f'import {root} as runnable') |
| 85 |
|
| 86 |
if not 'main' in runnable.__dict__: |
| 87 |
print(f'OOPS: file/module {cmdname} has no function main defined') |
| 88 |
sys.exit(1) |
| 89 |
|
| 90 |
prof = hotshot.Profile(pfilename, lineevents=profilelines) |
| 91 |
|
| 92 |
try: |
| 93 |
# actually return values are never setup |
| 94 |
# since unittest.main sys.exit's |
| 95 |
results = prof.runcall(runnable.main) |
| 96 |
except SystemExit: |
| 97 |
pass |
| 98 |
|
| 99 |
print(f'Saving profile data into {pfilename}') |
| 100 |
prof.close() |
| 101 |
|
| 102 |
if printstats or pstatsfilename: |
| 103 |
import hotshot.stats |
| 104 |
|
| 105 |
print('Loading profile file to print statistics') |
| 106 |
stats = hotshot.stats.load(pfilename) |
| 107 |
if printstats: |
| 108 |
stats.strip_dirs() |
| 109 |
stats.sort_stats('time', 'calls') |
| 110 |
stats.print_stats(profilelevel) |
| 111 |
if pstatsfilename: |
| 112 |
stats.dump_stats(pstatsfilename) |
| 113 |
|
| 114 |
kfilename = pfilename + '.kcache' |
| 115 |
if convert2kcache: |
| 116 |
cmd = 'hotshot2calltree -o %s %s' % (kfilename, pfilename) |
| 117 |
if os.system(cmd): |
| 118 |
print('!!! Make sure to install kcachegrind-converters ;-)') |
| 119 |
sys.exit(1) |
| 120 |
|
| 121 |
if displaykcachegrinder: |
| 122 |
if os.system('kcachegrind %s' % kfilename): |
| 123 |
print('!!! Make sure to install kcachegrind ;-)') |
| 124 |
sys.exit(1) |
| 125 |
|
| 126 |
else: |
| 127 |
print('Go away -- nothing to look here for as a module') |