Skip to content

Commit cbfb0cc

Browse files
authored
Make bm_runner location agnostic and include debugging. (#5247)
1 parent 3e2cfd4 commit cbfb0cc

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

benchmarks/bm_runner.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,26 @@
2323
# for more.
2424
COMPARE_FACTOR = 1.2
2525

26+
BENCHMARKS_DIR = Path(__file__).parent
2627

2728
# Common ASV arguments for all run_types except `custom`.
2829
ASV_HARNESS = (
29-
"asv run {posargs} --attribute rounds=4 --interleave-rounds --strict "
30+
"run {posargs} --attribute rounds=4 --interleave-rounds --strict "
3031
"--show-stderr"
3132
)
3233

3334

35+
def _subprocess_run_print(args, **kwargs):
36+
print(f"BM_RUNNER DEBUG: {' '.join(args)}")
37+
return subprocess.run(args, **kwargs)
38+
39+
40+
def _subprocess_run_asv(args, **kwargs):
41+
args.insert(0, "asv")
42+
kwargs["cwd"] = BENCHMARKS_DIR
43+
return _subprocess_run_print(args, **kwargs)
44+
45+
3446
def _check_requirements(package: str) -> None:
3547
try:
3648
import_module(package)
@@ -47,7 +59,7 @@ def _prep_data_gen_env() -> None:
4759
Create/access a separate, unchanging environment for generating test data.
4860
"""
4961

50-
root_dir = Path(__file__).parents[1]
62+
root_dir = BENCHMARKS_DIR.parent
5163
python_version = "3.10"
5264
data_gen_var = "DATA_GEN_PYTHON"
5365
if data_gen_var in environ:
@@ -56,7 +68,7 @@ def _prep_data_gen_env() -> None:
5668
print("Setting up the data generation environment ...")
5769
# Get Nox to build an environment for the `tests` session, but don't
5870
# run the session. Will re-use a cached environment if appropriate.
59-
subprocess.run(
71+
_subprocess_run_print(
6072
[
6173
"nox",
6274
f"--noxfile={root_dir / 'noxfile.py'}",
@@ -75,15 +87,15 @@ def _prep_data_gen_env() -> None:
7587
print("Installing Mule into data generation environment ...")
7688
mule_dir = data_gen_python.parents[1] / "resources" / "mule"
7789
if not mule_dir.is_dir():
78-
subprocess.run(
90+
_subprocess_run_print(
7991
[
8092
"git",
8193
"clone",
8294
"https://github.com/metomi/mule.git",
8395
str(mule_dir),
8496
]
8597
)
86-
subprocess.run(
98+
_subprocess_run_print(
8799
[
88100
str(data_gen_python),
89101
"-m",
@@ -103,28 +115,28 @@ def _setup_common() -> None:
103115
_prep_data_gen_env()
104116

105117
print("Setting up ASV ...")
106-
subprocess.run(["asv", "machine", "--yes"])
118+
_subprocess_run_asv(["machine", "--yes"])
107119

108120
print("Setup complete.")
109121

110122

111123
def _asv_compare(*commits: str, overnight_mode: bool = False) -> None:
112124
"""Run through a list of commits comparing each one to the next."""
113125
commits = [commit[:8] for commit in commits]
114-
shifts_dir = Path(".asv") / "performance-shifts"
126+
shifts_dir = BENCHMARKS_DIR / ".asv" / "performance-shifts"
115127
for i in range(len(commits) - 1):
116128
before = commits[i]
117129
after = commits[i + 1]
118130
asv_command = (
119-
f"asv compare {before} {after} --factor={COMPARE_FACTOR} --split"
131+
f"compare {before} {after} --factor={COMPARE_FACTOR} --split"
120132
)
121-
subprocess.run(asv_command.split(" "))
133+
_subprocess_run_asv(asv_command.split(" "))
122134

123135
if overnight_mode:
124136
# Record performance shifts.
125137
# Run the command again but limited to only showing performance
126138
# shifts.
127-
shifts = subprocess.run(
139+
shifts = _subprocess_run_asv(
128140
[*asv_command.split(" "), "--only-changed"],
129141
capture_output=True,
130142
text=True,
@@ -207,11 +219,11 @@ def func(args: argparse.Namespace) -> None:
207219

208220
commit_range = f"{args.first_commit}^^.."
209221
asv_command = ASV_HARNESS.format(posargs=commit_range)
210-
subprocess.run([*asv_command.split(" "), *args.asv_args])
222+
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
211223

212224
# git rev-list --first-parent is the command ASV uses.
213225
git_command = f"git rev-list --first-parent {commit_range}"
214-
commit_string = subprocess.run(
226+
commit_string = _subprocess_run_print(
215227
git_command.split(" "), capture_output=True, text=True
216228
).stdout
217229
commit_list = commit_string.rstrip().split("\n")
@@ -246,7 +258,7 @@ def func(args: argparse.Namespace) -> None:
246258
_setup_common()
247259

248260
git_command = f"git merge-base HEAD {args.base_branch}"
249-
merge_base = subprocess.run(
261+
merge_base = _subprocess_run_print(
250262
git_command.split(" "), capture_output=True, text=True
251263
).stdout[:8]
252264

@@ -255,7 +267,7 @@ def func(args: argparse.Namespace) -> None:
255267
hashfile.flush()
256268
commit_range = f"HASHFILE:{hashfile.name}"
257269
asv_command = ASV_HARNESS.format(posargs=commit_range)
258-
subprocess.run([*asv_command.split(" "), *args.asv_args])
270+
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
259271

260272
_asv_compare(merge_base, "HEAD")
261273

@@ -312,13 +324,13 @@ def csperf(
312324
asv_command = asv_command.replace(" --strict", "")
313325
# Only do a single round.
314326
asv_command = re.sub(r"rounds=\d", "rounds=1", asv_command)
315-
subprocess.run([*asv_command.split(" "), *args.asv_args])
327+
_subprocess_run_asv([*asv_command.split(" "), *args.asv_args])
316328

317-
asv_command = f"asv publish {commit_range} --html-dir={publish_subdir}"
318-
subprocess.run(asv_command.split(" "))
329+
asv_command = f"publish {commit_range} --html-dir={publish_subdir}"
330+
_subprocess_run_asv(asv_command.split(" "))
319331

320332
# Print completion message.
321-
location = Path().cwd() / ".asv"
333+
location = BENCHMARKS_DIR / ".asv"
322334
print(
323335
f'New ASV results for "{run_type}".\n'
324336
f'See "{publish_subdir}",'
@@ -366,7 +378,7 @@ def add_arguments(self) -> None:
366378
@staticmethod
367379
def func(args: argparse.Namespace) -> None:
368380
_setup_common()
369-
subprocess.run(["asv", args.asv_sub_command, *args.asv_args])
381+
_subprocess_run_asv([args.asv_sub_command, *args.asv_args])
370382

371383

372384
def main():

0 commit comments

Comments
 (0)