blob: 84167ddc216d30120d5183c0d863bf9c62fe1514 [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 os
4import sys
5import pstats
6import unittest
7from difflib import unified_diff
8from io import StringIO
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test.support import run_unittest
Guido van Rossumf137f752001-10-04 00:58:24 +000010
Christian Heimes2202f872008-02-06 14:31:34 +000011import profile
12from test.profilee import testfunc, timer
Tim Peters527e64f2001-10-04 05:36:56 +000013
Christian Heimes2202f872008-02-06 14:31:34 +000014
15class 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)
Christian Heimesdae2a892008-04-19 00:55:37 +000025 start_timer = timer()
Christian Heimes2202f872008-02-06 14:31:34 +000026 prof.runctx("testfunc()", globals(), locals())
Christian Heimesdae2a892008-04-19 00:55:37 +000027 results.append(timer() - start_timer)
Christian Heimes2202f872008-02-06 14:31:34 +000028 for methodname in cls.methodnames:
29 s = StringIO()
30 stats = pstats.Stats(prof, stream=s)
31 stats.strip_dirs().sort_stats("stdname")
32 getattr(stats, methodname)()
Brett Cannonc17b35c2008-03-01 04:28:23 +000033 output = s.getvalue().splitlines()
34 mod_name = testfunc.__module__.rsplit('.', 1)[1]
35 # Only compare against stats originating from the test file.
36 # Prevents outside code (e.g., the io module) from causing
37 # unexpected output.
38 output = [line.rstrip() for line in output if mod_name in line]
39 results.append('\n'.join(output))
Christian Heimes2202f872008-02-06 14:31:34 +000040 return results
41
42 def test_cprofile(self):
43 results = self.do_profiling()
Christian Heimesdae2a892008-04-19 00:55:37 +000044 self.assertEqual(results[0], 1000)
Christian Heimes2202f872008-02-06 14:31:34 +000045 for i, method in enumerate(self.methodnames):
46 if results[i+1] != self.expected_output[method]:
47 print("Stats.%s output for %s doesn't fit expectation!" %
48 (method, self.profilerclass.__name__))
49 print('\n'.join(unified_diff(
50 results[i+1].split('\n'),
51 self.expected_output[method].split('\n'))))
52
53
54def regenerate_expected_output(filename, cls):
55 filename = filename.rstrip('co')
56 print('Regenerating %s...' % filename)
57 results = cls.do_profiling()
58
59 newfile = []
60 with open(filename, 'r') as f:
61 for line in f:
62 newfile.append(line)
Brett Cannonc17b35c2008-03-01 04:28:23 +000063 if line.startswith('#--cut'):
Christian Heimes2202f872008-02-06 14:31:34 +000064 break
65
66 with open(filename, 'w') as f:
67 f.writelines(newfile)
68 for i, method in enumerate(cls.methodnames):
69 f.write('%s.expected_output[%r] = """\\\n%s"""\n' % (
70 cls.__name__, method, results[i+1]))
71 f.write('\nif __name__ == "__main__":\n main()\n')
72
Armin Rigoa871ef22006-02-08 12:53:56 +000073
74def test_main():
Christian Heimes2202f872008-02-06 14:31:34 +000075 run_unittest(ProfileTest)
Guido van Rossumf137f752001-10-04 00:58:24 +000076
Christian Heimes2202f872008-02-06 14:31:34 +000077def main():
78 if '-r' not in sys.argv:
79 test_main()
Armin Rigoa871ef22006-02-08 12:53:56 +000080 else:
Christian Heimes2202f872008-02-06 14:31:34 +000081 regenerate_expected_output(__file__, ProfileTest)
Armin Rigoa871ef22006-02-08 12:53:56 +000082
Guido van Rossumf137f752001-10-04 00:58:24 +000083
Christian Heimes2202f872008-02-06 14:31:34 +000084# Don't remove this comment. Everything below it is auto-generated.
85#--cut--------------------------------------------------------------------------
86ProfileTest.expected_output['print_stats'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +000087 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__)
88 1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc)
89 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial)
90 20 19.980 0.999 19.980 0.999 profilee.py:48(mul)
91 2 39.986 19.993 599.830 299.915 profilee.py:55(helper)
92 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1)
93 2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect)
94 8 311.976 38.997 399.912 49.989 profilee.py:88(helper2)
Brett Cannonc17b35c2008-03-01 04:28:23 +000095 8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)"""
Christian Heimes2202f872008-02-06 14:31:34 +000096ProfileTest.expected_output['print_callers'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +000097:0(append) <- profilee.py:73(helper1)(4) 119.964
98:0(exc_info) <- profilee.py:73(helper1)(4) 119.964
Christian Heimes2202f872008-02-06 14:31:34 +000099:0(hasattr) <- profilee.py:73(helper1)(4) 119.964
100 profilee.py:88(helper2)(8) 399.912
Christian Heimes2202f872008-02-06 14:31:34 +0000101profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964
102 profilee.py:98(subhelper)(16) 79.960
103profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767
104profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769
105 profilee.py:35(factorial)(20) 169.917
106 profilee.py:84(helper2_indirect)(2) 139.946
107profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917
108profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769
109profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830
110profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830
111profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830
112 profilee.py:84(helper2_indirect)(2) 139.946
Brett Cannonc17b35c2008-03-01 04:28:23 +0000113profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912"""
Christian Heimes2202f872008-02-06 14:31:34 +0000114ProfileTest.expected_output['print_callees'] = """\
Christian Heimes2202f872008-02-06 14:31:34 +0000115:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972
Christian Heimes2202f872008-02-06 14:31:34 +0000116<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769
Christian Heimes2202f872008-02-06 14:31:34 +0000117profilee.py:110(__getattr__) ->
118profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917
119 profilee.py:55(helper)(2) 599.830
120profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917
121 profilee.py:48(mul)(20) 19.980
122profilee.py:48(mul) ->
123profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964
124 profilee.py:84(helper2_indirect)(2) 139.946
125 profilee.py:88(helper2)(6) 399.912
126profilee.py:73(helper1) -> :0(append)(4) -0.004
Christian Heimes2202f872008-02-06 14:31:34 +0000127profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917
128 profilee.py:88(helper2)(2) 399.912
129profilee.py:88(helper2) -> :0(hasattr)(8) 11.964
130 profilee.py:98(subhelper)(8) 79.960
Brett Cannonc17b35c2008-03-01 04:28:23 +0000131profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972"""
Guido van Rossumf137f752001-10-04 00:58:24 +0000132
133if __name__ == "__main__":
Christian Heimes2202f872008-02-06 14:31:34 +0000134 main()