| 1 |
"""Test suite for the cProfile module.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
import unittest |
| 5 |
from test.test_support import run_unittest, TESTFN, unlink |
| 6 |
from test.support.script_helper import assert_python_failure |
| 7 |
|
| 8 |
|
| 9 |
import cProfile |
| 10 |
from test.test_profile import ProfileTest, regenerate_expected_output |
| 11 |
|
| 12 |
class CProfileTest(ProfileTest): |
| 13 |
profilerclass = cProfile.Profile |
| 14 |
expected_list_sort_output = "{method 'sort' of 'list' objects}" |
| 15 |
|
| 16 |
|
| 17 |
def test_bad_counter_during_dealloc(self): |
| 18 |
import _lsprof |
| 19 |
|
| 20 |
sys.stderr = open(TESTFN, 'w') |
| 21 |
try: |
| 22 |
obj = _lsprof.Profiler(lambda: int) |
| 23 |
obj.enable() |
| 24 |
obj = _lsprof.Profiler(1) |
| 25 |
obj.disable() |
| 26 |
finally: |
| 27 |
sys.stderr = sys.__stderr__ |
| 28 |
unlink(TESTFN) |
| 29 |
|
| 30 |
|
| 31 |
class TestCommandLine(unittest.TestCase): |
| 32 |
def test_sort(self): |
| 33 |
rc, out, err = assert_python_failure('-m', 'cProfile', '-s', 'demo') |
| 34 |
self.assertGreater(rc, 0) |
| 35 |
self.assertIn(b"option -s: invalid choice: 'demo'", err) |
| 36 |
|
| 37 |
def test_main(): |
| 38 |
run_unittest(CProfileTest, TestCommandLine) |
| 39 |
|
| 40 |
def main(): |
| 41 |
if '-r' not in sys.argv: |
| 42 |
test_main() |
| 43 |
else: |
| 44 |
regenerate_expected_output(__file__, CProfileTest) |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
CProfileTest.expected_output['print_stats'] = """\ |
| 50 |
126 function calls (106 primitive calls) in 1.000 seconds |
| 51 |
|
| 52 |
Ordered by: standard name |
| 53 |
|
| 54 |
ncalls tottime percall cumtime percall filename:lineno(function) |
| 55 |
1 0.000 0.000 1.000 1.000 <string>:1(<module>) |
| 56 |
28 0.028 0.001 0.028 0.001 profilee.py:110(__getattr__) |
| 57 |
1 0.270 0.270 1.000 1.000 profilee.py:25(testfunc) |
| 58 |
23/3 0.150 0.007 0.170 0.057 profilee.py:35(factorial) |
| 59 |
20 0.020 0.001 0.020 0.001 profilee.py:48(mul) |
| 60 |
2 0.040 0.020 0.600 0.300 profilee.py:55(helper) |
| 61 |
4 0.116 0.029 0.120 0.030 profilee.py:73(helper1) |
| 62 |
2 0.000 0.000 0.140 0.070 profilee.py:84(helper2_indirect) |
| 63 |
8 0.312 0.039 0.400 0.050 profilee.py:88(helper2) |
| 64 |
8 0.064 0.008 0.080 0.010 profilee.py:98(subhelper) |
| 65 |
12 0.000 0.000 0.012 0.001 {hasattr} |
| 66 |
4 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects} |
| 67 |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} |
| 68 |
8 0.000 0.000 0.000 0.000 {range} |
| 69 |
4 0.000 0.000 0.000 0.000 {sys.exc_info} |
| 70 |
|
| 71 |
|
| 72 |
""" |
| 73 |
CProfileTest.expected_output['print_callers'] = """\ |
| 74 |
Ordered by: standard name |
| 75 |
|
| 76 |
Function was called by... |
| 77 |
ncalls tottime cumtime |
| 78 |
<string>:1(<module>) <- |
| 79 |
profilee.py:110(__getattr__) <- 16 0.016 0.016 profilee.py:98(subhelper) |
| 80 |
12 0.012 0.012 {hasattr} |
| 81 |
profilee.py:25(testfunc) <- 1 0.270 1.000 <string>:1(<module>) |
| 82 |
profilee.py:35(factorial) <- 1 0.014 0.130 profilee.py:25(testfunc) |
| 83 |
20/3 0.130 0.147 profilee.py:35(factorial) |
| 84 |
2 0.006 0.040 profilee.py:84(helper2_indirect) |
| 85 |
profilee.py:48(mul) <- 20 0.020 0.020 profilee.py:35(factorial) |
| 86 |
profilee.py:55(helper) <- 2 0.040 0.600 profilee.py:25(testfunc) |
| 87 |
profilee.py:73(helper1) <- 4 0.116 0.120 profilee.py:55(helper) |
| 88 |
profilee.py:84(helper2_indirect) <- 2 0.000 0.140 profilee.py:55(helper) |
| 89 |
profilee.py:88(helper2) <- 6 0.234 0.300 profilee.py:55(helper) |
| 90 |
2 0.078 0.100 profilee.py:84(helper2_indirect) |
| 91 |
profilee.py:98(subhelper) <- 8 0.064 0.080 profilee.py:88(helper2) |
| 92 |
{hasattr} <- 4 0.000 0.004 profilee.py:73(helper1) |
| 93 |
8 0.000 0.008 profilee.py:88(helper2) |
| 94 |
{method 'append' of 'list' objects} <- 4 0.000 0.000 profilee.py:73(helper1) |
| 95 |
{method 'disable' of '_lsprof.Profiler' objects} <- |
| 96 |
{range} <- 8 0.000 0.000 profilee.py:98(subhelper) |
| 97 |
{sys.exc_info} <- 4 0.000 0.000 profilee.py:73(helper1) |
| 98 |
|
| 99 |
|
| 100 |
""" |
| 101 |
CProfileTest.expected_output['print_callees'] = """\ |
| 102 |
Ordered by: standard name |
| 103 |
|
| 104 |
Function called... |
| 105 |
ncalls tottime cumtime |
| 106 |
<string>:1(<module>) -> 1 0.270 1.000 profilee.py:25(testfunc) |
| 107 |
profilee.py:110(__getattr__) -> |
| 108 |
profilee.py:25(testfunc) -> 1 0.014 0.130 profilee.py:35(factorial) |
| 109 |
2 0.040 0.600 profilee.py:55(helper) |
| 110 |
profilee.py:35(factorial) -> 20/3 0.130 0.147 profilee.py:35(factorial) |
| 111 |
20 0.020 0.020 profilee.py:48(mul) |
| 112 |
profilee.py:48(mul) -> |
| 113 |
profilee.py:55(helper) -> 4 0.116 0.120 profilee.py:73(helper1) |
| 114 |
2 0.000 0.140 profilee.py:84(helper2_indirect) |
| 115 |
6 0.234 0.300 profilee.py:88(helper2) |
| 116 |
profilee.py:73(helper1) -> 4 0.000 0.004 {hasattr} |
| 117 |
4 0.000 0.000 {method 'append' of 'list' objects} |
| 118 |
4 0.000 0.000 {sys.exc_info} |
| 119 |
profilee.py:84(helper2_indirect) -> 2 0.006 0.040 profilee.py:35(factorial) |
| 120 |
2 0.078 0.100 profilee.py:88(helper2) |
| 121 |
profilee.py:88(helper2) -> 8 0.064 0.080 profilee.py:98(subhelper) |
| 122 |
8 0.000 0.008 {hasattr} |
| 123 |
profilee.py:98(subhelper) -> 16 0.016 0.016 profilee.py:110(__getattr__) |
| 124 |
8 0.000 0.000 {range} |
| 125 |
{hasattr} -> 12 0.012 0.012 profilee.py:110(__getattr__) |
| 126 |
{method 'append' of 'list' objects} -> |
| 127 |
{method 'disable' of '_lsprof.Profiler' objects} -> |
| 128 |
{range} -> |
| 129 |
{sys.exc_info} -> |
| 130 |
|
| 131 |
|
| 132 |
""" |
| 133 |
|
| 134 |
if __name__ == "__main__": |
| 135 |
main() |