def demo(): f = lambda x: x profiler = Profile() profiler.runcall(f) stats = Stats(profiler) stats.strip_dirs() stats.sort_stats('cumulative') stats.print_stats() stats.print_callers() stats.print_callees()
def tearDown(self): if ENABLE_PROFILE: if DUMP_PROFILE: self.pr.dump_stats('profile.out') p = Stats(self.pr) p.strip_dirs() p.sort_stats('time') p.print_stats(40) p.print_callees('types.py:846\(validate_value', 20) p.print_callees('types.py:828\(_validate_primitive_value', 20) p.print_callees('uploadsession.py:185\(write', 20) TestBase.teardown(self)
def filter(self, execution_func, prof_arg = None): # put thie imports here so the app doesn't choke if profiling # is not present (this is a debug-only feature anyway) import cProfile as profile from pstats import Stats tmpfile = tempfile.NamedTemporaryFile() file = line = func = None sort_order = 'time' if prof_arg: tokens = prof_arg.split(',') else: tokens = () for token in tokens: if token == "cum": sort_order = "cumulative" elif token == "name": sort_order = "name" else: try: file, line = prof_arg.split(':') line, func = line.split('(') func = func.strip(')') except: file = line = func = None try: profile.runctx('execution_func()', globals(), locals(), tmpfile.name) out = StringIO() stats = Stats(tmpfile.name, stream=out) stats.sort_stats(sort_order, 'calls') def parse_table(t, ncol): table = [] for s in t: t = [x for x in s.split(' ') if x] if len(t) > 1: table += [t[:ncol-1] + [' '.join(t[ncol-1:])]] return table def cmp(n): def _cmp(x, y): return 0 if x[n] == y[n] else 1 if x[n] < y[n] else -1 return _cmp if not file: stats.print_stats() stats_str = out.getvalue() statdata = stats_str.split('\n') headers = '\n'.join(statdata[:6]) table = parse_table(statdata[6:], 6) from r2.lib.pages import Profiling res = Profiling(header = headers, table = table, path = request.path).render() return [unicode(res)] else: query = "%s:%s" % (file, line) stats.print_callees(query) stats.print_callers(query) statdata = out.getvalue() data = statdata.split(query) callee = data[2].split('->')[1].split('Ordered by')[0] callee = parse_table(callee.split('\n'), 4) callee.sort(cmp(1)) callee = [['ncalls', 'tottime', 'cputime']] + callee i = 4 while '<-' not in data[i] and i < len(data): i+= 1 caller = data[i].split('<-')[1] caller = parse_table(caller.split('\n'), 4) caller.sort(cmp(1)) caller = [['ncalls', 'tottime', 'cputime']] + caller from r2.lib.pages import Profiling res = Profiling(header = prof_arg, caller = caller, callee = callee, path = request.path).render() return [unicode(res)] finally: tmpfile.close()
def filter(self, execution_func, prof_arg = None): import cProfile as profile from pstats import Stats tmpfile = tempfile.NamedTemporaryFile() try: file, line = prof_arg.split(':') line, func = line.split('(') func = func.strip(')') except: file = line = func = None try: profile.runctx('execution_func()', globals(), locals(), tmpfile.name) out = StringIO() stats = Stats(tmpfile.name, stream=out) stats.sort_stats('time', 'calls') def parse_table(t, ncol): table = [] for s in t: t = [x for x in s.split(' ') if x] if len(t) > 1: table += [t[:ncol-1] + [' '.join(t[ncol-1:])]] return table def cmp(n): def _cmp(x, y): return 0 if x[n] == y[n] else 1 if x[n] < y[n] else -1 return _cmp if not file: stats.print_stats() stats_str = out.getvalue() statdata = stats_str.split('\n') headers = '\n'.join(statdata[:6]) table = parse_table(statdata[6:], 6) from r2.lib.pages import Profiling res = Profiling(header = headers, table = table, path = request.path).render() return [unicode(res)] else: query = "%s:%s" % (file, line) stats.print_callees(query) stats.print_callers(query) statdata = out.getvalue() data = statdata.split(query) callee = data[2].split('->')[1].split('Ordered by')[0] callee = parse_table(callee.split('\n'), 4) callee.sort(cmp(1)) callee = [['ncalls', 'tottime', 'cputime']] + callee i = 4 while '<-' not in data[i] and i < len(data): i+= 1 caller = data[i].split('<-')[1] caller = parse_table(caller.split('\n'), 4) caller.sort(cmp(1)) caller = [['ncalls', 'tottime', 'cputime']] + caller from r2.lib.pages import Profiling res = Profiling(header = prof_arg, caller = caller, callee = callee, path = request.path).render() return [unicode(res)] finally: tmpfile.close()
class CProfileVStats(object): """Wrapper around pstats.Stats class.""" def __init__(self, output_file): self.output_file = output_file self.obj = Stats(output_file) self.reset_stream() def reset_stream(self): self.obj.stream = StringIO() def read(self): value = self.obj.stream.getvalue() self.reset_stream() # process stats output value = self._process_header(value) value = self._process_lines(value) return value IGNORE_FUNC_NAMES = ['function', ''] STATS_LINE_REGEX = r'(.*)\((.*)\)$' HEADER_LINE_REGEX = r'ncalls|tottime|cumtime' DEFAULT_SORT_ARG = 'cumulative' SORT_ARGS = { 'ncalls': 'calls', 'tottime': 'time', 'cumtime': 'cumulative', 'filename': 'module', 'lineno': 'nfl', } @classmethod def _process_header(cls, output): result = [] lines = output.splitlines(True) for idx, line in enumerate(lines): match = re.search(cls.HEADER_LINE_REGEX, line) if match: for key, val in cls.SORT_ARGS.iteritems(): url_link = template( "<a href='{{ url }}'>{{ key }}</a>", url=get_href(SORT_KEY, val), key=key) line = line.replace(key, url_link) lines[idx] = line break return ''.join(lines) @classmethod def _process_lines(cls, output): lines = output.splitlines(True) for idx, line in enumerate(lines): match = re.search(cls.STATS_LINE_REGEX, line) if match: prefix = match.group(1) func_name = match.group(2) if func_name not in cls.IGNORE_FUNC_NAMES: url_link = template( "<a href='{{ url }}'>{{ func_name }}</a>", url=get_href(FUNC_NAME_KEY, func_name), func_name=func_name) lines[idx] = template( "{{ prefix }}({{ !url_link }})\n", prefix=prefix, url_link=url_link) return ''.join(lines) def show(self, restriction=''): self.obj.print_stats(restriction) return self def show_callers(self, func_name): self.obj.print_callers(func_name) return self def show_callees(self, func_name): self.obj.print_callees(func_name) return self def sort(self, sort=''): sort = sort or self.DEFAULT_SORT_ARG self.obj.sort_stats(sort) return self
max_size = 10000 data_array = [randint(0, max_size) for _ in range(max_size)] test_func = lambda: insertion_sort(data_array, insert_value) profiler = Profile() print("profiler =", profiler) profiler.runcall(test_func) stats = Stats(profiler) print("stats =", stats) stats.strip_dirs() stats.sort_stats("cumulative") stats.print_stats() stats.print_callees() print("-" * 40) profiler2 = Profile() print("profiler2 =", profiler2) test_func_2 = lambda: insertion_sort(data_array, insert_value_ex) profiler2.runcall(test_func_2) stats2 = Stats(profiler2) print("stats2 =", stats2) stats2.strip_dirs() stats2.sort_stats("cumulative") stats2.print_stats()
a get_ipython().run_line_magic('logoff', '') get_ipython().run_line_magic('logstop', '') get_ipython().run_line_magic('logstop', '') get_ipython().run_line_magic('run', '-m cProfile -o cprofile.out -s cumulative 03_ml_data.py') get_ipython().run_line_magic('run', '-m cProfile -h') from pstats import Stats s=Stats(cprofile.out) s=Stats('cprofile.out') s print_stats(s) import pstats s.print_stats() s.print_caller() s.prim_calls() s.print_callees() s.print_call_line() s.strip_dirs() s=s.strip_dirs() s.sort_stats s.sort_stats() s=s.sort_stats('cumulative') s.print_callers() get_ipython().run_line_magic('logstart', '') s.print_callers() get_ipython().run_line_magic('run', '-m cProfile -s cumulative 03_ml_data.py >cprofile.out 2>&1') get_ipython().run_line_magic('run', '-m cProfile -s cumulative 03_ml_data.py 2>cprofile.out') get_ipython().run_line_magic('capture', 'cprofile.out') get_ipython().run_cell_magic('capture', 'cprofile.out', 'run -m cProfile -s cumulative 03_ml_data.py') run -m cProfile -s cumulative 03_ml_data.py cprofile.out
def filter(self, execution_func, prof_arg=None): # put thie imports here so the app doesn't choke if profiling # is not present (this is a debug-only feature anyway) import cProfile as profile from pstats import Stats tmpfile = tempfile.NamedTemporaryFile() try: file, line = prof_arg.split(":") line, func = line.split("(") func = func.strip(")") except: file = line = func = None try: profile.runctx("execution_func()", globals(), locals(), tmpfile.name) out = StringIO() stats = Stats(tmpfile.name, stream=out) stats.sort_stats("time", "calls") def parse_table(t, ncol): table = [] for s in t: t = [x for x in s.split(" ") if x] if len(t) > 1: table += [t[: ncol - 1] + [" ".join(t[ncol - 1 :])]] return table def cmp(n): def _cmp(x, y): return 0 if x[n] == y[n] else 1 if x[n] < y[n] else -1 return _cmp if not file: stats.print_stats() stats_str = out.getvalue() statdata = stats_str.split("\n") headers = "\n".join(statdata[:6]) table = parse_table(statdata[6:], 6) from r2.lib.pages import Profiling res = Profiling(header=headers, table=table, path=request.path).render() return [unicode(res)] else: query = "%s:%s" % (file, line) stats.print_callees(query) stats.print_callers(query) statdata = out.getvalue() data = statdata.split(query) callee = data[2].split("->")[1].split("Ordered by")[0] callee = parse_table(callee.split("\n"), 4) callee.sort(cmp(1)) callee = [["ncalls", "tottime", "cputime"]] + callee i = 4 while "<-" not in data[i] and i < len(data): i += 1 caller = data[i].split("<-")[1] caller = parse_table(caller.split("\n"), 4) caller.sort(cmp(1)) caller = [["ncalls", "tottime", "cputime"]] + caller from r2.lib.pages import Profiling res = Profiling(header=prof_arg, caller=caller, callee=callee, path=request.path).render() return [unicode(res)] finally: tmpfile.close()