def test_profile_parser(self): """ Verify that the function parse_profile produces the expected output. """ with contextlib.closing(StringIO()) as stream: with contextlib.redirect_stdout(stream): cProfile.run('print()') stream.seek(0) actual = list(parse_profile(stream)) expected = [[ 'ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.exec}' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.print}' ], [ '1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}" ]] self.assertListEqual(actual, expected)
def profile_table(self): for n, columns in enumerate(parse_profile(self.pyprofile)): location = columns[-1] if n and '{' not in location and '<' not in location: r = re.compile('(?P<src>.*\.py)\:(?P<num>[0-9]+).*') m = r.search(location) group = m.groupdict() src = group['src'] num = group['num'] name = 'c%d' % n fmt = '<a name={name} href="?pos={n}&file_path={src}&line_num={num}#{name}">{location}</a>' rep = fmt.format(**dict(group, **locals())) yield columns[:-1] + [mark_safe(rep)] else: yield columns
def test_profile_parser(self): """ Verify that the function parse_profile produces the expected output. """ with contextlib.closing(io.StringIO()) as stream: with contextlib.redirect_stdout(stream): cProfile.run('print()') stream.seek(0) actual = list(parse_profile(stream)) # Expected format for the profiling output on cPython implementations (PyPy differs) # actual = [ # ["ncalls", "tottime", "percall", "cumtime", "percall", "filename:lineno(function)"], # ["1", "0.000", "0.000", "0.000", "0.000", "<string>:1(<module>)"], # ["1", "0.000", "0.000", "0.000", "0.000", "{built-in method builtins.exec}"], # ["1", "0.000", "0.000", "0.000", "0.000", "{built-in method builtins.print}"], # ["1", "0.000", "0.000", "0.000", "0.000", "{method 'disable' of '_lsprof.Profiler' objects}"], # ] exc_header = [ "ncalls", "tottime", "percall", "cumtime", "percall", "filename:lineno(function)" ] self.assertEqual(actual[0], exc_header) exc_number = re.compile(r"\d(.\d+)?") exc_module = re.compile( r"({method.*})|({built-in.*})|(<.+>:\d+\(<.+>\))") exc_row = [ exc_number, exc_number, exc_number, exc_number, exc_number, exc_module ] for row in actual[1:]: for text, expected_regex in zip(row, exc_row): self.assertRegex( text, expected_regex, msg="Expected something like {} but found {}")
def test_profile_parser(self): """ Verify that the function parse_profile produces the expected output. """ with contextlib.closing(StringIO()) as stream: with contextlib.redirect_stdout(stream): cProfile.run('print()') stream.seek(0) actual = list(parse_profile(stream)) if PY3: if sys.version_info < (3,5): expected = [ ['ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)'], ['1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)'], ['1', '0.000', '0.000', '0.000', '0.000', '{built-in method exec}'], ['1', '0.000', '0.000', '0.000', '0.000', '{built-in method print}'], ['1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}"], ] else: expected = [ ['ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)'], ['1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)'], ['1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.exec}'], ['1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.print}'], ['1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}"], ] else: expected = [ ['ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)'], ['1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)'], ['2', '0.000', '0.000', '0.000', '0.000', 'StringIO.py:208(write)'], ['2', '0.000', '0.000', '0.000', '0.000', 'StringIO.py:38(_complain_ifclosed)'], ['2', '0.000', '0.000', '0.000', '0.000', '{isinstance}'], ['2', '0.000', '0.000', '0.000', '0.000', '{len}'], ['2', '0.000', '0.000', '0.000', '0.000', "{method 'append' of 'list' objects}"], ['1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}"] ] self.assertListEqual(actual, expected)
def test_profile_parser(self): """ Verify that the function parse_profile produces the expected output. """ with contextlib.closing(StringIO()) as stream: with contextlib.redirect_stdout(stream): cProfile.run('print()') stream.seek(0) actual = list(parse_profile(stream)) if PY3: if sys.version_info < (3, 5): expected = [ [ 'ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method exec}' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method print}' ], [ '1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}" ], ] else: expected = [ [ 'ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.exec}' ], [ '1', '0.000', '0.000', '0.000', '0.000', '{built-in method builtins.print}' ], [ '1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}" ], ] else: expected = [ [ 'ncalls', 'tottime', 'percall', 'cumtime', 'percall', 'filename:lineno(function)' ], [ '1', '0.000', '0.000', '0.000', '0.000', '<string>:1(<module>)' ], [ '2', '0.000', '0.000', '0.000', '0.000', 'StringIO.py:208(write)' ], [ '2', '0.000', '0.000', '0.000', '0.000', 'StringIO.py:38(_complain_ifclosed)' ], ['2', '0.000', '0.000', '0.000', '0.000', '{isinstance}'], ['2', '0.000', '0.000', '0.000', '0.000', '{len}'], [ '2', '0.000', '0.000', '0.000', '0.000', "{method 'append' of 'list' objects}" ], [ '1', '0.000', '0.000', '0.000', '0.000', "{method 'disable' of '_lsprof.Profiler' objects}" ] ] self.assertListEqual(actual, expected)