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)() |
| 32 | results.append(s.getvalue()) |
| 33 | return results |
| 34 | |
| 35 | def test_cprofile(self): |
| 36 | results = self.do_profiling() |
| 37 | self.assertEqual(results[0], 43000) |
| 38 | for i, method in enumerate(self.methodnames): |
| 39 | if results[i+1] != self.expected_output[method]: |
| 40 | print("Stats.%s output for %s doesn't fit expectation!" % |
| 41 | (method, self.profilerclass.__name__)) |
| 42 | print('\n'.join(unified_diff( |
| 43 | results[i+1].split('\n'), |
| 44 | self.expected_output[method].split('\n')))) |
| 45 | |
| 46 | |
| 47 | def regenerate_expected_output(filename, cls): |
| 48 | filename = filename.rstrip('co') |
| 49 | print('Regenerating %s...' % filename) |
| 50 | results = cls.do_profiling() |
| 51 | |
| 52 | newfile = [] |
| 53 | with open(filename, 'r') as f: |
| 54 | for line in f: |
| 55 | newfile.append(line) |
| 56 | if line[:6] == '#--cut': |
| 57 | break |
| 58 | |
| 59 | with open(filename, 'w') as f: |
| 60 | f.writelines(newfile) |
| 61 | for i, method in enumerate(cls.methodnames): |
| 62 | f.write('%s.expected_output[%r] = """\\\n%s"""\n' % ( |
| 63 | cls.__name__, method, results[i+1])) |
| 64 | f.write('\nif __name__ == "__main__":\n main()\n') |
| 65 | |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 66 | |
| 67 | def test_main(): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 68 | run_unittest(ProfileTest) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 69 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 70 | def main(): |
| 71 | if '-r' not in sys.argv: |
| 72 | test_main() |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 73 | else: |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 74 | regenerate_expected_output(__file__, ProfileTest) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 77 | # Don't remove this comment. Everything below it is auto-generated. |
| 78 | #--cut-------------------------------------------------------------------------- |
| 79 | ProfileTest.expected_output['print_stats'] = """\ |
| 80 | 126 function calls (106 primitive calls) in 999.751 CPU seconds |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 81 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 82 | Ordered by: standard name |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 83 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 84 | ncalls tottime percall cumtime percall filename:lineno(function) |
| 85 | 4 -0.004 -0.001 -0.004 -0.001 :0(append) |
| 86 | 4 -0.004 -0.001 -0.004 -0.001 :0(exc_info) |
| 87 | 1 -0.004 -0.004 999.753 999.753 :0(exec) |
| 88 | 12 -0.024 -0.002 11.964 0.997 :0(hasattr) |
| 89 | 1 0.000 0.000 0.000 0.000 :0(setprofile) |
| 90 | 1 -0.002 -0.002 999.767 999.767 <string>:1(<module>) |
| 91 | 2 -0.004 -0.002 -0.010 -0.005 io.py:1213(flush) |
| 92 | 2 -0.002 -0.001 -0.002 -0.001 io.py:656(closed) |
| 93 | 2 -0.004 -0.002 -0.006 -0.003 io.py:874(flush) |
| 94 | 0 0.000 0.000 profile:0(profiler) |
| 95 | 1 -0.002 -0.002 999.751 999.751 profile:0(testfunc()) |
| 96 | 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__) |
| 97 | 1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc) |
| 98 | 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial) |
| 99 | 20 19.980 0.999 19.980 0.999 profilee.py:48(mul) |
| 100 | 2 39.986 19.993 599.830 299.915 profilee.py:55(helper) |
| 101 | 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1) |
| 102 | 2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect) |
| 103 | 8 311.976 38.997 399.912 49.989 profilee.py:88(helper2) |
| 104 | 8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 106 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 107 | """ |
| 108 | ProfileTest.expected_output['print_callers'] = """\ |
| 109 | Ordered by: standard name |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 110 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 111 | Function was called by... |
| 112 | :0(append) <- profilee.py:73(helper1)(4) 119.964 |
| 113 | :0(exc_info) <- profilee.py:73(helper1)(4) 119.964 |
| 114 | :0(exec) <- profile:0(testfunc())(1) 999.751 |
| 115 | :0(hasattr) <- profilee.py:73(helper1)(4) 119.964 |
| 116 | profilee.py:88(helper2)(8) 399.912 |
| 117 | :0(setprofile) <- profile:0(testfunc())(1) 999.751 |
| 118 | <string>:1(<module>) <- :0(exec)(1) 999.753 |
| 119 | io.py:1213(flush) <- :0(exec)(2) 999.753 |
| 120 | io.py:656(closed) <- io.py:874(flush)(2) -0.006 |
| 121 | io.py:874(flush) <- io.py:1213(flush)(2) -0.010 |
| 122 | profile:0(profiler) <- |
| 123 | profile:0(testfunc()) <- profile:0(profiler)(1) 0.000 |
| 124 | profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964 |
| 125 | profilee.py:98(subhelper)(16) 79.960 |
| 126 | profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767 |
| 127 | profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769 |
| 128 | profilee.py:35(factorial)(20) 169.917 |
| 129 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 130 | profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917 |
| 131 | profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769 |
| 132 | profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830 |
| 133 | profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830 |
| 134 | profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830 |
| 135 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 136 | profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912 |
| 137 | |
| 138 | |
| 139 | """ |
| 140 | ProfileTest.expected_output['print_callees'] = """\ |
| 141 | Ordered by: standard name |
| 142 | |
| 143 | Function called... |
| 144 | :0(append) -> |
| 145 | :0(exc_info) -> |
| 146 | :0(exec) -> <string>:1(<module>)(1) 999.767 |
| 147 | io.py:1213(flush)(2) -0.010 |
| 148 | :0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972 |
| 149 | :0(setprofile) -> |
| 150 | <string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769 |
| 151 | io.py:1213(flush) -> io.py:874(flush)(2) -0.006 |
| 152 | io.py:656(closed) -> |
| 153 | io.py:874(flush) -> io.py:656(closed)(2) -0.002 |
| 154 | profile:0(profiler) -> profile:0(testfunc())(1) 999.751 |
| 155 | profile:0(testfunc()) -> :0(exec)(1) 999.753 |
| 156 | :0(setprofile)(1) 0.000 |
| 157 | profilee.py:110(__getattr__) -> |
| 158 | profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917 |
| 159 | profilee.py:55(helper)(2) 599.830 |
| 160 | profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917 |
| 161 | profilee.py:48(mul)(20) 19.980 |
| 162 | profilee.py:48(mul) -> |
| 163 | profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964 |
| 164 | profilee.py:84(helper2_indirect)(2) 139.946 |
| 165 | profilee.py:88(helper2)(6) 399.912 |
| 166 | profilee.py:73(helper1) -> :0(append)(4) -0.004 |
| 167 | :0(exc_info)(4) -0.004 |
| 168 | :0(hasattr)(4) 11.964 |
| 169 | profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917 |
| 170 | profilee.py:88(helper2)(2) 399.912 |
| 171 | profilee.py:88(helper2) -> :0(hasattr)(8) 11.964 |
| 172 | profilee.py:98(subhelper)(8) 79.960 |
| 173 | profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972 |
| 174 | |
| 175 | |
| 176 | """ |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 177 | |
| 178 | if __name__ == "__main__": |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 179 | main() |