|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
build profile graph for the given instance |
|
|
|
running: |
|
$ get_gprof <args> <instance> |
|
|
|
executes: |
|
gprof2dot -f pstats <args> <type>.prof | dot -Tpng -o <type>.call.png |
|
|
|
where: |
|
<args> are arguments for gprof2dot, such as "-n 5 -e 5" |
|
<instance> is code to create the instance to profile |
|
<type> is the class of the instance (i.e. type(instance)) |
|
|
|
For example: |
|
$ get_gprof -n 5 -e 1 "import numpy; numpy.array([1,2])" |
|
|
|
will create 'ndarray.call.png' with the profile graph for numpy.array([1,2]), |
|
where '-n 5' eliminates nodes below 5% threshold, similarly '-e 1' eliminates |
|
edges below 1% threshold |
|
''' |
|
|
|
if __name__ == "__main__": |
|
import sys |
|
if len(sys.argv) < 2: |
|
print ("Please provide an object instance (e.g. 'import math; math.pi')") |
|
sys.exit() |
|
|
|
args = sys.argv[1:-1] |
|
args = ' '.join(args) |
|
|
|
obj = sys.argv[-1] |
|
obj = obj.split(';') |
|
|
|
for line in obj[:-1]: |
|
exec(line) |
|
|
|
try: |
|
obj = eval(obj[-1]) |
|
except Exception: |
|
print ("Error processing object instance") |
|
sys.exit() |
|
|
|
|
|
objtype = type(obj) |
|
name = getattr(objtype, '__name__', getattr(objtype, '__class__', objtype)) |
|
|
|
|
|
import dill |
|
import os |
|
import cProfile |
|
|
|
cProfile.run("dill.dumps(obj)", filename="%s.prof" % name) |
|
msg = "gprof2dot -f pstats %s %s.prof | dot -Tpng -o %s.call.png" % (args, name, name) |
|
try: |
|
res = os.system(msg) |
|
except Exception: |
|
print ("Please verify install of 'gprof2dot' to view profile graphs") |
|
if res: |
|
print ("Please verify install of 'gprof2dot' to view profile graphs") |
|
|
|
|
|
f_prof = "%s.prof" % name |
|
import pstats |
|
stats = pstats.Stats(f_prof, stream=sys.stdout) |
|
stats.strip_dirs().sort_stats('cumtime') |
|
stats.print_stats(20) |
|
os.remove(f_prof) |
|
|