Skip to content

Commit 1bd56d5

Browse files
authored
Improve benchmark runner printing (#5429)
* More sensible print and run functions. * Avoid permanent modifications in _subprocess_runner.
1 parent 299b335 commit 1bd56d5

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

benchmarks/bm_runner.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,26 @@
3232
)
3333

3434

35-
def _subprocess_run_print(args, **kwargs):
35+
def echo(echo_string: str):
3636
# Use subprocess for printing to reduce chance of printing out of sequence
3737
# with the subsequent calls.
38-
subprocess.run(["echo", f"BM_RUNNER DEBUG: {' '.join(args)}"])
38+
subprocess.run(["echo", f"BM_RUNNER DEBUG: {echo_string}"])
39+
40+
41+
def _subprocess_runner(args, asv=False, **kwargs):
42+
# Avoid permanent modifications if the same arguments are used more than once.
43+
args = args.copy()
44+
kwargs = kwargs.copy()
45+
if asv:
46+
args.insert(0, "asv")
47+
kwargs["cwd"] = BENCHMARKS_DIR
48+
echo(" ".join(args))
3949
return subprocess.run(args, **kwargs)
4050

4151

42-
def _subprocess_run_asv(args, **kwargs):
43-
args.insert(0, "asv")
44-
kwargs["cwd"] = BENCHMARKS_DIR
45-
return _subprocess_run_print(args, **kwargs)
52+
def _subprocess_runner_capture(args, **kwargs) -> str:
53+
result = _subprocess_runner(args, capture_output=True, **kwargs)
54+
return result.stdout.decode()
4655

4756

4857
def _check_requirements(package: str) -> None:
@@ -65,12 +74,12 @@ def _prep_data_gen_env() -> None:
6574
python_version = "3.11"
6675
data_gen_var = "DATA_GEN_PYTHON"
6776
if data_gen_var in environ:
68-
print("Using existing data generation environment.")
77+
echo("Using existing data generation environment.")
6978
else:
70-
print("Setting up the data generation environment ...")
79+
echo("Setting up the data generation environment ...")
7180
# Get Nox to build an environment for the `tests` session, but don't
7281
# run the session. Will re-use a cached environment if appropriate.
73-
_subprocess_run_print(
82+
_subprocess_runner(
7483
[
7584
"nox",
7685
f"--noxfile={root_dir / 'noxfile.py'}",
@@ -86,18 +95,18 @@ def _prep_data_gen_env() -> None:
8695
).resolve()
8796
environ[data_gen_var] = str(data_gen_python)
8897

89-
print("Installing Mule into data generation environment ...")
98+
echo("Installing Mule into data generation environment ...")
9099
mule_dir = data_gen_python.parents[1] / "resources" / "mule"
91100
if not mule_dir.is_dir():
92-
_subprocess_run_print(
101+
_subprocess_runner(
93102
[
94103
"git",
95104
"clone",
96105
"https://github.com/metomi/mule.git",
97106
str(mule_dir),
98107
]
99108
)
100-
_subprocess_run_print(
109+
_subprocess_runner(
101110
[
102111
str(data_gen_python),
103112
"-m",
@@ -107,7 +116,7 @@ def _prep_data_gen_env() -> None:
107116
]
108117
)
109118

110-
print("Data generation environment ready.")
119+
echo("Data generation environment ready.")
111120

112121

113122
def _setup_common() -> None:
@@ -116,10 +125,10 @@ def _setup_common() -> None:
116125

117126
_prep_data_gen_env()
118127

119-
print("Setting up ASV ...")
120-
_subprocess_run_asv(["machine", "--yes"])
128+
echo("Setting up ASV ...")
129+
_subprocess_runner(["machine", "--yes"], asv=True)
121130

122-
print("Setup complete.")
131+
echo("Setup complete.")
123132

124133

125134
def _asv_compare(*commits: str, overnight_mode: bool = False) -> None:
@@ -132,17 +141,15 @@ def _asv_compare(*commits: str, overnight_mode: bool = False) -> None:
132141
asv_command = (
133142
f"compare {before} {after} --factor={COMPARE_FACTOR} --split"
134143
)
135-
_subprocess_run_asv(asv_command.split(" "))
144+
_subprocess_runner(asv_command.split(" "), asv=True)
136145

137146
if overnight_mode:
138147
# Record performance shifts.
139148
# Run the command again but limited to only showing performance
140149
# shifts.
141-
shifts = _subprocess_run_asv(
142-
[*asv_command.split(" "), "--only-changed"],
143-
capture_output=True,
144-
text=True,
145-
).stdout
150+
shifts = _subprocess_runner_capture(
151+
[*asv_command.split(" "), "--only-changed"], asv=True
152+
)
146153
if shifts:
147154
# Write the shifts report to a file.
148155
# Dir is used by .github/workflows/benchmarks.yml,
@@ -221,13 +228,11 @@ def func(args: argparse.Namespace) -> None:
221228

222229
commit_range = f"{args.first_commit}^^.."
223230
asv_command = ASV_HARNESS.format(posargs=commit_range)
224-
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
231+
_subprocess_runner([*asv_command.split(" "), *args.asv_args], asv=True)
225232

226233
# git rev-list --first-parent is the command ASV uses.
227234
git_command = f"git rev-list --first-parent {commit_range}"
228-
commit_string = _subprocess_run_print(
229-
git_command.split(" "), capture_output=True, text=True
230-
).stdout
235+
commit_string = _subprocess_runner_capture(git_command.split(" "))
231236
commit_list = commit_string.rstrip().split("\n")
232237
_asv_compare(*reversed(commit_list), overnight_mode=True)
233238

@@ -260,16 +265,16 @@ def func(args: argparse.Namespace) -> None:
260265
_setup_common()
261266

262267
git_command = f"git merge-base HEAD {args.base_branch}"
263-
merge_base = _subprocess_run_print(
264-
git_command.split(" "), capture_output=True, text=True
265-
).stdout[:8]
268+
merge_base = _subprocess_runner_capture(git_command.split(" "))[:8]
266269

267270
with NamedTemporaryFile("w") as hashfile:
268271
hashfile.writelines([merge_base, "\n", "HEAD"])
269272
hashfile.flush()
270273
commit_range = f"HASHFILE:{hashfile.name}"
271274
asv_command = ASV_HARNESS.format(posargs=commit_range)
272-
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
275+
_subprocess_runner(
276+
[*asv_command.split(" "), *args.asv_args], asv=True
277+
)
273278

274279
_asv_compare(merge_base, "HEAD")
275280

@@ -326,14 +331,14 @@ def csperf(
326331
asv_command = asv_command.replace(" --strict", "")
327332
# Only do a single round.
328333
asv_command = re.sub(r"rounds=\d", "rounds=1", asv_command)
329-
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
334+
_subprocess_runner([*asv_command.split(" "), *args.asv_args], asv=True)
330335

331336
asv_command = f"publish {commit_range} --html-dir={publish_subdir}"
332-
_subprocess_run_asv(asv_command.split(" "))
337+
_subprocess_runner(asv_command.split(" "), asv=True)
333338

334339
# Print completion message.
335340
location = BENCHMARKS_DIR / ".asv"
336-
print(
341+
echo(
337342
f'New ASV results for "{run_type}".\n'
338343
f'See "{publish_subdir}",'
339344
f'\n or JSON files under "{location / "results"}".'
@@ -380,7 +385,7 @@ def add_arguments(self) -> None:
380385
@staticmethod
381386
def func(args: argparse.Namespace) -> None:
382387
_setup_common()
383-
_subprocess_run_asv([args.asv_sub_command, *args.asv_args])
388+
_subprocess_runner([args.asv_sub_command, *args.asv_args], asv=True)
384389

385390

386391
def main():

0 commit comments

Comments
 (0)