blob: 406d70305f9e9c8d64a42eccb1617d3ffd968094 [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
Stéphane Wirtelfcd5e842018-10-17 12:03:40 +02005import unittest
Benjamin Peterson4e299c72008-10-06 21:03:05 +00006
7# rip off all interesting stuff from test_profile
8import cProfile
9from test.test_profile import ProfileTest, regenerate_expected_output
Sanyam Khurana7973e272017-11-08 16:20:56 +053010from test.support.script_helper import assert_python_failure, assert_python_ok
Serhiy Storchakae437a102016-04-24 21:41:02 +030011
Benjamin Peterson4e299c72008-10-06 21:03:05 +000012
13class CProfileTest(ProfileTest):
14 profilerclass = cProfile.Profile
Giampaolo Rodola'b071d4f2013-02-12 14:31:06 +010015 profilermodule = cProfile
Antoine Pitrou8477f7a2014-06-27 23:49:29 -040016 expected_max_output = "{built-in method builtins.max}"
Benjamin Peterson4e299c72008-10-06 21:03:05 +000017
Benjamin Peterson7d766532008-10-06 22:05:00 +000018 def get_expected_output(self):
19 return _ProfileOutput
20
Benjamin Peterson6ccc7032008-10-07 02:32:59 +000021 # Issue 3895.
22 def test_bad_counter_during_dealloc(self):
23 import _lsprof
24 # Must use a file as StringIO doesn't trigger the bug.
Victor Stinner7f868112011-06-30 00:00:45 +020025 orig_stderr = sys.stderr
26 try:
27 with open(TESTFN, 'w') as file:
28 sys.stderr = file
29 try:
30 obj = _lsprof.Profiler(lambda: int)
31 obj.enable()
32 obj = _lsprof.Profiler(1)
33 obj.disable()
Christian Heimes647f1202013-12-05 07:40:29 +010034 obj.clear()
Victor Stinner7f868112011-06-30 00:00:45 +020035 finally:
36 sys.stderr = orig_stderr
37 finally:
38 unlink(TESTFN)
Benjamin Peterson6ccc7032008-10-07 02:32:59 +000039
Sanyam Khurana7973e272017-11-08 16:20:56 +053040 # Issue 21862
41 def test_module_path_option(self):
42 # Test -m switch with modules
43
44 # Test that -m switch needs an argument
45 assert_python_failure('-m', 'cProfile', '-m')
46
47 # Test failure for not-existent module
48 assert_python_failure('-m', 'cProfile', '-m', 'random_module_xyz')
49
50 # Test successful run
51 assert_python_ok('-m', 'cProfile', '-m', 'timeit', '-n', '1')
52
Scott Sanderson2e01b752018-06-01 16:36:23 -040053 def test_profile_enable_disable(self):
54 prof = self.profilerclass()
55 # Make sure we clean ourselves up if the test fails for some reason.
56 self.addCleanup(prof.disable)
57
58 prof.enable()
59 self.assertIs(sys.getprofile(), prof)
60
61 prof.disable()
62 self.assertIs(sys.getprofile(), None)
63
64 def test_profile_as_context_manager(self):
65 prof = self.profilerclass()
66 # Make sure we clean ourselves up if the test fails for some reason.
67 self.addCleanup(prof.disable)
68
69 with prof as __enter__return_value:
70 # profile.__enter__ should return itself.
71 self.assertIs(prof, __enter__return_value)
72
73 # profile should be set as the global profiler inside the
74 # with-block
75 self.assertIs(sys.getprofile(), prof)
76
77 # profile shouldn't be set once we leave the with-block.
78 self.assertIs(sys.getprofile(), None)
79
Stéphane Wirtelfcd5e842018-10-17 12:03:40 +020080class TestCommandLine(unittest.TestCase):
81 def test_sort(self):
82 rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo')
83 self.assertGreater(rc, 0)
84 self.assertIn(b"option -s: invalid choice: 'demo'", err)
Benjamin Peterson4e299c72008-10-06 21:03:05 +000085
86def test_main():
Stéphane Wirtelfcd5e842018-10-17 12:03:40 +020087 run_unittest(CProfileTest, TestCommandLine)
Benjamin Peterson4e299c72008-10-06 21:03:05 +000088
89def main():
90 if '-r' not in sys.argv:
91 test_main()
92 else:
93 regenerate_expected_output(__file__, CProfileTest)
94
95
96# Don't remove this comment. Everything below it is auto-generated.
97#--cut--------------------------------------------------------------------------
Benjamin Peterson7d766532008-10-06 22:05:00 +000098_ProfileOutput = {}
99_ProfileOutput['print_stats'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000100 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__)
101 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc)
102 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial)
103 20 0.020 0.001 0.020 0.001 profilee.py:48(mul)
104 2 0.040 0.020 0.600 0.300 profilee.py:55(helper)
105 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1)
106 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect)
107 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2)
108 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)"""
Benjamin Peterson7d766532008-10-06 22:05:00 +0000109_ProfileOutput['print_callers'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000110profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper)
111profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>)
112profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc)
113 20/3 0.130 0.147 profilee.py:35(factorial)
114 2 0.006 0.040 profilee.py:84(helper2_indirect)
115profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial)
116profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc)
117profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper)
118profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper)
119profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper)
120 2 0.078 0.100 profilee.py:84(helper2_indirect)
121profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400122{built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1)
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000123 8 0.000 0.008 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400124{built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1)
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000125{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)"""
Benjamin Peterson7d766532008-10-06 22:05:00 +0000126_ProfileOutput['print_callees'] = """\
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000127<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc)
128profilee.py:110(__getattr__) ->
129profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial)
130 2 0.040 0.600 profilee.py:55(helper)
131profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial)
132 20 0.020 0.020 profilee.py:48(mul)
133profilee.py:48(mul) ->
134profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1)
135 2 0.000 0.140 profilee.py:84(helper2_indirect)
136 6 0.234 0.300 profilee.py:88(helper2)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400137profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr}
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000138profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial)
139 2 0.078 0.100 profilee.py:88(helper2)
140profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper)
141profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__)
Antoine Pitrou8477f7a2014-06-27 23:49:29 -0400142{built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)"""
Benjamin Peterson4e299c72008-10-06 21:03:05 +0000143
144if __name__ == "__main__":
145 main()