blob: cd7ec58e23122837c708ab555843b8d82d16e767 [file] [log] [blame]
Guido van Rossumf137f752001-10-04 00:58:24 +00001"""Test suite for the profile module."""
2
Christian Heimes2202f872008-02-06 14:31:34 +00003import sys
4import pstats
5import unittest
6from difflib import unified_diff
7from io import StringIO
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test.support import run_unittest
Guido van Rossumf137f752001-10-04 00:58:24 +00009
Christian Heimes2202f872008-02-06 14:31:34 +000010import profile
11from test.profilee import testfunc, timer
Tim Peters527e64f2001-10-04 05:36:56 +000012
Christian Heimes2202f872008-02-06 14:31:34 +000013
14class ProfileTest(unittest.TestCase):
15
16 profilerclass = profile.Profile
17 methodnames = ['print_stats', 'print_callers', 'print_callees']
Antoine Pitrou8e124f32009-05-30 21:41:10 +000018 expected_max_output = ':0(max)'
Benjamin Peterson7d766532008-10-06 22:05:00 +000019
20 def get_expected_output(self):
21 return _ProfileOutput
Christian Heimes2202f872008-02-06 14:31:34 +000022
23 @classmethod
24 def do_profiling(cls):
25 results = []
26 prof = cls.profilerclass(timer, 0.001)
Christian Heimesdae2a892008-04-19 00:55:37 +000027 start_timer = timer()
Christian Heimes2202f872008-02-06 14:31:34 +000028 prof.runctx("testfunc()", globals(), locals())
Christian Heimesdae2a892008-04-19 00:55:37 +000029 results.append(timer() - start_timer)
Christian Heimes2202f872008-02-06 14:31:34 +000030 for methodname in cls.methodnames:
31 s = StringIO()
32 stats = pstats.Stats(prof, stream=s)
33 stats.strip_dirs().sort_stats("stdname")
34 getattr(stats, methodname)()
Brett Cannonc17b35c2008-03-01 04:28:23 +000035 output = s.getvalue().splitlines()
36 mod_name = testfunc.__module__.rsplit('.', 1)[1]
37 # Only compare against stats originating from the test file.
38 # Prevents outside code (e.g., the io module) from causing
39 # unexpected output.
40 output = [line.rstrip() for line in output if mod_name in line]
41 results.append('\n'.join(output))
Christian Heimes2202f872008-02-06 14:31:34 +000042 return results
43
44 def test_cprofile(self):
45 results = self.do_profiling()
Benjamin Peterson7d766532008-10-06 22:05:00 +000046 expected = self.get_expected_output()
Christian Heimesdae2a892008-04-19 00:55:37 +000047 self.assertEqual(results[0], 1000)
Christian Heimes2202f872008-02-06 14:31:34 +000048 for i, method in enumerate(self.methodnames):
Benjamin Peterson7d766532008-10-06 22:05:00 +000049 if results[i+1] != expected[method]:
Christian Heimes2202f872008-02-06 14:31:34 +000050 print("Stats.%s output for %s doesn't fit expectation!" %
51 (method, self.profilerclass.__name__))
52 print('\n'.join(unified_diff(
53 results[i+1].split('\n'),
Benjamin Peterson7d766532008-10-06 22:05:00 +000054 expected[method].split('\n'))))
Christian Heimes2202f872008-02-06 14:31:34 +000055
Antoine Pitrou8e124f32009-05-30 21:41:10 +000056 def test_calling_conventions(self):
57 # Issue #5330: profile and cProfile wouldn't report C functions called
58 # with keyword arguments. We test all calling conventions.
59 stmts = [
60 "max([0])",
61 "max([0], key=int)",
62 "max([0], **dict(key=int))",
63 "max(*([0],))",
64 "max(*([0],), key=int)",
65 "max(*([0],), **dict(key=int))",
66 ]
67 for stmt in stmts:
68 s = StringIO()
69 prof = self.profilerclass(timer, 0.001)
70 prof.runctx(stmt, globals(), locals())
71 stats = pstats.Stats(prof, stream=s)
72 stats.print_stats()
73 res = s.getvalue()
Ezio Melottib58e0bd2010-01-23 15:40:09 +000074 self.assertIn(self.expected_max_output, res,
Antoine Pitrou8e124f32009-05-30 21:41:10 +000075 "Profiling {0!r} didn't report max:\n{1}".format(stmt, res))
76
Christian Heimes2202f872008-02-06 14:31:34 +000077
78def regenerate_expected_output(filename, cls):
79 filename = filename.rstrip('co')
80 print('Regenerating %s...' % filename)
81 results = cls.do_profiling()
82
83 newfile = []
84 with open(filename, 'r') as f:
85 for line in f:
86 newfile.append(line)
Brett Cannonc17b35c2008-03-01 04:28:23 +000087 if line.startswith('#--cut'):
Christian Heimes2202f872008-02-06 14:31:34 +000088 break
89
90 with open(filename, 'w') as f:
91 f.writelines(newfile)
Benjamin Peterson7d766532008-10-06 22:05:00 +000092 f.write("_ProfileOutput = {}\n")
Christian Heimes2202f872008-02-06 14:31:34 +000093 for i, method in enumerate(cls.methodnames):
Benjamin Peterson7d766532008-10-06 22:05:00 +000094 f.write('_ProfileOutput[%r] = """\\\n%s"""\n' % (
95 method, results[i+1]))
Christian Heimes2202f872008-02-06 14:31:34 +000096 f.write('\nif __name__ == "__main__":\n main()\n')
97
Armin Rigoa871ef22006-02-08 12:53:56 +000098
99def test_main():
Christian Heimes2202f872008-02-06 14:31:34 +0000100 run_unittest(ProfileTest)
Guido van Rossumf137f752001-10-04 00:58:24 +0000101
Christian Heimes2202f872008-02-06 14:31:34 +0000102def main():
103 if '-r' not in sys.argv:
104 test_main()
Armin Rigoa871ef22006-02-08 12:53:56 +0000105 else:
Christian Heimes2202f872008-02-06 14:31:34 +0000106 regenerate_expected_output(__file__, ProfileTest)
Armin Rigoa871ef22006-02-08 12:53:56 +0000107
Guido van Rossumf137f752001-10-04 00:58:24 +0000108
Christian Heimes2202f872008-02-06 14:31:34 +0000109# Don't remove this comment. Everything below it is auto-generated.
110#--cut--------------------------------------------------------------------------
Benjamin Peterson7d766532008-10-06 22:05:00 +0000111_ProfileOutput = {}
112_ProfileOutput['print_stats'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +0000113 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__)
114 1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc)
115 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial)
116 20 19.980 0.999 19.980 0.999 profilee.py:48(mul)
117 2 39.986 19.993 599.830 299.915 profilee.py:55(helper)
118 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1)
119 2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect)
120 8 311.976 38.997 399.912 49.989 profilee.py:88(helper2)
Brett Cannonc17b35c2008-03-01 04:28:23 +0000121 8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)"""
Benjamin Peterson7d766532008-10-06 22:05:00 +0000122_ProfileOutput['print_callers'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +0000123:0(append) <- profilee.py:73(helper1)(4) 119.964
124:0(exc_info) <- profilee.py:73(helper1)(4) 119.964
Christian Heimes2202f872008-02-06 14:31:34 +0000125:0(hasattr) <- profilee.py:73(helper1)(4) 119.964
126 profilee.py:88(helper2)(8) 399.912
Christian Heimes2202f872008-02-06 14:31:34 +0000127profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964
128 profilee.py:98(subhelper)(16) 79.960
129profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767
130profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769
131 profilee.py:35(factorial)(20) 169.917
132 profilee.py:84(helper2_indirect)(2) 139.946
133profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917
134profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769
135profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830
136profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830
137profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830
138 profilee.py:84(helper2_indirect)(2) 139.946
Brett Cannonc17b35c2008-03-01 04:28:23 +0000139profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912"""
Benjamin Peterson7d766532008-10-06 22:05:00 +0000140_ProfileOutput['print_callees'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +0000141:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972
Christian Heimes2202f872008-02-06 14:31:34 +0000142<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769
Christian Heimes2202f872008-02-06 14:31:34 +0000143profilee.py:110(__getattr__) ->
144profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917
145 profilee.py:55(helper)(2) 599.830
146profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917
147 profilee.py:48(mul)(20) 19.980
148profilee.py:48(mul) ->
149profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964
150 profilee.py:84(helper2_indirect)(2) 139.946
151 profilee.py:88(helper2)(6) 399.912
152profilee.py:73(helper1) -> :0(append)(4) -0.004
Christian Heimes2202f872008-02-06 14:31:34 +0000153profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917
154 profilee.py:88(helper2)(2) 399.912
155profilee.py:88(helper2) -> :0(hasattr)(8) 11.964
156 profilee.py:98(subhelper)(8) 79.960
Brett Cannonc17b35c2008-03-01 04:28:23 +0000157profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972"""
Guido van Rossumf137f752001-10-04 00:58:24 +0000158
159if __name__ == "__main__":
Christian Heimes2202f872008-02-06 14:31:34 +0000160 main()