def _profile(continuation): prof_file = 'populateDir.prof' try: import cProfile import pstats print('Profiling using cProfile') cProfile.runctx('continuation()', globals(), locals(), prof_file) stats = pstats.Stats(prof_file) except ImportError: import hotshot import hotshot.stats prof = hotshot.Profile(prof_file, lineevents=1) print('Profiling using hotshot') prof.runcall(continuation) prof.close() stats = hotshot.stats.load(prof_file) stats.strip_dirs() #for a in ['calls', 'cumtime', 'cumulative', 'ncalls', 'time', 'tottime']: for a in ['cumtime', 'time', 'ncalls']: print("------------------------------------------------------------------------------------------------------------------------------") try: stats.sort_stats(a) stats.print_stats(150) stats.print_callees(150) stats.print_callers(150) except KeyError: pass os.remove(prof_file)
def show_profile(stats): # stats.strip_dirs() stats.sort_stats(options['order']) # now capture the output out = cStringIO.StringIO() old_stdout = sys.stdout sys.stdout = out # Figure out the correct part of stats to call try: if options['output'] == 'callers': print " Callers of '" + options['data'] + "':" stats.print_callers(options['data'], options['limit']) elif options['output'] == 'callees': print " Functions that '" + options['data'] + "' call:" stats.print_callees(options['data'], options['limit']) else: # show stats print "Statistics: " stats.print_stats(options['limit']) except: print "Couldn't generate output. Possibly bad caller/callee pattern" # reset to defaults sys.stdout = old_stdout out.seek(0) parse_state = None; # keep track of where the 2nd column of functions start # we'll find this out from the header col2starts = 0 result = ""; for line in out: # funclist1: the first line of the function list if parse_state == 'funclist': function = line[0:col2starts].strip() subfunc = line[col2starts:].strip() if function: result += "\n" + function + "\n" result += " " + subfunc + "\n" # default parse_state, look for Function header elif line.startswith('Function'): if options['output'] == 'callers': col2starts = line.find('was called by') elif options['output'] == 'callees': col2starts = line.find('called') parse_state = 'funclist' else: result += line + "\n" # now spit out to less output_with_pager(result)
def show_profile(stats): # stats.strip_dirs() stats.sort_stats(options['order']) # now capture the output out = cStringIO.StringIO() old_stdout = sys.stdout sys.stdout = out # Figure out the correct part of stats to call try: if options['output'] == 'callers': print " Callers of '" + options['data'] + "':" stats.print_callers(options['data'], options['limit']) elif options['output'] == 'callees': print " Functions that '" + options['data'] + "' call:" stats.print_callees(options['data'], options['limit']) else: # show stats print "Statistics: " stats.print_stats(options['limit']) except: print "Couldn't generate output. Possibly bad caller/callee pattern" # reset to defaults sys.stdout = old_stdout out.seek(0) parse_state = None # keep track of where the 2nd column of functions start # we'll find this out from the header col2starts = 0 result = "" for line in out: # funclist1: the first line of the function list if parse_state == 'funclist': function = line[0:col2starts].strip() subfunc = line[col2starts:].strip() if function: result += "\n" + function + "\n" result += " " + subfunc + "\n" # default parse_state, look for Function header elif line.startswith('Function'): if options['output'] == 'callers': col2starts = line.find('was called by') elif options['output'] == 'callees': col2starts = line.find('called') parse_state = 'funclist' else: result += line + "\n" # now spit out to less output_with_pager(result)
def print_profile_data(filename): import hotshot.stats stats = hotshot.stats.load(filename) #stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats() print '--Callers--' stats.print_callers() print '--Callees--' stats.print_callees()
def reportLastRun(self): import hotshot.stats stats = hotshot.stats.load(self._logfile) stats.strip_dirs() for sortKey in self._options.profile_sort: stats.sort_stats(sortKey) stats.print_stats(self._options.profile_regex, self._options.profile_limit) if self._options.profile_callers: stats.print_callers() if self._options.profile_callees: stats.print_callees()
def print_stats(): def func_std_string(func): path, line, name = func return '%s at File "%s", line %d' % (name, path, line) if __profiled__: import pstats pstats.func_std_string = func_std_string stats = hotshot.stats.load('profile.prof') stats.sort_stats('time', 'calls') stats.print_stats(20) for funcname in __profiled__: stats.print_callees(funcname)
def show(file, call='cum', length=50): if file.endswith('stats'): stats = pstats.Stats(file) else: stats = hotshot.stats.load(file) stats.strip_dirs() stats.dump_stats('.'.join((os.path.splitext(file)[0], 'stats'))) if call == 'cum': cum(stats, int(length)) elif call == 'tot': calls(stats, int(length)) else: stats.sort_stats(call) stats.print_stats(int(length)) stats.print_callers(int(length)) stats.print_callees(int(length)) return stats
def _profile(continuation): prof_file = 'iotop.prof' try: import cProfile import pstats print('Profiling using cProfile') cProfile.runctx('continuation()', globals(), locals(), prof_file) stats = pstats.Stats(prof_file) except ImportError: import hotshot import hotshot.stats prof = hotshot.Profile(prof_file, lineevents=1) print('Profiling using hotshot') prof.runcall(continuation) prof.close() stats = hotshot.stats.load(prof_file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(50) stats.print_callees(50) os.remove(prof_file)
def _profile(continuation): prof_file = "iotop.prof" try: import cProfile import pstats print("Profiling using cProfile") cProfile.runctx("continuation()", globals(), locals(), prof_file) stats = pstats.Stats(prof_file) except ImportError: import hotshot import hotshot.stats prof = hotshot.Profile(prof_file, lineevents=1) print("Profiling using hotshot") prof.runcall(continuation) prof.close() stats = hotshot.stats.load(prof_file) stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(50) stats.print_callees(50) os.remove(prof_file)