1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
import os
import re
import subprocess
import sys
import textwrap
from pathlib import Path
import pytest
from .util import BUSY_WAIT_SCRIPT
EXECUTION_DETAILS_SCRIPT = f"""
#!{sys.executable}
import sys, os
print('__name__', __name__, file=sys.stderr)
print('sys.argv', sys.argv, file=sys.stderr)
print('sys.executable', os.path.realpath(sys.executable), file=sys.stderr)
print('os.getcwd()', os.getcwd(), file=sys.stderr)
""".strip()
@pytest.mark.parametrize(
"pyinstrument_invocation",
(["pyinstrument"], [sys.executable, "-m", "pyinstrument"]),
)
class TestCommandLine:
@pytest.fixture(autouse=True)
def _suppress_warnings(self, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setenv("PYINSTRUMENT_IGNORE_OVERHEAD_WARNING", "1")
def test_command_line(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
# need to wrap Paths with str() due to CPython bug 33617 (fixed in Python 3.8)
output = subprocess.check_output([*pyinstrument_invocation, str(busy_wait_py)])
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_module_running(self, pyinstrument_invocation, tmp_path: Path):
(tmp_path / "busy_wait_module").mkdir()
(tmp_path / "busy_wait_module" / "__init__.py").touch()
(tmp_path / "busy_wait_module" / "__main__.py").write_text(BUSY_WAIT_SCRIPT)
output = subprocess.check_output(
[*pyinstrument_invocation, "-m", "busy_wait_module"], cwd=tmp_path
)
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_single_file_module_running(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
output = subprocess.check_output(
[*pyinstrument_invocation, "-m", "busy_wait"], cwd=tmp_path
)
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_running_yourself_as_module(self, pyinstrument_invocation):
subprocess.check_call(
[*pyinstrument_invocation, "-m", "pyinstrument", "--help"],
)
def test_path(self, pyinstrument_invocation, tmp_path: Path, monkeypatch):
if sys.platform == "win32":
pytest.skip("--from-path is not supported on Windows")
program_path = tmp_path / "pyi_test_program"
program_path.write_text(BUSY_WAIT_SCRIPT)
program_path.chmod(0x755)
monkeypatch.setenv("PATH", str(tmp_path), prepend=os.pathsep)
subprocess.check_call(
[*pyinstrument_invocation, "--from-path", "--", "pyi_test_program"],
)
def test_program_passed_as_string(self, pyinstrument_invocation, tmp_path: Path):
# check the program actually runs
output_file = tmp_path / "output.txt"
output = subprocess.check_output(
[
*pyinstrument_invocation,
"-c",
textwrap.dedent(
f"""
import sys
from pathlib import Path
output_file = Path(sys.argv[1])
output_file.write_text("Hello World")
print("Finished.")
"""
),
str(output_file),
],
)
assert "Finished." in str(output)
assert output_file.read_text() == "Hello World"
# check the output
output = subprocess.check_output([*pyinstrument_invocation, "-c", BUSY_WAIT_SCRIPT])
print(output.decode("utf-8"))
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_script_execution_details(self, pyinstrument_invocation, tmp_path: Path):
program_path = tmp_path / "program.py"
program_path.write_text(EXECUTION_DETAILS_SCRIPT)
process_pyi = subprocess.run(
[*pyinstrument_invocation, str(program_path), "arg1", "arg2"],
stderr=subprocess.PIPE,
check=True,
text=True,
)
process_native = subprocess.run(
[sys.executable, str(program_path), "arg1", "arg2"],
stderr=subprocess.PIPE,
check=True,
text=True,
)
print("process_pyi.stderr", process_pyi.stderr)
print("process_native.stderr", process_native.stderr)
assert process_pyi.stderr == process_native.stderr
def test_module_execution_details(self, pyinstrument_invocation, tmp_path: Path):
(tmp_path / "test_module").mkdir()
(tmp_path / "test_module" / "__init__.py").touch()
(tmp_path / "test_module" / "__main__.py").write_text(EXECUTION_DETAILS_SCRIPT)
process_pyi = subprocess.run(
[*pyinstrument_invocation, "-m", "test_module", "arg1", "arg2"],
stderr=subprocess.PIPE,
check=True,
cwd=tmp_path,
text=True,
)
process_native = subprocess.run(
[sys.executable, "-m", "test_module", "arg1", "arg2"],
stderr=subprocess.PIPE,
check=True,
cwd=tmp_path,
text=True,
)
print("process_pyi.stderr", process_pyi.stderr)
print("process_native.stderr", process_native.stderr)
assert process_native.stderr
assert process_pyi.stderr == process_native.stderr
def test_path_execution_details(self, pyinstrument_invocation, tmp_path: Path, monkeypatch):
if sys.platform == "win32":
pytest.skip("--from-path is not supported on Windows")
program_path = tmp_path / "pyi_test_program"
program_path.write_text(EXECUTION_DETAILS_SCRIPT)
program_path.chmod(0x755)
monkeypatch.setenv("PATH", str(tmp_path), prepend=os.pathsep)
process_pyi = subprocess.run(
[
*pyinstrument_invocation,
"--from-path",
"--",
"pyi_test_program",
"arg1",
"arg2",
],
stderr=subprocess.PIPE,
check=True,
text=True,
)
process_native = subprocess.run(
["pyi_test_program", "arg1", "arg2"],
stderr=subprocess.PIPE,
check=True,
text=True,
)
print("process_pyi.stderr", process_pyi.stderr)
print("process_native.stderr", process_native.stderr)
assert process_pyi.stderr == process_native.stderr
def test_program_passed_as_string_execution_details(
self, pyinstrument_invocation, tmp_path: Path
):
process_pyi = subprocess.run(
[*pyinstrument_invocation, "-c", EXECUTION_DETAILS_SCRIPT],
stderr=subprocess.PIPE,
check=True,
text=True,
)
process_native = subprocess.run(
[sys.executable, "-c", EXECUTION_DETAILS_SCRIPT],
stderr=subprocess.PIPE,
check=True,
text=True,
)
print("process_pyi.stderr", process_pyi.stderr)
print("process_native.stderr", process_native.stderr)
assert process_native.stderr
assert process_pyi.stderr == process_native.stderr
def test_session_save_and_load(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
session_file = tmp_path / "session.pyisession"
subprocess.check_call(
[
*pyinstrument_invocation,
"--renderer=session",
f"--outfile={session_file}",
str(busy_wait_py),
]
)
# check it's a valid Session file
from pyinstrument.session import Session
Session.load(session_file)
# run pyinstrument again to render the output
output = subprocess.check_output([*pyinstrument_invocation, f"--load={session_file}"])
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_interval(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
output = subprocess.check_output(
[
*pyinstrument_invocation,
"--interval",
"0.002",
str(busy_wait_py),
]
)
assert "busy_wait" in str(output)
assert "do_nothing" in str(output)
def test_invocation_machinery_is_trimmed(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
output = subprocess.check_output(
[
*pyinstrument_invocation,
"--show-all",
str(busy_wait_py),
],
universal_newlines=True,
)
print("Output:")
print(output)
first_profiling_line = re.search(r"^\d+(\.\d+)?\s+([^\s]+)\s+(.*)", output, re.MULTILINE)
assert first_profiling_line
function_name = first_profiling_line.group(2)
location = first_profiling_line.group(3)
assert function_name == "<module>"
assert "busy_wait.py" in location
def test_binary_output(self, pyinstrument_invocation, tmp_path: Path):
busy_wait_py = tmp_path / "busy_wait.py"
busy_wait_py.write_text(BUSY_WAIT_SCRIPT)
output_file = tmp_path / "output.pstats"
subprocess.check_call(
[
*pyinstrument_invocation,
"--renderer=pstats",
f"--outfile={output_file}",
str(busy_wait_py),
],
universal_newlines=True,
)
assert output_file.exists()
# check it can be loaded
import pstats
stats = pstats.Stats(str(output_file))
assert stats
def test_program_exit_code(self, pyinstrument_invocation, tmp_path: Path):
exit_1_py = tmp_path / "exit_1.py"
exit_1_py.write_text("""import sys; sys.exit(1)""")
retcode = subprocess.call(
[
*pyinstrument_invocation,
str(exit_1_py),
],
)
assert retcode == 1
|