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 |
| 9 | from test.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'] |
| 19 | expected_output = {} |
| 20 | |
| 21 | @classmethod |
| 22 | def do_profiling(cls): |
| 23 | results = [] |
| 24 | prof = cls.profilerclass(timer, 0.001) |
| 25 | prof.runctx("testfunc()", globals(), locals()) |
| 26 | results.append(timer()) |
| 27 | for methodname in cls.methodnames: |
| 28 | s = StringIO() |
| 29 | stats = pstats.Stats(prof, stream=s) |
| 30 | stats.strip_dirs().sort_stats("stdname") |
| 31 | getattr(stats, methodname)() |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame^] | 32 | output = s.getvalue().splitlines() |
| 33 | mod_name = testfunc.__module__.rsplit('.', 1)[1] |
| 34 | # Only compare against stats originating from the test file. |
| 35 | # Prevents outside code (e.g., the io module) from causing |
| 36 | # unexpected output. |
| 37 | output = [line.rstrip() for line in output if mod_name in line] |
| 38 | results.append('\n'.join(output)) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 39 | return results |
| 40 | |
| 41 | def test_cprofile(self): |
| 42 | results = self.do_profiling() |
| 43 | self.assertEqual(results[0], 43000) |
| 44 | for i, method in enumerate(self.methodnames): |
| 45 | if results[i+1] != self.expected_output[method]: |
| 46 | print("Stats.%s output for %s doesn't fit expectation!" % |
| 47 | (method, self.profilerclass.__name__)) |
| 48 | print('\n'.join(unified_diff( |
| 49 | results[i+1].split('\n'), |
| 50 | self.expected_output[method].split('\n')))) |
| 51 | |
| 52 | |
| 53 | def regenerate_expected_output(filename, cls): |
| 54 | filename = filename.rstrip('co') |
| 55 | print('Regenerating %s...' % filename) |
| 56 | results = cls.do_profiling() |
| 57 | |
| 58 | newfile = [] |
| 59 | with open(filename, 'r') as f: |
| 60 | for line in f: |
| 61 | newfile.append(line) |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame^] | 62 | if line.startswith('#--cut'): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 63 | break |
| 64 | |
| 65 | with open(filename, 'w') as f: |
| 66 | f.writelines(newfile) |
| 67 | for i, method in enumerate(cls.methodnames): |
| 68 | f.write('%s.expected_output[%r] = """\\\n%s"""\n' % ( |
| 69 | cls.__name__, method, results[i+1])) |
| 70 | f.write('\nif __name__ == "__main__":\n main()\n') |
| 71 | |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 72 | |
| 73 | def test_main(): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 74 | run_unittest(ProfileTest) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 75 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 76 | def main(): |
| 77 | if '-r' not in sys.argv: |
| 78 | test_main() |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 79 | else: |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 80 | regenerate_expected_output(__file__, ProfileTest) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 82 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 83 | # Don't remove this comment. Everything below it is auto-generated. |
| 84 | #--cut-------------------------------------------------------------------------- |
| 85 | ProfileTest.expected_output['print_stats'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 86 | 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__) |
| 87 | 1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc) |
| 88 | 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial) |
| 89 | 20 19.980 0.999 19.980 0.999 profilee.py:48(mul) |
| 90 | 2 39.986 19.993 599.830 299.915 profilee.py:55(helper) |
| 91 | 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1) |
| 92 | 2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect) |
| 93 | 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^] | 94 | 8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)""" |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 95 | ProfileTest.expected_output['print_callers'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 96 | :0(append) <- profilee.py:73(helper1)(4) 119.964 |
| 97 | :0(exc_info) <- profilee.py:73(helper1)(4) 119.964 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 98 | :0(hasattr) <- profilee.py:73(helper1)(4) 119.964 |
| 99 | profilee.py:88(helper2)(8) 399.912 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 100 | profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964 |
| 101 | profilee.py:98(subhelper)(16) 79.960 |
| 102 | profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767 |
| 103 | profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769 |
| 104 | profilee.py:35(factorial)(20) 169.917 |
| 105 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 106 | profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917 |
| 107 | profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769 |
| 108 | profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830 |
| 109 | profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830 |
| 110 | profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830 |
| 111 | profilee.py:84(helper2_indirect)(2) 139.946 |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame^] | 112 | profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912""" |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 113 | ProfileTest.expected_output['print_callees'] = """\ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 114 | :0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 115 | <string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 116 | profilee.py:110(__getattr__) -> |
| 117 | profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917 |
| 118 | profilee.py:55(helper)(2) 599.830 |
| 119 | profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917 |
| 120 | profilee.py:48(mul)(20) 19.980 |
| 121 | profilee.py:48(mul) -> |
| 122 | profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964 |
| 123 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 124 | profilee.py:88(helper2)(6) 399.912 |
| 125 | profilee.py:73(helper1) -> :0(append)(4) -0.004 |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 126 | profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917 |
| 127 | profilee.py:88(helper2)(2) 399.912 |
| 128 | profilee.py:88(helper2) -> :0(hasattr)(8) 11.964 |
| 129 | profilee.py:98(subhelper)(8) 79.960 |
Brett Cannon | c17b35c | 2008-03-01 04:28:23 +0000 | [diff] [blame^] | 130 | profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972""" |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 131 | |
| 132 | if __name__ == "__main__": |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 133 | main() |