Skip to content

Commit 28d22a3

Browse files
committed
benchmark: each project gets their own tmp dir
1 parent 74d6600 commit 28d22a3

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

benchmark/benchmark.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __exit__(
5959
self.foutput.close()
6060

6161
@contextlib.contextmanager
62-
def set_env(self, *env_varss: dict[str, str] | None) -> Iterator[None]:
62+
def set_env(self, *env_varss: Env_VarsType) -> Iterator[None]:
6363
"""Set environment variables.
6464
6565
All the arguments are dicts of name:value, or None. All are applied
@@ -116,12 +116,13 @@ def run_command(self, cmd: str) -> str:
116116
return output.strip()
117117

118118

119-
def rmrf(path: Path) -> None:
119+
def remake(path: Path) -> None:
120120
"""
121-
Remove a directory tree. It's OK if it doesn't exist.
121+
Remove a directory tree and recreate it. It's OK if it doesn't exist.
122122
"""
123123
if path.exists():
124124
shutil.rmtree(path)
125+
path.mkdir(parents=True)
125126

126127

127128
@contextlib.contextmanager
@@ -204,8 +205,9 @@ def shell(self) -> ShellSession:
204205

205206
def make_dir(self) -> None:
206207
self.dir = Path(f"work_{self.slug}")
207-
if self.dir.exists():
208-
rmrf(self.dir)
208+
remake(self.dir)
209+
self.tmpdir = Path(f"tmp_{self.slug}").resolve()
210+
remake(self.tmpdir)
209211

210212
def get_source(self, shell: ShellSession, retries: int = 5) -> None:
211213
"""Get the source of the project."""
@@ -277,11 +279,18 @@ def run_with_coverage(self, env: Env, cov_ver: Coverage) -> float:
277279
class ToxProject(ProjectToTest):
278280
"""A project using tox to run the test suite."""
279281

282+
ALLOWABLE_ENV_VARS = [
283+
"COVERAGE_DEBUG",
284+
"COVERAGE_CORE",
285+
"COVERAGE_FORCE_CONFIG",
286+
"PATH",
287+
"TMPDIR",
288+
]
289+
280290
env_vars: Env_VarsType = {
281291
**(ProjectToTest.env_vars or {}),
282292
# Allow some environment variables into the tox execution.
283-
"TOX_OVERRIDE": "testenv.pass_env+=COVERAGE_DEBUG,COVERAGE_CORE,COVERAGE_FORCE_CONFIG",
284-
"COVERAGE_DEBUG": "config,sys",
293+
"TOX_OVERRIDE": "testenv.pass_env+=" + ",".join(ALLOWABLE_ENV_VARS),
285294
}
286295

287296
def prep_environment(self, env: Env) -> None:
@@ -423,22 +432,19 @@ def __init__(self, more_pytest_args: str = ""):
423432

424433
def prep_environment(self, env: Env) -> None:
425434
env.shell.run_command(f"{env.python} -m pip install tox")
426-
Path("/tmp/operator_tmp").mkdir(exist_ok=True)
427435
env.shell.run_command(f"{env.python} -m tox -e unit --notest")
428436
env.shell.run_command(f"{env.python} -m tox -e unitnocov --notest")
429437

430438
def run_no_coverage(self, env: Env) -> float:
431439
env.shell.run_command(
432-
f"TMPDIR=/tmp/operator_tmp {env.python} -m tox -e unitnocov --skip-pkg-install"
433-
+ f" -- {self.more_pytest_args}"
440+
f"{env.python} -m tox -e unitnocov --skip-pkg-install -- {self.more_pytest_args}"
434441
)
435442
return env.shell.last_duration
436443

437444
def run_with_coverage(self, env: Env, cov_ver: Coverage) -> float:
438445
env.shell.run_command(f"{env.python} -m pip install {cov_ver.pip_args}")
439446
env.shell.run_command(
440-
f"TMPDIR=/tmp/operator_tmp {env.python} -m tox -e unit --skip-pkg-install"
441-
+ f" -- {self.more_pytest_args}"
447+
f"{env.python} -m tox -e unit --skip-pkg-install -- {self.more_pytest_args}"
442448
)
443449
duration = env.shell.last_duration
444450
report = env.shell.run_command(f"{env.python} -m coverage report --precision=6")
@@ -848,18 +854,17 @@ def __init__(
848854
py_versions: list[PyVersion],
849855
cov_versions: list[Coverage],
850856
projects: list[ProjectToTest],
851-
results_file: str = "results.json",
857+
results_file: Path = Path("results.json"),
852858
load: bool = False,
853-
cwd: str = "",
854859
):
855860
self.py_versions = py_versions
856861
self.cov_versions = cov_versions
857862
self.projects = projects
858-
self.results_file = Path(cwd) / Path(results_file)
859-
self.result_data: dict[ResultKey, list[float]] = (
860-
self.load_results() if load else {}
861-
)
863+
self.results_file = results_file
864+
self.result_data: dict[ResultKey, list[float]] = {}
862865
self.summary_data: dict[ResultKey, float] = {}
866+
if load:
867+
self.result_data = self.load_results()
863868

864869
def save_results(self) -> None:
865870
"""Save current results to the JSON file."""
@@ -933,8 +938,13 @@ def run(self, num_runs: int = 3) -> None:
933938
)
934939
print(banner)
935940
env.shell.print_banner(banner)
941+
env_vars = [
942+
proj.env_vars,
943+
cov_ver.env_vars,
944+
{"TMPDIR": str(proj.tmpdir)},
945+
]
936946
with change_dir(proj.dir):
937-
with env.shell.set_env(proj.env_vars, cov_ver.env_vars):
947+
with env.shell.set_env(*env_vars):
938948
try:
939949
if cov_ver.pip_args is None:
940950
dur = proj.run_no_coverage(env)
@@ -1056,16 +1066,16 @@ def run_experiment(
10561066
)
10571067

10581068
print(f"Removing and re-making {PERF_DIR}")
1059-
rmrf(PERF_DIR)
1069+
remake(PERF_DIR)
10601070

1061-
cwd = str(Path.cwd())
1071+
results_file = Path("results.json").resolve()
10621072
with change_dir(PERF_DIR):
10631073
exp = Experiment(
10641074
py_versions=py_versions,
10651075
cov_versions=cov_versions,
10661076
projects=projects,
1077+
results_file=results_file,
10671078
load=load,
1068-
cwd=cwd,
10691079
)
10701080
exp.run(num_runs=int(num_runs))
10711081
exp.show_results(rows=rows, column=column, ratios=ratios)

0 commit comments

Comments
 (0)