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