-
Notifications
You must be signed in to change notification settings - Fork 222
/
test_cmdline.py
224 lines (180 loc) 路 7.45 KB
/
test_cmdline.py
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
import os
import re
import subprocess
import sys
from pathlib import Path
import pytest
from .util import BUSY_WAIT_SCRIPT
# this script just does a busywait for 0.25 seconds.
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.path', sys.path, 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:
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"],
)
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_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_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