blob: 0bd2530cf2abf5dd847716960e370ff3364f091f [file] [log] [blame]
Guido van Rossumf137f752001-10-04 00:58:24 +00001"""Test suite for the profile module."""
2
Georg Brandlb7090772008-02-05 19:58:17 +00003import os
4import sys
5import pstats
6import unittest
7from StringIO import StringIO
8from test.test_support import run_unittest
Guido van Rossumf137f752001-10-04 00:58:24 +00009
Georg Brandlb7090772008-02-05 19:58:17 +000010import profile
11from test.profilee import testfunc, timer
Tim Peters527e64f2001-10-04 05:36:56 +000012
Georg Brandlb7090772008-02-05 19:58:17 +000013
14class 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)
Amaury Forgeot d'Arc607bff12008-04-18 23:31:33 +000024 start_timer = timer()
Georg Brandlb7090772008-02-05 19:58:17 +000025 prof.runctx("testfunc()", globals(), locals())
Amaury Forgeot d'Arc607bff12008-04-18 23:31:33 +000026 results.append(timer() - start_timer)
Georg Brandlb7090772008-02-05 19:58:17 +000027 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()
Amaury Forgeot d'Arc607bff12008-04-18 23:31:33 +000037 self.assertEqual(results[0], 1000)
Georg Brandlb7090772008-02-05 19:58:17 +000038 for i, method in enumerate(self.methodnames):
39 self.assertEqual(results[i+1], self.expected_output[method],
40 "Stats.%s output for %s doesn't fit expectation!" %
41 (method, self.profilerclass.__name__))
42
43
44def regenerate_expected_output(filename, cls):
45 filename = filename.rstrip('co')
46 print 'Regenerating %s...' % filename
47 results = cls.do_profiling()
48
49 newfile = []
50 with open(filename, 'r') as f:
51 for line in f:
52 newfile.append(line)
53 if line[:6] == '#--cut':
54 break
55
56 with open(filename, 'w') as f:
57 f.writelines(newfile)
58 for i, method in enumerate(cls.methodnames):
59 f.write('%s.expected_output[%r] = """\\\n%s"""\n' % (
60 cls.__name__, method, results[i+1]))
61 f.write('\nif __name__ == "__main__":\n main()\n')
62
Armin Rigoa871ef22006-02-08 12:53:56 +000063
64def test_main():
Georg Brandlb7090772008-02-05 19:58:17 +000065 run_unittest(ProfileTest)
Guido van Rossumf137f752001-10-04 00:58:24 +000066
Georg Brandlb7090772008-02-05 19:58:17 +000067def main():
68 if '-r' not in sys.argv:
69 test_main()
Armin Rigoa871ef22006-02-08 12:53:56 +000070 else:
Georg Brandlb7090772008-02-05 19:58:17 +000071 regenerate_expected_output(__file__, ProfileTest)
Armin Rigoa871ef22006-02-08 12:53:56 +000072
Guido van Rossumf137f752001-10-04 00:58:24 +000073
Georg Brandlb7090772008-02-05 19:58:17 +000074# Don't remove this comment. Everything below it is auto-generated.
75#--cut--------------------------------------------------------------------------
76ProfileTest.expected_output['print_stats'] = """\
77 127 function calls (107 primitive calls) in 999.749 CPU seconds
Guido van Rossumf137f752001-10-04 00:58:24 +000078
Georg Brandlb7090772008-02-05 19:58:17 +000079 Ordered by: standard name
Armin Rigoa871ef22006-02-08 12:53:56 +000080
Georg Brandlb7090772008-02-05 19:58:17 +000081 ncalls tottime percall cumtime percall filename:lineno(function)
82 4 -0.004 -0.001 -0.004 -0.001 :0(append)
83 4 -0.004 -0.001 -0.004 -0.001 :0(exc_info)
84 12 -0.024 -0.002 11.964 0.997 :0(hasattr)
85 8 -0.008 -0.001 -0.008 -0.001 :0(range)
86 1 0.000 0.000 0.000 0.000 :0(setprofile)
87 1 -0.002 -0.002 999.751 999.751 <string>:1(<module>)
88 0 0.000 0.000 profile:0(profiler)
89 1 -0.002 -0.002 999.749 999.749 profile:0(testfunc())
90 28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__)
91 1 269.996 269.996 999.753 999.753 profilee.py:25(testfunc)
92 23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial)
93 20 19.980 0.999 19.980 0.999 profilee.py:48(mul)
94 2 39.986 19.993 599.814 299.907 profilee.py:55(helper)
95 4 115.984 28.996 119.964 29.991 profilee.py:73(helper1)
96 2 -0.006 -0.003 139.942 69.971 profilee.py:84(helper2_indirect)
97 8 311.976 38.997 399.896 49.987 profilee.py:88(helper2)
98 8 63.968 7.996 79.944 9.993 profilee.py:98(subhelper)
Guido van Rossumf137f752001-10-04 00:58:24 +000099
Guido van Rossumf137f752001-10-04 00:58:24 +0000100
Georg Brandlb7090772008-02-05 19:58:17 +0000101"""
102ProfileTest.expected_output['print_callers'] = """\
103 Ordered by: standard name
Guido van Rossumf137f752001-10-04 00:58:24 +0000104
Georg Brandlb7090772008-02-05 19:58:17 +0000105Function was called by...
106:0(append) <- profilee.py:73(helper1)(4) 119.964
107:0(exc_info) <- profilee.py:73(helper1)(4) 119.964
108:0(hasattr) <- profilee.py:73(helper1)(4) 119.964
109 profilee.py:88(helper2)(8) 399.896
110:0(range) <- profilee.py:98(subhelper)(8) 79.944
111:0(setprofile) <- profile:0(testfunc())(1) 999.749
112<string>:1(<module>) <- profile:0(testfunc())(1) 999.749
113profile:0(profiler) <-
114profile:0(testfunc()) <- profile:0(profiler)(1) 0.000
115profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964
116 profilee.py:98(subhelper)(16) 79.944
117profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.751
118profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.753
119 profilee.py:35(factorial)(20) 169.917
120 profilee.py:84(helper2_indirect)(2) 139.942
121profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917
122profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.753
123profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.814
124profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.814
125profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.814
126 profilee.py:84(helper2_indirect)(2) 139.942
127profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.896
128
129
130"""
131ProfileTest.expected_output['print_callees'] = """\
132 Ordered by: standard name
133
134Function called...
135:0(append) ->
136:0(exc_info) ->
137:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972
138:0(range) ->
139:0(setprofile) ->
140<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.753
141profile:0(profiler) -> profile:0(testfunc())(1) 999.749
142profile:0(testfunc()) -> :0(setprofile)(1) 0.000
143 <string>:1(<module>)(1) 999.751
144profilee.py:110(__getattr__) ->
145profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917
146 profilee.py:55(helper)(2) 599.814
147profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917
148 profilee.py:48(mul)(20) 19.980
149profilee.py:48(mul) ->
150profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964
151 profilee.py:84(helper2_indirect)(2) 139.942
152 profilee.py:88(helper2)(6) 399.896
153profilee.py:73(helper1) -> :0(append)(4) -0.004
154 :0(exc_info)(4) -0.004
155 :0(hasattr)(4) 11.964
156profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917
157 profilee.py:88(helper2)(2) 399.896
158profilee.py:88(helper2) -> :0(hasattr)(8) 11.964
159 profilee.py:98(subhelper)(8) 79.944
160profilee.py:98(subhelper) -> :0(range)(8) -0.008
161 profilee.py:110(__getattr__)(16) 27.972
162
163
164"""
Guido van Rossumf137f752001-10-04 00:58:24 +0000165
166if __name__ == "__main__":
Georg Brandlb7090772008-02-05 19:58:17 +0000167 main()