Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 1 | """Test suite for the cProfile module.""" |
| 2 | |
| 3 | import sys |
Benjamin Peterson | 6ccc703 | 2008-10-07 02:32:59 +0000 | [diff] [blame] | 4 | from test.support import run_unittest, TESTFN, unlink |
Stéphane Wirtel | fcd5e84 | 2018-10-17 12:03:40 +0200 | [diff] [blame] | 5 | import unittest |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 6 | |
| 7 | # rip off all interesting stuff from test_profile |
| 8 | import cProfile |
| 9 | from test.test_profile import ProfileTest, regenerate_expected_output |
Sanyam Khurana | 7973e27 | 2017-11-08 16:20:56 +0530 | [diff] [blame] | 10 | from test.support.script_helper import assert_python_failure, assert_python_ok |
Serhiy Storchaka | e437a10 | 2016-04-24 21:41:02 +0300 | [diff] [blame] | 11 | |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 12 | |
| 13 | class CProfileTest(ProfileTest): |
| 14 | profilerclass = cProfile.Profile |
Giampaolo Rodola' | b071d4f | 2013-02-12 14:31:06 +0100 | [diff] [blame] | 15 | profilermodule = cProfile |
Antoine Pitrou | 8477f7a | 2014-06-27 23:49:29 -0400 | [diff] [blame] | 16 | expected_max_output = "{built-in method builtins.max}" |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 18 | def get_expected_output(self): |
| 19 | return _ProfileOutput |
| 20 | |
Benjamin Peterson | 6ccc703 | 2008-10-07 02:32:59 +0000 | [diff] [blame] | 21 | # 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 Stinner | 7f86811 | 2011-06-30 00:00:45 +0200 | [diff] [blame] | 25 | 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 Heimes | 647f120 | 2013-12-05 07:40:29 +0100 | [diff] [blame] | 34 | obj.clear() |
Victor Stinner | 7f86811 | 2011-06-30 00:00:45 +0200 | [diff] [blame] | 35 | finally: |
| 36 | sys.stderr = orig_stderr |
| 37 | finally: |
| 38 | unlink(TESTFN) |
Benjamin Peterson | 6ccc703 | 2008-10-07 02:32:59 +0000 | [diff] [blame] | 39 | |
Scott Sanderson | 2e01b75 | 2018-06-01 16:36:23 -0400 | [diff] [blame] | 40 | def test_profile_enable_disable(self): |
| 41 | prof = self.profilerclass() |
| 42 | # Make sure we clean ourselves up if the test fails for some reason. |
| 43 | self.addCleanup(prof.disable) |
| 44 | |
| 45 | prof.enable() |
| 46 | self.assertIs(sys.getprofile(), prof) |
| 47 | |
| 48 | prof.disable() |
| 49 | self.assertIs(sys.getprofile(), None) |
| 50 | |
| 51 | def test_profile_as_context_manager(self): |
| 52 | prof = self.profilerclass() |
| 53 | # Make sure we clean ourselves up if the test fails for some reason. |
| 54 | self.addCleanup(prof.disable) |
| 55 | |
| 56 | with prof as __enter__return_value: |
| 57 | # profile.__enter__ should return itself. |
| 58 | self.assertIs(prof, __enter__return_value) |
| 59 | |
| 60 | # profile should be set as the global profiler inside the |
| 61 | # with-block |
| 62 | self.assertIs(sys.getprofile(), prof) |
| 63 | |
| 64 | # profile shouldn't be set once we leave the with-block. |
| 65 | self.assertIs(sys.getprofile(), None) |
| 66 | |
Stéphane Wirtel | fcd5e84 | 2018-10-17 12:03:40 +0200 | [diff] [blame] | 67 | class TestCommandLine(unittest.TestCase): |
| 68 | def test_sort(self): |
| 69 | rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') |
| 70 | self.assertGreater(rc, 0) |
| 71 | self.assertIn(b"option -s: invalid choice: 'demo'", err) |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 72 | |
| 73 | def test_main(): |
Stéphane Wirtel | fcd5e84 | 2018-10-17 12:03:40 +0200 | [diff] [blame] | 74 | run_unittest(CProfileTest, TestCommandLine) |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 75 | |
| 76 | def main(): |
| 77 | if '-r' not in sys.argv: |
| 78 | test_main() |
| 79 | else: |
| 80 | regenerate_expected_output(__file__, CProfileTest) |
| 81 | |
| 82 | |
| 83 | # Don't remove this comment. Everything below it is auto-generated. |
| 84 | #--cut-------------------------------------------------------------------------- |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 85 | _ProfileOutput = {} |
| 86 | _ProfileOutput['print_stats'] = """\ |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 87 | 28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__) |
| 88 | 1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc) |
| 89 | 23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial) |
| 90 | 20 0.020 0.001 0.020 0.001 profilee.py:48(mul) |
| 91 | 2 0.040 0.020 0.600 0.300 profilee.py:55(helper) |
| 92 | 4 0.116 0.029 0.120 0.030 profilee.py:73(helper1) |
| 93 | 2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect) |
| 94 | 8 0.312 0.039 0.400 0.050 profilee.py:88(helper2) |
| 95 | 8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper)""" |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 96 | _ProfileOutput['print_callers'] = """\ |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 97 | profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper) |
| 98 | profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>) |
| 99 | profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc) |
| 100 | 20/3 0.130 0.147 profilee.py:35(factorial) |
| 101 | 2 0.006 0.040 profilee.py:84(helper2_indirect) |
| 102 | profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial) |
| 103 | profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc) |
| 104 | profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper) |
| 105 | profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper) |
| 106 | profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper) |
| 107 | 2 0.078 0.100 profilee.py:84(helper2_indirect) |
| 108 | profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2) |
Antoine Pitrou | 8477f7a | 2014-06-27 23:49:29 -0400 | [diff] [blame] | 109 | {built-in method builtins.hasattr} <- 4 0.000 0.004 profilee.py:73(helper1) |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 110 | 8 0.000 0.008 profilee.py:88(helper2) |
Antoine Pitrou | 8477f7a | 2014-06-27 23:49:29 -0400 | [diff] [blame] | 111 | {built-in method sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1) |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 112 | {method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1)""" |
Benjamin Peterson | 7d76653 | 2008-10-06 22:05:00 +0000 | [diff] [blame] | 113 | _ProfileOutput['print_callees'] = """\ |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 114 | <string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc) |
| 115 | profilee.py:110(__getattr__) -> |
| 116 | profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial) |
| 117 | 2 0.040 0.600 profilee.py:55(helper) |
| 118 | profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial) |
| 119 | 20 0.020 0.020 profilee.py:48(mul) |
| 120 | profilee.py:48(mul) -> |
| 121 | profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1) |
| 122 | 2 0.000 0.140 profilee.py:84(helper2_indirect) |
| 123 | 6 0.234 0.300 profilee.py:88(helper2) |
Antoine Pitrou | 8477f7a | 2014-06-27 23:49:29 -0400 | [diff] [blame] | 124 | profilee.py:73(helper1) -> 4 0.000 0.004 {built-in method builtins.hasattr} |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 125 | profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial) |
| 126 | 2 0.078 0.100 profilee.py:88(helper2) |
| 127 | profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper) |
| 128 | profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__) |
Antoine Pitrou | 8477f7a | 2014-06-27 23:49:29 -0400 | [diff] [blame] | 129 | {built-in method builtins.hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__)""" |
Benjamin Peterson | 4e299c7 | 2008-10-06 21:03:05 +0000 | [diff] [blame] | 130 | |
| 131 | if __name__ == "__main__": |
| 132 | main() |