| 1 |
"""Test suite for the profile module.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
import pstats |
| 5 |
import unittest |
| 6 |
import os |
| 7 |
from difflib import unified_diff |
| 8 |
from io import StringIO |
| 9 |
from test.support.os_helper import TESTFN, unlink, temp_dir, change_cwd |
| 10 |
from contextlib import contextmanager, redirect_stdout |
| 11 |
|
| 12 |
import profile |
| 13 |
from test.profilee import testfunc, timer |
| 14 |
from test.support.script_helper import assert_python_failure, assert_python_ok |
| 15 |
|
| 16 |
|
| 17 |
class ProfileTest(unittest.TestCase): |
| 18 |
|
| 19 |
profilerclass = profile.Profile |
| 20 |
profilermodule = profile |
| 21 |
methodnames = ['print_stats', 'print_callers', 'print_callees'] |
| 22 |
expected_max_output = ':0(max)' |
| 23 |
|
| 24 |
def tearDown(self): |
| 25 |
unlink(TESTFN) |
| 26 |
|
| 27 |
def get_expected_output(self): |
| 28 |
return _ProfileOutput |
| 29 |
|
| 30 |
@classmethod |
| 31 |
def do_profiling(cls): |
| 32 |
results = [] |
| 33 |
prof = cls.profilerclass(timer, 0.001) |
| 34 |
start_timer = timer() |
| 35 |
prof.runctx("testfunc()", globals(), locals()) |
| 36 |
results.append(timer() - start_timer) |
| 37 |
for methodname in cls.methodnames: |
| 38 |
s = StringIO() |
| 39 |
stats = pstats.Stats(prof, stream=s) |
| 40 |
stats.strip_dirs().sort_stats("stdname") |
| 41 |
getattr(stats, methodname)() |
| 42 |
output = s.getvalue().splitlines() |
| 43 |
mod_name = testfunc.__module__.rsplit('.', 1)[1] |
| 44 |
# Only compare against stats originating from the test file. |
| 45 |
# Prevents outside code (e.g., the io module) from causing |
| 46 |
# unexpected output. |
| 47 |
output = [line.rstrip() for line in output if mod_name in line] |
| 48 |
results.append('\n'.join(output)) |
| 49 |
return results |
| 50 |
|
| 51 |
def test_cprofile(self): |
| 52 |
results = self.do_profiling() |
| 53 |
expected = self.get_expected_output() |
| 54 |
self.assertEqual(results[0], 1000) |
| 55 |
fail = [] |
| 56 |
for i, method in enumerate(self.methodnames): |
| 57 |
a = expected[method] |
| 58 |
b = results[i+1] |
| 59 |
if a != b: |
| 60 |
fail.append(f"\nStats.{method} output for " |
| 61 |
f"{self.profilerclass.__name__} " |
| 62 |
"does not fit expectation:") |
| 63 |
fail.extend(unified_diff(a.split('\n'), b.split('\n'), |
| 64 |
lineterm="")) |
| 65 |
if fail: |
| 66 |
self.fail("\n".join(fail)) |
| 67 |
|
| 68 |
def test_calling_conventions(self): |
| 69 |
# Issue #5330: profile and cProfile wouldn't report C functions called |
| 70 |
# with keyword arguments. We test all calling conventions. |
| 71 |
stmts = [ |
| 72 |
"max([0])", |
| 73 |
"max([0], key=int)", |
| 74 |
"max([0], **dict(key=int))", |
| 75 |
"max(*([0],))", |
| 76 |
"max(*([0],), key=int)", |
| 77 |
"max(*([0],), **dict(key=int))", |
| 78 |
] |
| 79 |
for stmt in stmts: |
| 80 |
s = StringIO() |
| 81 |
prof = self.profilerclass(timer, 0.001) |
| 82 |
prof.runctx(stmt, globals(), locals()) |
| 83 |
stats = pstats.Stats(prof, stream=s) |
| 84 |
stats.print_stats() |
| 85 |
res = s.getvalue() |
| 86 |
self.assertIn(self.expected_max_output, res, |
| 87 |
"Profiling {0!r} didn't report max:\n{1}".format(stmt, res)) |
| 88 |
|
| 89 |
def test_run(self): |
| 90 |
with silent(): |
| 91 |
self.profilermodule.run("int('1')") |
| 92 |
self.profilermodule.run("int('1')", filename=TESTFN) |
| 93 |
self.assertTrue(os.path.exists(TESTFN)) |
| 94 |
|
| 95 |
def test_run_with_sort_by_values(self): |
| 96 |
with redirect_stdout(StringIO()) as f: |
| 97 |
self.profilermodule.run("int('1')", sort=('tottime', 'stdname')) |
| 98 |
self.assertIn("Ordered by: internal time, standard name", f.getvalue()) |
| 99 |
|
| 100 |
def test_runctx(self): |
| 101 |
with silent(): |
| 102 |
self.profilermodule.runctx("testfunc()", globals(), locals()) |
| 103 |
self.profilermodule.runctx("testfunc()", globals(), locals(), |
| 104 |
filename=TESTFN) |
| 105 |
self.assertTrue(os.path.exists(TESTFN)) |
| 106 |
|
| 107 |
def test_run_profile_as_module(self): |
| 108 |
# Test that -m switch needs an argument |
| 109 |
assert_python_failure('-m', self.profilermodule.__name__, '-m') |
| 110 |
|
| 111 |
# Test failure for not-existent module |
| 112 |
assert_python_failure('-m', self.profilermodule.__name__, |
| 113 |
'-m', 'random_module_xyz') |
| 114 |
|
| 115 |
# Test successful run |
| 116 |
assert_python_ok('-m', self.profilermodule.__name__, |
| 117 |
'-m', 'timeit', '-n', '1') |
| 118 |
|
| 119 |
def test_output_file_when_changing_directory(self): |
| 120 |
with temp_dir() as tmpdir, change_cwd(tmpdir): |
| 121 |
os.mkdir('dest') |
| 122 |
with open('demo.py', 'w', encoding="utf-8") as f: |
| 123 |
f.write('import os; os.chdir("dest")') |
| 124 |
|
| 125 |
assert_python_ok( |
| 126 |
'-m', self.profilermodule.__name__, |
| 127 |
'-o', 'out.pstats', |
| 128 |
'demo.py', |
| 129 |
) |
| 130 |
|
| 131 |
self.assertTrue(os.path.exists('out.pstats')) |
| 132 |
|
| 133 |
|
| 134 |
def regenerate_expected_output(filename, cls): |
| 135 |
filename = filename.rstrip('co') |
| 136 |
print('Regenerating %s...' % filename) |
| 137 |
results = cls.do_profiling() |
| 138 |
|
| 139 |
newfile = [] |
| 140 |
with open(filename, 'r') as f: |
| 141 |
for line in f: |
| 142 |
newfile.append(line) |
| 143 |
if line.startswith('#--cut'): |
| 144 |
break |
| 145 |
|
| 146 |
with open(filename, 'w') as f: |
| 147 |
f.writelines(newfile) |
| 148 |
f.write("_ProfileOutput = {}\n") |
| 149 |
for i, method in enumerate(cls.methodnames): |
| 150 |
f.write('_ProfileOutput[%r] = """\\\n%s"""\n' % ( |
| 151 |
method, results[i+1])) |
| 152 |
f.write('\nif __name__ == "__main__":\n main()\n') |
| 153 |
|
| 154 |
@contextmanager |
| 155 |
def silent(): |
| 156 |
stdout = sys.stdout |
| 157 |
try: |
| 158 |
sys.stdout = StringIO() |
| 159 |
yield |
| 160 |
finally: |
| 161 |
sys.stdout = stdout |
| 162 |
|
| 163 |
|
| 164 |
def main(): |
| 165 |
if '-r' not in sys.argv: |
| 166 |
unittest.main() |
| 167 |
else: |
| 168 |
regenerate_expected_output(__file__, ProfileTest) |
| 169 |
|
| 170 |
|
| 171 |
# Don't remove this comment. Everything below it is auto-generated. |
| 172 |
#--cut-------------------------------------------------------------------------- |
| 173 |
_ProfileOutput = {} |
| 174 |
_ProfileOutput['print_stats'] = """\ |
| 175 |
28 27.972 0.999 27.972 0.999 profilee.py:110(__getattr__) |
| 176 |
1 269.996 269.996 999.769 999.769 profilee.py:25(testfunc) |
| 177 |
23/3 149.937 6.519 169.917 56.639 profilee.py:35(factorial) |
| 178 |
20 19.980 0.999 19.980 0.999 profilee.py:48(mul) |
| 179 |
2 39.986 19.993 599.830 299.915 profilee.py:55(helper) |
| 180 |
4 115.984 28.996 119.964 29.991 profilee.py:73(helper1) |
| 181 |
2 -0.006 -0.003 139.946 69.973 profilee.py:84(helper2_indirect) |
| 182 |
8 311.976 38.997 399.912 49.989 profilee.py:88(helper2) |
| 183 |
8 63.976 7.997 79.960 9.995 profilee.py:98(subhelper)""" |
| 184 |
_ProfileOutput['print_callers'] = """\ |
| 185 |
:0(append) <- profilee.py:73(helper1)(4) 119.964 |
| 186 |
:0(exception) <- profilee.py:73(helper1)(4) 119.964 |
| 187 |
:0(hasattr) <- profilee.py:73(helper1)(4) 119.964 |
| 188 |
profilee.py:88(helper2)(8) 399.912 |
| 189 |
profilee.py:110(__getattr__) <- :0(hasattr)(12) 11.964 |
| 190 |
profilee.py:98(subhelper)(16) 79.960 |
| 191 |
profilee.py:25(testfunc) <- <string>:1(<module>)(1) 999.767 |
| 192 |
profilee.py:35(factorial) <- profilee.py:25(testfunc)(1) 999.769 |
| 193 |
profilee.py:35(factorial)(20) 169.917 |
| 194 |
profilee.py:84(helper2_indirect)(2) 139.946 |
| 195 |
profilee.py:48(mul) <- profilee.py:35(factorial)(20) 169.917 |
| 196 |
profilee.py:55(helper) <- profilee.py:25(testfunc)(2) 999.769 |
| 197 |
profilee.py:73(helper1) <- profilee.py:55(helper)(4) 599.830 |
| 198 |
profilee.py:84(helper2_indirect) <- profilee.py:55(helper)(2) 599.830 |
| 199 |
profilee.py:88(helper2) <- profilee.py:55(helper)(6) 599.830 |
| 200 |
profilee.py:84(helper2_indirect)(2) 139.946 |
| 201 |
profilee.py:98(subhelper) <- profilee.py:88(helper2)(8) 399.912""" |
| 202 |
_ProfileOutput['print_callees'] = """\ |
| 203 |
:0(hasattr) -> profilee.py:110(__getattr__)(12) 27.972 |
| 204 |
<string>:1(<module>) -> profilee.py:25(testfunc)(1) 999.769 |
| 205 |
profilee.py:110(__getattr__) -> |
| 206 |
profilee.py:25(testfunc) -> profilee.py:35(factorial)(1) 169.917 |
| 207 |
profilee.py:55(helper)(2) 599.830 |
| 208 |
profilee.py:35(factorial) -> profilee.py:35(factorial)(20) 169.917 |
| 209 |
profilee.py:48(mul)(20) 19.980 |
| 210 |
profilee.py:48(mul) -> |
| 211 |
profilee.py:55(helper) -> profilee.py:73(helper1)(4) 119.964 |
| 212 |
profilee.py:84(helper2_indirect)(2) 139.946 |
| 213 |
profilee.py:88(helper2)(6) 399.912 |
| 214 |
profilee.py:73(helper1) -> :0(append)(4) -0.004 |
| 215 |
profilee.py:84(helper2_indirect) -> profilee.py:35(factorial)(2) 169.917 |
| 216 |
profilee.py:88(helper2)(2) 399.912 |
| 217 |
profilee.py:88(helper2) -> :0(hasattr)(8) 11.964 |
| 218 |
profilee.py:98(subhelper)(8) 79.960 |
| 219 |
profilee.py:98(subhelper) -> profilee.py:110(__getattr__)(16) 27.972""" |
| 220 |
|
| 221 |
if __name__ == "__main__": |
| 222 |
main() |