Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 1 | """Test suite for the profile module.""" |
| 2 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 3 | import os |
| 4 | import sys |
| 5 | import pstats |
| 6 | import unittest |
| 7 | from StringIO import StringIO |
| 8 | from test.test_support import run_unittest |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 9 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 10 | import profile |
| 11 | from test.profilee import testfunc, timer |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 12 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 13 | |
| 14 | class ProfileTest(unittest.TestCase): |
| 15 | |
| 16 | profilerclass = profile.Profile |
| 17 | methodnames = ['print_stats', 'print_callers', 'print_callees'] |
| 18 | expected_output = {} |
| 19 | |
| 20 | @classmethod |
| 21 | def do_profiling(cls): |
| 22 | results = [] |
| 23 | prof = cls.profilerclass(timer, 0.001) |
| 24 | prof.runctx("testfunc()", globals(), locals()) |
| 25 | results.append(timer()) |
| 26 | for methodname in cls.methodnames: |
| 27 | s = StringIO() |
| 28 | stats = pstats.Stats(prof, stream=s) |
| 29 | stats.strip_dirs().sort_stats("stdname") |
| 30 | getattr(stats, methodname)() |
| 31 | results.append(s.getvalue()) |
| 32 | return results |
| 33 | |
| 34 | def test_cprofile(self): |
| 35 | results = self.do_profiling() |
| 36 | self.assertEqual(results[0], 43000) |
| 37 | for i, method in enumerate(self.methodnames): |
| 38 | self.assertEqual(results[i+1], self.expected_output[method], |
| 39 | "Stats.%s output for %s doesn't fit expectation!" % |
| 40 | (method, self.profilerclass.__name__)) |
| 41 | |
| 42 | |
| 43 | def regenerate_expected_output(filename, cls): |
| 44 | filename = filename.rstrip('co') |
| 45 | print 'Regenerating %s...' % filename |
| 46 | results = cls.do_profiling() |
| 47 | |
| 48 | newfile = [] |
| 49 | with open(filename, 'r') as f: |
| 50 | for line in f: |
| 51 | newfile.append(line) |
| 52 | if line[:6] == '#--cut': |
| 53 | break |
| 54 | |
| 55 | with open(filename, 'w') as f: |
| 56 | f.writelines(newfile) |
| 57 | for i, method in enumerate(cls.methodnames): |
| 58 | f.write('%s.expected_output[%r] = """\\\n%s"""\n' % ( |
| 59 | cls.__name__, method, results[i+1])) |
| 60 | f.write('\nif __name__ == "__main__":\n main()\n') |
| 61 | |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 62 | |
| 63 | def test_main(): |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 64 | run_unittest(ProfileTest) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 65 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 66 | def main(): |
| 67 | if '-r' not in sys.argv: |
| 68 | test_main() |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 69 | else: |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 70 | regenerate_expected_output(__file__, ProfileTest) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 72 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 73 | # Don't remove this comment. Everything below it is auto-generated. |
| 74 | #--cut-------------------------------------------------------------------------- |
| 75 | ProfileTest.expected_output['print_stats'] = """\ |
| 76 | 127 function calls (107 primitive calls) in 999.749 CPU seconds |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 77 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 78 | Ordered by: standard name |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 79 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 80 | ncalls tottime percall cumtime percall filename:lineno(function) |
| 81 | 4 -0.004 -0.001 -0.004 -0.001 :0(append) |
| 82 | 4 -0.004 -0.001 -0.004 -0.001 :0(exc_info) |
| 83 | 12 -0.024 -0.002 11.964 0.997 :0(hasattr) |
| 84 | 8 -0.008 -0.001 -0.008 -0.001 :0(range) |
| 85 | 1 0.000 0.000 0.000 0.000 :0(setprofile) |
| 86 | 1 -0.002 -0.002 999.751 999.751 <string>:1(<module>) |
| 87 | 0 0.000 0.000 profile:0(profiler) |
| 88 | 1 -0.002 -0.002 999.749 999.749 profile:0(testfunc()) |
| 89 | 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__) |
| 90 | 1 269.996 269.996 999.753 999.753 profilee.py:25(testfunc) |
| 91 | 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial) |
| 92 | 20 19.980 0.999 19.980 0.999 profilee.py:48(mul) |
| 93 | 2 39.986 19.993 599.814 299.907 profilee.py:55(helper) |
| 94 | 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1) |
| 95 | 2 -0.006 -0.003 139.942 69.971 profilee.py:84(helper2_indirect) |
| 96 | 8 311.976 38.997 399.896 49.987 profilee.py:88(helper2) |
| 97 | 8 63.968 7.996 79.944 9.993 profilee.py:98(subhelper) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 99 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 100 | """ |
| 101 | ProfileTest.expected_output['print_callers'] = """\ |
| 102 | Ordered by: standard name |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 103 | |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 104 | Function was called by... |
| 105 | :0(append) <- profilee.py:73(helper1)(4) 119.964 |
| 106 | :0(exc_info) <- profilee.py:73(helper1)(4) 119.964 |
| 107 | :0(hasattr) <- profilee.py:73(helper1)(4) 119.964 |
| 108 | profilee.py:88(helper2)(8) 399.896 |
| 109 | :0(range) <- profilee.py:98(subhelper)(8) 79.944 |
| 110 | :0(setprofile) <- profile:0(testfunc())(1) 999.749 |
| 111 | <string>:1(<module>) <- profile:0(testfunc())(1) 999.749 |
| 112 | profile:0(profiler) <- |
| 113 | profile:0(testfunc()) <- profile:0(profiler)(1) 0.000 |
| 114 | profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964 |
| 115 | profilee.py:98(subhelper)(16) 79.944 |
| 116 | profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.751 |
| 117 | profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.753 |
| 118 | profilee.py:35(factorial)(20) 169.917 |
| 119 | profilee.py:84(helper2_indirect)(2) 139.942 |
| 120 | profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917 |
| 121 | profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.753 |
| 122 | profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.814 |
| 123 | profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.814 |
| 124 | profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.814 |
| 125 | profilee.py:84(helper2_indirect)(2) 139.942 |
| 126 | profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.896 |
| 127 | |
| 128 | |
| 129 | """ |
| 130 | ProfileTest.expected_output['print_callees'] = """\ |
| 131 | Ordered by: standard name |
| 132 | |
| 133 | Function called... |
| 134 | :0(append) -> |
| 135 | :0(exc_info) -> |
| 136 | :0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972 |
| 137 | :0(range) -> |
| 138 | :0(setprofile) -> |
| 139 | <string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.753 |
| 140 | profile:0(profiler) -> profile:0(testfunc())(1) 999.749 |
| 141 | profile:0(testfunc()) -> :0(setprofile)(1) 0.000 |
| 142 | <string>:1(<module>)(1) 999.751 |
| 143 | profilee.py:110(__getattr__) -> |
| 144 | profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917 |
| 145 | profilee.py:55(helper)(2) 599.814 |
| 146 | profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917 |
| 147 | profilee.py:48(mul)(20) 19.980 |
| 148 | profilee.py:48(mul) -> |
| 149 | profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964 |
| 150 | profilee.py:84(helper2_indirect)(2) 139.942 |
| 151 | profilee.py:88(helper2)(6) 399.896 |
| 152 | profilee.py:73(helper1) -> :0(append)(4) -0.004 |
| 153 | :0(exc_info)(4) -0.004 |
| 154 | :0(hasattr)(4) 11.964 |
| 155 | profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917 |
| 156 | profilee.py:88(helper2)(2) 399.896 |
| 157 | profilee.py:88(helper2) -> :0(hasattr)(8) 11.964 |
| 158 | profilee.py:98(subhelper)(8) 79.944 |
| 159 | profilee.py:98(subhelper) -> :0(range)(8) -0.008 |
| 160 | profilee.py:110(__getattr__)(16) 27.972 |
| 161 | |
| 162 | |
| 163 | """ |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 164 | |
| 165 | if __name__ == "__main__": |
Georg Brandl | b709077 | 2008-02-05 19:58:17 +0000 | [diff] [blame^] | 166 | main() |