def __call__(self, *args): ##, **kw): kw unused import hotshot, hotshot.stats, os, tempfile ##, time already imported f, filename = tempfile.mkstemp() os.close(f) prof = hotshot.Profile(filename) stime = time.time() result = prof.runcall(self.func, *args) stime = time.time() - stime prof.close() import cStringIO out = cStringIO.StringIO() stats = hotshot.stats.load(filename) stats.stream = out stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40) stats.print_callers() x = '\n\ntook '+ str(stime) + ' seconds\n' x += out.getvalue() # remove the tempfile try: os.remove(filename) except IOError: pass return result, x
def run_profile(self): using_hotshot = False if using_hotshot: import hotshot import hotshot.stats else: import profile import pstats profile_name = 'NormalFormTest.prof' proportion_worst_results = 0.125 create_empty_file(profile_name) if using_hotshot: profiler = hotshot.Profile(profile_name) benchtime = profiler.runcall(run_nf) #main call profiler.close() stats = hotshot.stats.load(profile_name) else: profile.run('run_nf()', profile_name) #main call stats = pstats.Stats(profile_name) #output profile stats.strip_dirs() stats.sort_stats('time', 'cum', 'calls') print '[1] Statistics:' stats.print_stats(proportion_worst_results) print '[2] Callers for the above:' stats.print_callers(proportion_worst_results)
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 __call__(self, *args): ##, **kw): kw unused import hotshot, hotshot.stats, tempfile ##, time already imported temp = tempfile.NamedTemporaryFile() prof = hotshot.Profile(temp.name) stime = time.time() result = prof.runcall(self.func, *args) stime = time.time() - stime prof.close() import cStringIO out = cStringIO.StringIO() oldout = sys.stdout stats = hotshot.stats.load(temp.name) if sys.version_info >= (2, 5): stats.stream = out else: sys.stdout = out stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40) stats.print_callers() if sys.version_info < (2, 5): sys.stdout = oldout x = '\n\ntook '+ str(stime) + ' seconds\n' x += out.getvalue() return result, x
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 __call__(self, *args): ##, **kw): kw unused import hotshot, hotshot.stats, os, tempfile ##, time already imported f, filename = tempfile.mkstemp() os.close(f) prof = hotshot.Profile(filename) stime = time.time() result = prof.runcall(self.func, *args) stime = time.time() - stime prof.close() import cStringIO out = cStringIO.StringIO() stats = hotshot.stats.load(filename) stats.stream = out stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40) stats.print_callers() def xx(): yield '\n\ntook '+ str(stime) + ' seconds\n' yield out.getvalue() # remove the tempfile try: os.remove(filename) except IOError: pass if result and not(hasattr(result, 'next') or hasattr(result, '__iter__')): result = [result] return itertools.chain(result, xx())
def format_stats(stats, strip_dirs=False, sort='time'): if strip_dirs: stats.strip_dirs() stats.sort_stats(sort, 'calls') so = StringIO() stats.stream = so stats.print_stats(PROFILE_LIMIT) stats.print_callers(PROFILE_LIMIT) return so.getvalue()
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 profile(filename, limit=DEFAULT_LIMIT): print "loading profile stats for %s" % filename stats = hotshot.stats.load(filename) # normal stats stats.sort_stats('cumulative', 'calls').print_stats(limit) # stats.strip_dirs() # this fails! bug in python http://bugs.python.org/issue7372 # callers stats.print_callers(limit)
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 profile(filename, limit=DEFAULT_LIMIT): outfile = filename + ".txt" with open(outfile, 'w') as f: print "loading profile stats for %s" % filename if filename.endswith('.agg.prof'): stats = pstats.Stats(filename, stream=f) else: stats = hotshot.stats.load(filename) stats.stream = f # normal stats stats.sort_stats('cumulative', 'calls').print_stats(limit) stats.print_callers(limit)
def profile(func, *args, **kwargs): import hotshot.stats prf = hotshot.Profile('mystats.profile') print("Start") r = prf.runcall(func, *args, **kwargs) print("Finished") prf.close() print("Loading profile") stats = hotshot.stats.load('mystats.profile') print("done") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.print_callers(100) return r
def format_stats(stats, strip_dirs=None, sort='time'): if strip_dirs: stats.strip_dirs() stats.sort_stats(sort, 'calls') so = StringIO() stats.stream = so stats.print_stats(PROFILE_LIMIT) stats.print_callers(PROFILE_LIMIT) r = so.getvalue() if not strip_dirs: rootdir = dirname(dirname(abspath(__file__))).lower() + os.sep r = r.replace(rootdir, '') for i in sys.path: if '/site-packages/' in i: r = r.replace(i.rsplit('/', 1)[0], '') return r
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 __logprofile(name, callers, sort, result): oldstdout = sys.stdout sys.stdout = cStringIO.StringIO() file = profiledir + name stats = hotshot.stats.load(file) stats.sort_stats(*sort) if callers: stats.print_callers(*result) else: stats.print_stats(*result) sys.stdout.seek(0) stats_txt = sys.stdout.read() sys.stdout.close() sys.stdout = oldstdout return stats_txt
def format_stats(stats, strip_dirs=None, sort='time'): if strip_dirs: stats.strip_dirs() stats.sort_stats(sort, 'calls') so = StringIO() stats.stream = so stats.print_stats(PROFILE_LIMIT) stats.print_callers(PROFILE_LIMIT) r = so.getvalue() if not strip_dirs: rootdir = dirname( dirname(abspath(__file__)) ).lower()+os.sep r = r.replace(rootdir, '') for i in sys.path: if '/site-packages/' in i: r = r.replace(i.rsplit('/', 1)[0], '') return r
def run_profile(options,progmain): if options.profiler == 'python': try: import cProfile as profile except ImportError: import profile import pstats prof = profile.Profile() try: return prof.runcall(progmain) finally: stats = pstats.Stats(prof,stream=sys.stderr) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(25) stats.print_callers(25) elif options.profiler == 'hotshot': import hotshot, hotshot.stats if sys.argv: statfile = '%s.prof' % sys.argv[0] else: statfile = 'tmp.prof' prof = hotshot.Profile(statfile) try: return prof.runcall(progmain) finally: prof.close() stats = hotshot.stats.load(statfile) stats.stream = sys.stderr stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(25) stats.print_callers(25) else: raise GLUError('ERROR: Unknown profiling option provided "%s"' % options.profiler)
def __call__(self, *args): ##, **kw): kw unused import hotshot, hotshot.stats, tempfile ##, time already imported temp = tempfile.NamedTemporaryFile() prof = hotshot.Profile(temp.name) stime = time.time() result = prof.runcall(self.func, *args) stime = time.time() - stime prof.close() import cStringIO out = cStringIO.StringIO() stats = hotshot.stats.load(temp.name) stats.stream = out stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40) stats.print_callers() x = '\n\ntook '+ str(stime) + ' seconds\n' x += out.getvalue() return result, x
def print_stats(): stats = hotshot.stats.load(temp.name) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40) stats.print_callers()
import hotshot.stats print "loading stats..." stats =hotshot.stats.load("pydir.prof") print "...done" stats.strip_dirs() stats.sort_stats('time') import sys stdout = sys.stdout sys.stdout = open("profile.dump", "w") stats.print_stats() stats.print_callers() sys.stdout = stdout
nf.set_tolerance(tolerance) nf.perform_all_computations(degree) def create_empty_file(name): file = open(name, 'w') file.close() if __name__ == '__main__': profile_name = 'NormalFormTest.prof' proportion_worst_results = 0.125 create_empty_file(profile_name) if using_hotshot: profiler = hotshot.Profile(profile_name) benchtime = profiler.runcall(main) #main call profiler.close() stats = hotshot.stats.load(profile_name) else: profile.run('main()', profile_name) #main call stats = pstats.Stats(profile_name) stats.strip_dirs() stats.sort_stats('time', 'cum', 'calls') print '[1] Statistics:' stats.print_stats(proportion_worst_results) print '[2] Callers for the above:' stats.print_callers(proportion_worst_results)
def print_stats(): stats = hotshot.stats.load(temp.name) stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(40) stats.print_callers()