def __call__(self, environ, start_response): request = MiniRequest(environ) if request.path_info == self.path: # we're being asked to render the profile view self.lock.acquire() try: text = self.index(request) finally: self.lock.release() start_response('200 OK', [('content-type', 'text/html; charset="UTF-8"'), ('content-length', str(len(text)))]) return [bytes_(text)] self.lock.acquire() try: _locals = locals() code = self.unwind and PROFILE_EXEC_EAGER or PROFILE_EXEC_LAZY self.profiler.runctx(code, globals(), _locals) if self.first_request: # discard to avoid timing warm-up self.profiler = profile.Profile() self.first_request = False else: self.profiler.dump_stats(self.log_filename) if HAS_PP2CT and self.cachegrind_filename is not None: stats = pstats.Stats(self.profiler) conv = pyprof2calltree.CalltreeConverter(stats) with open(self.cachegrind_filename, 'w') as grind: conv.output(grind) app_iter = _locals['app_iter_'] return app_iter finally: self.lock.release()
def __init__( self, app, global_conf=None, log_filename_prefix=DEFAULT_PROFILE_LOG, cachegrind_filename=None, dump_interval=10, dump_timestamp=True, discard_first_request=True, flush_at_shutdown=True, path='/__profile__', unwind=False, ): self.exists = os.path.exists # for __del__ self.remove = os.remove # for __del__ self.app = app self.profiler = profile.Profile() self.log_filename_prefix = log_filename_prefix self.cachegrind_filename = cachegrind_filename self.dump_interval = dump_interval self.dump_timestamp = dump_timestamp self.first_request = discard_first_request #self.lock = threading.Lock() self.flush_at_shutdown = flush_at_shutdown self.path = path self.unwind = unwind self.dump_task = RepeatedTimer(self.dump_interval, dump_profile, self.profiler, \ self.log_filename_prefix, os.getpid(), self.dump_timestamp)
def wrapper(*args, **kw): profiler = python_profile.Profile() profiler.enable() result = profiler.runcall(f, *args, **kw) profiler.disable() stats = pstats.Stats(profiler) if stripdirs: stats.strip_dirs() stats.sort_stats(*sort_columns) print("-" * 80) print print(title) print("") if lines == 0: stats.print_stats() else: stats.print_stats(lines) print("") return result
def __init__( self, app, global_conf=None, log_filename=DEFAULT_PROFILE_LOG, cachegrind_filename=None, discard_first_request=True, flush_at_shutdown=True, path='/__profile__', unwind=False, ): self.exists = os.path.exists # for __del__ self.remove = os.remove # for __del__ self.app = app self.profiler = profile.Profile() self.log_filename = log_filename self.cachegrind_filename = cachegrind_filename self.first_request = discard_first_request self.lock = threading.Lock() self.flush_at_shutdown = flush_at_shutdown self.path = path self.unwind = unwind
def reset_profiler(self): self.profile_file.unlink(missing_ok=True) self.cachegrind_file.unlink(missing_ok=True) self.profiled_app.profiler = profile.Profile()
def index(self, request, output=None): # output=None D/I for testing querydata = request.get_params() fulldirs = int(querydata.get('fulldirs', 0)) sort = querydata.get('sort', 'time') clear = querydata.get('clear', None) filename = querydata.get('filename', '').strip() limit = int(querydata.get('limit', 100)) mode = querydata.get('mode', 'stats') if output is None: output = StringIO() url = request.get_url() log_exists = os.path.exists(self.log_filename) if clear and log_exists: os.remove(self.log_filename) self.profiler = profile.Profile() log_exists = False if log_exists: stats = self.Stats(self.log_filename) # D/I if not fulldirs: stats.strip_dirs() stats.sort_stats(sort) if hasattr(stats, 'stream'): # python 2.5 stats.stream = output try: orig_stdout = sys.stdout # python 2.4 sys.stdout = output print_fn = getattr(stats, 'print_%s' % mode) if filename: print_fn(filename, limit) else: print_fn(limit) finally: sys.stdout = orig_stdout profiledata = output.getvalue() description = empty_description action = url formelements = '' filename = filename or '' if profiledata: description = """ Profiling information is generated using the standard Python profiler. To learn how to interpret the profiler statistics, see the <a href="http://www.python.org/doc/current/lib/module-profile.html"> Python profiler documentation</a>.""" sort_repl = '<option value="%s">' % sort sort_selected = '<option value="%s" selected>' % sort sort = sort_tmpl.replace(sort_repl, sort_selected) limit_repl = '<option value="%s">' % limit limit_selected = '<option value="%s" selected>' % limit limit = limit_tmpl.replace(limit_repl, limit_selected) mode_repl = '<option value="%s">' % mode mode_selected = '<option value="%s" selected>' % mode mode = mode_tmpl.replace(mode_repl, mode_selected) fulldirs_checked = '/>' fulldirs_repl = '/>' if fulldirs: fulldirs_checked = 'checked/>' fulldirs = fulldirs_tmpl.replace(fulldirs_repl, fulldirs_checked) filename_repl = 'value=""' filename_selected = 'value="%s"' % filename filename = filename_tmpl.replace(filename_repl, filename_selected) fulldirs_repl formelements = string.Template(formelements_tmpl) formelements = formelements.substitute({ 'description': description, 'action': action, 'sort': sort, 'limit': limit, 'fulldirs': fulldirs, 'mode': mode, 'filename': filename, }) index = string.Template(index_tmpl) index = index.substitute({ 'formelements': formelements, 'action': action, 'description': description, 'profiledata': profiledata, }) return index