blob: 1430d22504854e67fe8956b9cf0ee4c2b25ab96d [file] [log] [blame]
Benjamin Peterson4e299c72008-10-06 21:03:05 +00001"""Test suite for the cProfile module."""
2
3import sys
Benjamin Peterson6ccc7032008-10-07 02:32:59 +00004from test.support import run_unittest, TESTFN, unlink
Benjamin Peterson4e299c72008-10-06 21:03:05 +00005
6# rip off all interesting stuff from test_profile
7import cProfile
8from test.test_profile import ProfileTest, regenerate_expected_output
Sanyam Khurana7973e272017-11-08 16:20:56 +05309from test.support.script_helper import assert_python_failure, assert_python_ok
Serhiy Storchakae437a102016-04-24 21:41:02 +030010
Benjamin Peterson4e299c72008-10-06 21:03:05 +000011
12class CProfileTest(ProfileTest):
13 profilerclass = cProfile.Profile
Giampaolo Rodola'b071d4f2013-02-12 14:31:06 +010014 profilermodule = cProfile
Antoine Pitrou8477f7a2014-06-27 23:49:29 -040015 expected_max_output = "{built-in method builtins.max}"
Benjamin Peterson4e299c72008-10-06 21:03:05 +000016
Benjamin Peterson7d766532008-10-06 22:05:00 +000017 def get_expected_output(self):
18 return _ProfileOutput
19
Benjamin Peterson6ccc7032008-10-07 02:32:59 +000020 # Issue 3895.
21 def test_bad_counter_during_dealloc(self):
22 import _lsprof
23 # Must use a file as StringIO doesn't trigger the bug.
Victor Stinner7f868112011-06-30 00:00:45 +020024 orig_stderr = sys.stderr
25 try:
26 with open(TESTFN, 'w') as file:
27 sys.stderr = file
28 try:
29 obj = _lsprof.Profiler(lambda: int)
30 obj.enable()
31 obj = _lsprof.Profiler(1)
32 obj.disable()
Christian Heimes647f1202013-12-05 07:40:29 +010033 obj.clear()
Victor Stinner7f868112011-06-30 00:00:45 +020034 finally:
35 sys.stderr = orig_stderr
36 finally:
37 unlink(TESTFN)
Benjamin Peterson6ccc7032008-10-07 02:32:59 +000038
Sanyam Khurana7973e272017-11-08 16:20:56 +053039 # Issue 21862
40 def test_module_path_option(self):
41 # Test -m switch with modules
42
43 # Test that -m switch needs an argument
44 assert_python_failure('-m', 'cProfile', '-m')
45
46 # Test failure for not-existent module
47 assert_python_failure('-m', 'cProfile', '-m', 'random_module_xyz')
48
49 # Test successful run
50 assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
51
Benjamin Peterson4e299c72008-10-06 21:03:05 +000052
53def test_main():
54 run_unittest(CProfileTest)
55
56def main():
57 if '-r' not in sys.argv:
58 test_main()
59 else:
60 regenerate_expected_output(__file__, CProfileTest)
61
62
63# Don't remove this comment. Everything below it is auto-generated.
64#--cut--------------------------------------------------------------------------
Benjamin Peterson7d766532008-10-06 22:05:00 +000065_ProfileOutput = {}
66_ProfileOutput['print_stats'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +000067 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
68 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
69 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
70 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
71 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
72 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
73 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
74 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
75 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
Benjamin Peterson7d766532008-10-06 22:05:00 +000076_ProfileOutput['print_callers'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +000077profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
78profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
79profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
80 20/3 0.130 0.147 profilee.py:35(factorial)
81 2 0.006 0.040 profilee.py:84(helper2_indirect)
82profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
83profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
84profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
85profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
86profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
87 2 0.078 0.100 profilee.py:84(helper2_indirect)
88profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -040089{built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
Benjamin Peterson4e299c72008-10-06 21:03:05 +000090 8 0.000 0.008 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -040091{built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
Benjamin Peterson4e299c72008-10-06 21:03:05 +000092{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
Benjamin Peterson7d766532008-10-06 22:05:00 +000093_ProfileOutput['print_callees'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +000094<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
95profilee.py:110(__getattr__) ->
96profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
97 2 0.040 0.600 profilee.py:55(helper)
98profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
99 20 0.020 0.020 profilee.py:48(mul)
100profilee.py:48(mul) ->
101profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
102 2 0.000 0.140 profilee.py:84(helper2_indirect)
103 6 0.234 0.300 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400104profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000105profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
106 2 0.078 0.100 profilee.py:88(helper2)
107profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
108profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400109{built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000110
111if __name__ == "__main__":
112 main()