Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 1 | """Test suite for the profile module.""" |
| 2 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 3 | import os |
| 4 | import sys |
| 5 | import pstats |
| 6 | import unittest |
| 7 | from difflib import unified_diff |
| 8 | from io import StringIO |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test.support import run_unittest |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 10 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 11 | import profile |
| 12 | from test.profilee import testfunc, timer |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 13 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 14 | |
| 15 | class ProfileTest(unittest.TestCase): |
| 16 | |
| 17 | profilerclass = profile.Profile |
| 18 | methodnames = ['print_stats', 'print_callers', 'print_callees'] |
Antoine Pitrou | 8e124f3 | 2009-05-30 21:41:10 +0000 | [diff] [blame] | 19 | expected_max_output = ':0(max)' |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 20 | |
| 21 | def get_expected_output(self): |
| 22 | return _ProfileOutput |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 23 | |
| 24 | @classmethod |
| 25 | def do_profiling(cls): |
| 26 | results = [] |
| 27 | prof = cls.profilerclass(timer, 0.001) |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 28 | start_timer = timer() |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 29 | prof.runctx("testfunc()", globals(), locals()) |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 30 | results.append(timer() - start_timer) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 31 | for methodname in cls.methodnames: |
| 32 | s = StringIO() |
| 33 | stats = pstats.Stats(prof, stream=s) |
| 34 | stats.strip_dirs().sort_stats("stdname") |
| 35 | getattr(stats, methodname)() |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame] | 36 | output = s.getvalue().splitlines() |
| 37 | mod_name = testfunc.__module__.rsplit('.', 1)[1] |
| 38 | # Only compare against stats originating from the test file. |
| 39 | # Prevents outside code (e.g., the io module) from causing |
| 40 | # unexpected output. |
| 41 | output = [line.rstrip() for line in output if mod_name in line] |
| 42 | results.append('\n'.join(output)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 43 | return results |
| 44 | |
| 45 | def test_cprofile(self): |
| 46 | results = self.do_profiling() |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 47 | expected = self.get_expected_output() |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 48 | self.assertEqual(results[0], 1000) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 49 | for i, method in enumerate(self.methodnames): |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 50 | if results[i+1] != expected[method]: |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 51 | print("Stats.%s output for %s doesn't fit expectation!" % |
| 52 | (method, self.profilerclass.__name__)) |
| 53 | print('\n'.join(unified_diff( |
| 54 | results[i+1].split('\n'), |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 55 | expected[method].split('\n')))) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 56 | |
Antoine Pitrou | 8e124f3 | 2009-05-30 21:41:10 +0000 | [diff] [blame] | 57 | def test_calling_conventions(self): |
| 58 | # Issue #5330: profile and cProfile wouldn't report C functions called |
| 59 | # with keyword arguments. We test all calling conventions. |
| 60 | stmts = [ |
| 61 | "max([0])", |
| 62 | "max([0], key=int)", |
| 63 | "max([0], **dict(key=int))", |
| 64 | "max(*([0],))", |
| 65 | "max(*([0],), key=int)", |
| 66 | "max(*([0],), **dict(key=int))", |
| 67 | ] |
| 68 | for stmt in stmts: |
| 69 | s = StringIO() |
| 70 | prof = self.profilerclass(timer, 0.001) |
| 71 | prof.runctx(stmt, globals(), locals()) |
| 72 | stats = pstats.Stats(prof, stream=s) |
| 73 | stats.print_stats() |
| 74 | res = s.getvalue() |
| 75 | self.assertTrue(self.expected_max_output in res, |
| 76 | "Profiling {0!r} didn't report max:\n{1}".format(stmt, res)) |
| 77 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 78 | |
| 79 | def regenerate_expected_output(filename, cls): |
| 80 | filename = filename.rstrip('co') |
| 81 | print('Regenerating %s...' % filename) |
| 82 | results = cls.do_profiling() |
| 83 | |
| 84 | newfile = [] |
| 85 | with open(filename, 'r') as f: |
| 86 | for line in f: |
| 87 | newfile.append(line) |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame] | 88 | if line.startswith('#--cut'): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 89 | break |
| 90 | |
| 91 | with open(filename, 'w') as f: |
| 92 | f.writelines(newfile) |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 93 | f.write("_ProfileOutput = {}\n") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 94 | for i, method in enumerate(cls.methodnames): |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 95 | f.write('_ProfileOutput[%r] = """\\\n%s"""\n' % ( |
| 96 | method, results[i+1])) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 97 | f.write('\nif __name__ == "__main__":\n main()\n') |
| 98 | |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 99 | |
| 100 | def test_main(): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 101 | run_unittest(ProfileTest) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 102 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 103 | def main(): |
| 104 | if '-r' not in sys.argv: |
| 105 | test_main() |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 106 | else: |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | regenerate_expected_output(__file__, ProfileTest) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 109 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 110 | # Don't remove this comment. Everything below it is auto-generated. |
| 111 | #--cut-------------------------------------------------------------------------- |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 112 | _ProfileOutput = {} |
| 113 | _ProfileOutput['print_stats'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 114 | 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__) |
| 115 | 1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc) |
| 116 | 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial) |
| 117 | 20 19.980 0.999 19.980 0.999 profilee.py:48(mul) |
| 118 | 2 39.986 19.993 599.830 299.915 profilee.py:55(helper) |
| 119 | 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1) |
| 120 | 2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect) |
| 121 | 8 311.976 38.997 399.912 49.989 profilee.py:88(helper2) |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame] | 122 | 8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)""" |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 123 | _ProfileOutput['print_callers'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 124 | :0(append) <- profilee.py:73(helper1)(4) 119.964 |
| 125 | :0(exc_info) <- profilee.py:73(helper1)(4) 119.964 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 126 | :0(hasattr) <- profilee.py:73(helper1)(4) 119.964 |
| 127 | profilee.py:88(helper2)(8) 399.912 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 128 | profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964 |
| 129 | profilee.py:98(subhelper)(16) 79.960 |
| 130 | profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767 |
| 131 | profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769 |
| 132 | profilee.py:35(factorial)(20) 169.917 |
| 133 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 134 | profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917 |
| 135 | profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769 |
| 136 | profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830 |
| 137 | profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830 |
| 138 | profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830 |
| 139 | profilee.py:84(helper2_indirect)(2) 139.946 |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame] | 140 | profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912""" |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 141 | _ProfileOutput['print_callees'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 142 | :0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 143 | <string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 144 | profilee.py:110(__getattr__) -> |
| 145 | profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917 |
| 146 | profilee.py:55(helper)(2) 599.830 |
| 147 | profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917 |
| 148 | profilee.py:48(mul)(20) 19.980 |
| 149 | profilee.py:48(mul) -> |
| 150 | profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964 |
| 151 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 152 | profilee.py:88(helper2)(6) 399.912 |
| 153 | profilee.py:73(helper1) -> :0(append)(4) -0.004 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 154 | profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917 |
| 155 | profilee.py:88(helper2)(2) 399.912 |
| 156 | profilee.py:88(helper2) -> :0(hasattr)(8) 11.964 |
| 157 | profilee.py:98(subhelper)(8) 79.960 |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame] | 158 | profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972""" |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 159 | |
| 160 | if __name__ == "__main__": |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 161 | main() |