def interactive_mode(index, prof=0): while 1: line = raw_input("> ") try: if prof: prof = hotshot.Profile('tx.prof') resultset = prof.runctx('index._apply_index( {\'text\':{\'query\':line}})[0] ', globals(), locals()) prof.close() stats = hotshot.stats.load('tx.prof') stats.strip_dirs() stats.sort_stats('cumulative') stats.print_stats(100) else: resultset = index._apply_index( {'text':{'query':line}})[0] print "Result: %d matches" % len(resultset) lst = list(resultset.items()) lst.sort(lambda x,y : -cmp(x[1],y[1])) for docid,score in lst: print "%-2d %s %d" % (docid, docs[docid].getId(), score) except: traceback.print_exc()
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 run_or_profile(suite): runner = unittest.TextTestRunner(verbosity=2) args = sys.argv[1:] if '-P' in args or '-PP' in args: try: import psyco if '-PP' in sys.argv[1:]: psyco.profile() else: psyco.full() print "Using Psyco." except: pass if '-p' in args: import os, hotshot, hotshot.stats LOG_FILE="profile.log" profiler = hotshot.Profile(LOG_FILE) profiler.runcall(runner.run, suite) profiler.close() stats = hotshot.stats.load(LOG_FILE) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(60) try: os.unlink(LOG_FILE) except: pass else: runner.run(suite)
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 run(): profilelevel = None if 'PROFILELEVEL' in environ: profilelevel = int(environ['PROFILELEVEL']) if profilelevel is None: TestProgramPyMVPA() else: profilelines = 'PROFILELINES' in environ import hotshot, hotshot.stats pname = "%s.prof" % sys.argv[0] prof = hotshot.Profile(pname, lineevents=profilelines) try: # actually return values are never setup # since unittest.main sys.exit's benchtime, stones = prof.runcall( unittest.main ) except SystemExit: pass print "Saving profile data into %s" % pname prof.close() if profilelevel > 0: # we wanted to see the summary right here # instead of just storing it into a file print "Loading profile data from %s" % pname stats = hotshot.stats.load(pname) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(profilelevel)
def atexit(self): """Stop profiling and print profile information to sys.stderr. This function is registered as an atexit hook. """ self.profiler.close() funcname = self.fn.__name__ filename = self.fn.func_code.co_filename lineno = self.fn.func_code.co_firstlineno print print "*** PROFILER RESULTS ***" print "%s (%s:%s)" % (funcname, filename, lineno) print "function called %d times" % self.ncalls, if self.skipped: print "(%d calls not profiled)" % self.skipped else: print print stats = hotshot.stats.load(self.logfilename) if self.filename: stats.dump_stats(self.filename) stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.print_stats(40)
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 process_response(self, request, response): if self.show_profile(request): stats = self.stats() if 'prof_strip' in request.GET: stats.strip_dirs() if 'prof_sort' in request.GET: # See # http://docs.python.org/2/library/profile.html#pstats.Stats.sort_stats # noqa # for the fields you can sort on. stats.sort_stats(*request.GET['prof_sort'].split(',')) else: stats.sort_stats('time', 'calls') # Capture STDOUT temporarily old_stdout = sys.stdout out = StringIO() sys.stdout = out stats.print_stats() stats_str = out.getvalue() sys.stdout.close() sys.stdout = old_stdout # Print status within PRE block if response and response.content and stats_str: response.content = "<pre>" + stats_str + "</pre>" return response
def wrapped(*args, **kwds): """ Inner method for calling the profiler method. """ # define the profile name filename = os.path.join(path, '%s.prof' % func.__name__) # create a profiler for the method to run through prof = hotshot.Profile(filename) results = prof.runcall(func, *args, **kwds) prof.close() # log the information about it stats = hotshot.stats.load(filename) if stripDirs: stats.strip_dirs() # we don't want to know about the arguments for this method stats.sort_stats(*sorting) stats.print_stats(limit) # remove the file if desired if autoclean: os.remove(filename) return results
def profile(filename, command): import hotshot, hotshot.stats, os file=os.path.abspath(filename) profile=hotshot.Profile(file) profile.run(command) profile.close() del profile howmany=100 stats=hotshot.stats.load(file) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100) stats.sort_stats('cum', 'calls') stats.print_stats(100) stats.sort_stats('calls', 'time') stats.print_stats(100) sys.exit(0)
def atexit(self): """Stop profiling and print profile information to sys.stderr. This function is registered as an atexit hook. """ self.profiler.close() funcname = self.fn.__name__ filename = self.fn.__code__.co_filename lineno = self.fn.__code__.co_firstlineno print("") print("*** PROFILER RESULTS ***") print("%s (%s:%s)" % (funcname, filename, lineno)) if self.skipped: skipped = "(%d calls not profiled)" % self.skipped else: skipped = "" print("function called %d times%s" % (self.ncalls, skipped)) print("") stats = hotshot.stats.load(self.logfilename) # hotshot.stats.load takes ages, and the .prof file eats megabytes, but # a saved stats object is small and fast if self.filename: stats.dump_stats(self.filename) # it is best to save before strip_dirs stats.strip_dirs() stats.sort_stats('cumulative', 'time', 'calls') stats.print_stats(40)
def prof_main(argv): import getopt import hotshot, hotshot.stats def usage(): print 'usage: %s module.function [args ...]' % argv[0] return 100 args = argv[1:] if len(args) < 1: return usage() name = args.pop(0) prof = name+'.prof' i = name.rindex('.') (modname, funcname) = (name[:i], name[i+1:]) module = __import__(modname, fromlist=1) func = getattr(module, funcname) if args: args.insert(0, argv[0]) prof = hotshot.Profile(prof) prof.runcall(lambda : func(args)) prof.close() else: stats = hotshot.stats.load(prof) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(1000) return
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 run_profile(self, slosl_statements, db_attributes, init_code): import hotshot, hotshot.stats, os, sys stderr = sys.stderr # discard runtime warning for tempnam sys.stderr = StringIO() prof_filename = os.tempnam(None, 'slow-') sys.stderr = stderr prof = hotshot.Profile(prof_filename) views = prof.runcall(self.build_views, slosl_statements, db_attributes, init_code) prof.close() stats = hotshot.stats.load(prof_filename) stats.strip_dirs() stdout = sys.stdout stats.sort_stats('time', 'calls') sys.stdout = StringIO() stats.print_stats() profile_data = sys.stdout.getvalue() stats.sort_stats('cumulative', 'calls') sys.stdout = StringIO() stats.print_stats() self.profile_data = (profile_data, sys.stdout.getvalue()) sys.stdout = stdout os.remove(prof_filename) return views
def process_view(self, request, view, *args, **kwargs): for item in request.META['QUERY_STRING'].split('&'): if item.split('=')[0] == 'profile': # profile in query string # catch the output, must happen before stats object is created # see https://bugs.launchpad.net/webpy/+bug/133080 for the details std_old, std_new = sys.stdout, StringIO.StringIO() sys.stdout = std_new # now let's do some profiling tmpfile = '/tmp/%s' % request.COOKIES['sessionid'] prof = hotshot.Profile(tmpfile) # make a call to the actual view function with the given arguments response = prof.runcall(view, request, *args[0], *args[1]) prof.close() # and then statistical reporting stats = hotshot.stats.load(tmpfile) stats.strip_dirs() stats.sort_stats('time') # do the output stats.print_stats(1.0) # restore default output sys.stdout = std_old # delete file os.remove(tmpfile) return HttpResponse('<pre\>%s</pre>' % std_new.getvalue()) return None
def process_response(self, request, response): if self.show_profile(request): stats = self.stats() if 'prof_strip' in request.GET: stats.strip_dirs() if 'prof_sort' in request.GET: stats.sort_stats(*request.GET['prof_sort'].split(',')) else: stats.sort_stats('time', 'calls') # Capture STDOUT temporarily old_stdout = sys.stdout out = StringIO() sys.stdout = out stats.print_stats() stats_str = out.getvalue() sys.stdout.close() sys.stdout = old_stdout # Print status within PRE block if response and response.content and stats_str: response.content = "<pre>" + stats_str + "</pre>" return response
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 stopProfiling(): c.profile.stop() c.profile.close() stats = hotshot.stats.load("BT.prof") stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(20)
def profilefunction(f): prof = hotshot.Profile("test.prof") prof.runcall(f) prof.close() stats = hotshot.stats.load("test.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(10)
def pfile(function): prof = hotshot.Profile('/tmp/test.prof') prof.runcall(function) prof.close() stats = hotshot.stats.load('/tmp/test.prof') stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20)
def main(): opts, args = parse_options() bind = parse_bind(opts.bind) if opts.validate: application = (Application() + Root()) app = validator(application) httpd = make_server(bind[0], bind[1], app) httpd.serve_forever() raise SystemExit(0) manager = Manager() opts.debug and Debugger().register(manager) Poller = select_poller(opts.poller.lower()) Poller().register(manager) if opts.server.lower() == "base": BaseServer(bind).register(manager) HelloWorld().register(manager) else: Server(bind).register(manager) Root().register(manager) docroot = os.getcwd() if not args else args[0] Static(docroot=docroot, dirlisting=True).register(manager) opts.passwd and Authentication(passwd=opts.passwd).register(manager) opts.logging and Logger().register(manager) if opts.profile and hotshot: profiler = hotshot.Profile(".profile") profiler.start() if opts.debug: print(graph(manager, name="circuits.web")) print() print(inspect(manager)) for i in range(opts.jobs): manager.start(process=True) manager.run() if opts.profile and hotshot: profiler.stop() profiler.close() stats = hotshot.stats.load(".profile") stats.strip_dirs() stats.sort_stats("time", "calls") stats.print_stats(20)
def print_profile(profile_path): # .stats needs extra package (python-profile) # so relative import is here # Ergo stats analysis on home computer only! import hotshot.stats stats = hotshot.stats.load(profile_path) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40)
def profile(func, *args, **kwargs): prof = hotshot.Profile("object.prof") prof.runcall(func, *args, **kwargs) prof.close() stats = hotshot.stats.load("object.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(30) os.remove('object.prof')
def profile_main(): import hotshot, hotshot.stats prof = hotshot.Profile("_parsefortran.prof") prof.runcall(simple_main) prof.close() stats = hotshot.stats.load("_parsefortran.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(30)
def process_response(self, request, response): if settings.DEBUG and 'prof' in request.GET: self.profiler.create_stats() io = cStringIO() stats = pstats.Stats(self.profiler, stream=io) stats.strip_dirs().sort_stats(request.GET.get('sort', 'time')) stats.print_stats(int(request.GET.get('count', 10000))) response.content = '<pre>%s</pre>' % io.getvalue() return response
def profile_me (): print 'Profiling MATE Sudoku' import tempfile, hotshot, hotshot.stats pname = os.path.join(tempfile.gettempdir(), 'MATE_SUDOKU_HOTSHOT_PROFILE') prof = hotshot.Profile(pname) prof.runcall(start_game) stats = hotshot.stats.load(pname) stats.strip_dirs() stats.sort_stats('time', 'calls').print_stats()
def doProf(profname,func,*args,**kwd): import hotshot, hotshot.stats prof = hotshot.Profile(profname) prof.runcall(func) prof.close() stats = hotshot.stats.load(profname) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20)
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 _profile(): import hotshot, hotshot.stats prof = hotshot.Profile("recognizer.prof") prof.run('_test()') prof.close() stats = hotshot.stats.load("recognizer.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(40)
def profile(): print "profile starts" prof = hotshot.Profile("test.prof") prof.runcall(foo) #test.test_all.main) prof.close() stats = hotshot.stats.load("test.prof") stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(100)
av = sys.argv[1:] if not av: main(av) firstarg = av[0].lower() if firstarg == "hotshot": import hotshot import hotshot.stats av = av[1:] prof_log_name = "XXXX.prof" prof = hotshot.Profile(prof_log_name) # benchtime, result = prof.runcall(main, *av) result = prof.runcall(main, *(av, )) print("result", repr(result)) prof.close() stats = hotshot.stats.load(prof_log_name) stats.strip_dirs() stats.sort_stats('time', 'calls') stats.print_stats(20) elif firstarg == "profile": import cProfile av = av[1:] cProfile.run('main(av)', 'YYYY.prof') import pstats p = pstats.Stats('YYYY.prof') p.strip_dirs().sort_stats('cumulative').print_stats(30) elif firstarg == "psyco": PSYCO = 1 main(av[1:]) else: main(av)