blob: 632911df063f51ccf96e775a5feb7fbd640417e1 [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
9from test.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)
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
47def 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 Rigoa871ef22006-02-08 12:53:56 +000066
67def test_main():
Christian Heimes2202f872008-02-06 14:31:34 +000068 run_unittest(ProfileTest)
Guido van Rossumf137f752001-10-04 00:58:24 +000069
Christian Heimes2202f872008-02-06 14:31:34 +000070def main():
71 if '-r' not in sys.argv:
72 test_main()
Armin Rigoa871ef22006-02-08 12:53:56 +000073 else:
Christian Heimes2202f872008-02-06 14:31:34 +000074 regenerate_expected_output(__file__, ProfileTest)
Armin Rigoa871ef22006-02-08 12:53:56 +000075
Guido van Rossumf137f752001-10-04 00:58:24 +000076
Christian Heimes2202f872008-02-06 14:31:34 +000077# Don't remove this comment. Everything below it is auto-generated.
78#--cut--------------------------------------------------------------------------
79ProfileTest.expected_output['print_stats'] = """\
80 126 function calls (106 primitive calls) in 999.751 CPU seconds
Guido van Rossumf137f752001-10-04 00:58:24 +000081
Christian Heimes2202f872008-02-06 14:31:34 +000082 Ordered by: standard name
Armin Rigoa871ef22006-02-08 12:53:56 +000083
Christian Heimes2202f872008-02-06 14:31:34 +000084 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 Rossumf137f752001-10-04 00:58:24 +0000105
Guido van Rossumf137f752001-10-04 00:58:24 +0000106
Christian Heimes2202f872008-02-06 14:31:34 +0000107"""
108ProfileTest.expected_output['print_callers'] = """\
109 Ordered by: standard name
Guido van Rossumf137f752001-10-04 00:58:24 +0000110
Christian Heimes2202f872008-02-06 14:31:34 +0000111Function 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
119io.py:1213(flush) <- :0(exec)(2) 999.753
120io.py:656(closed) <- io.py:874(flush)(2) -0.006
121io.py:874(flush) <- io.py:1213(flush)(2) -0.010
122profile:0(profiler) <-
123profile:0(testfunc()) <- profile:0(profiler)(1) 0.000
124profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964
125 profilee.py:98(subhelper)(16) 79.960
126profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767
127profilee.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
130profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917
131profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769
132profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830
133profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830
134profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830
135 profilee.py:84(helper2_indirect)(2) 139.946
136profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912
137
138
139"""
140ProfileTest.expected_output['print_callees'] = """\
141 Ordered by: standard name
142
143Function 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
151io.py:1213(flush) -> io.py:874(flush)(2) -0.006
152io.py:656(closed) ->
153io.py:874(flush) -> io.py:656(closed)(2) -0.002
154profile:0(profiler) -> profile:0(testfunc())(1) 999.751
155profile:0(testfunc()) -> :0(exec)(1) 999.753
156 :0(setprofile)(1) 0.000
157profilee.py:110(__getattr__) ->
158profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917
159 profilee.py:55(helper)(2) 599.830
160profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917
161 profilee.py:48(mul)(20) 19.980
162profilee.py:48(mul) ->
163profilee.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
166profilee.py:73(helper1) -> :0(append)(4) -0.004
167 :0(exc_info)(4) -0.004
168 :0(hasattr)(4) 11.964
169profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917
170 profilee.py:88(helper2)(2) 399.912
171profilee.py:88(helper2) -> :0(hasattr)(8) 11.964
172 profilee.py:98(subhelper)(8) 79.960
173profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972
174
175
176"""
Guido van Rossumf137f752001-10-04 00:58:24 +0000177
178if __name__ == "__main__":
Christian Heimes2202f872008-02-06 14:31:34 +0000179 main()