Skip to content

Commit 9554e50

Browse files
committed
style(perf): blacken lab/benchmark.py
1 parent dc4f0c1 commit 9554e50

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

Diff for: lab/benchmark.py

+58-27
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ def prep_environment(self, env):
142142
"""
143143
pass
144144

145-
def tweak_coverage_settings(self, settings: Iterable[Tuple[str, Any]]) -> Iterator[None]:
145+
def tweak_coverage_settings(
146+
self, settings: Iterable[Tuple[str, Any]]
147+
) -> Iterator[None]:
146148
"""Tweak the coverage settings.
147149
148150
NOTE: This is not properly factored, and is only used by ToxProject now!!!
@@ -160,6 +162,7 @@ def run_with_coverage(self, env, pip_args, cov_tweaks):
160162

161163
class EmptyProject(ProjectToTest):
162164
"""A dummy project for testing other parts of this code."""
165+
163166
def __init__(self, slug: str = "empty", fake_durations: Iterable[float] = (1.23,)):
164167
self.slug = slug
165168
self.durations = iter(itertools.cycle(fake_durations))
@@ -193,9 +196,11 @@ def run_with_coverage(self, env, pip_args, cov_tweaks):
193196
f".tox/{env.pyver.toxenv}/bin/python -m pip install {pip_args}"
194197
)
195198
with self.tweak_coverage_settings(cov_tweaks):
196-
self.pre_check(env) # NOTE: Not properly factored, and only used from here.
199+
self.pre_check(env) # NOTE: Not properly factored, and only used from here.
197200
duration = self.run_tox(env, env.pyver.toxenv, "--skip-pkg-install")
198-
self.post_check(env) # NOTE: Not properly factored, and only used from here.
201+
self.post_check(
202+
env
203+
) # NOTE: Not properly factored, and only used from here.
199204
return duration
200205

201206

@@ -238,7 +243,9 @@ class ProjectAttrs(ToxProject):
238243

239244
git_url = "https://github.com/python-attrs/attrs"
240245

241-
def tweak_coverage_settings(self, tweaks: Iterable[Tuple[str, Any]]) -> Iterator[None]:
246+
def tweak_coverage_settings(
247+
self, tweaks: Iterable[Tuple[str, Any]]
248+
) -> Iterator[None]:
242249
return tweak_toml_coverage_settings("pyproject.toml", tweaks)
243250

244251
def pre_check(self, env):
@@ -248,7 +255,9 @@ def post_check(self, env):
248255
env.shell.run_command("ls -al")
249256

250257

251-
def tweak_toml_coverage_settings(toml_file: str, tweaks: Iterable[Tuple[str, Any]]) -> Iterator[None]:
258+
def tweak_toml_coverage_settings(
259+
toml_file: str, tweaks: Iterable[Tuple[str, Any]]
260+
) -> Iterator[None]:
252261
if tweaks:
253262
toml_inserts = []
254263
for name, value in tweaks:
@@ -265,8 +274,6 @@ def tweak_toml_coverage_settings(toml_file: str, tweaks: Iterable[Tuple[str, Any
265274
return file_replace(Path(toml_file), header, insert)
266275

267276

268-
269-
270277
class AdHocProject(ProjectToTest):
271278
"""A standalone program to run locally."""
272279

@@ -295,9 +302,7 @@ def run_no_coverage(self, env):
295302
def run_with_coverage(self, env, pip_args, cov_tweaks):
296303
env.shell.run_command(f"{env.python} -m pip install {pip_args}")
297304
with change_dir(self.cur_dir):
298-
env.shell.run_command(
299-
f"{env.python} -m coverage run {self.python_file}"
300-
)
305+
env.shell.run_command(f"{env.python} -m coverage run {self.python_file}")
301306
return env.shell.last_duration
302307

303308

@@ -308,13 +313,15 @@ class SlipcoverBenchmark(AdHocProject):
308313
Clone https://github.com/plasma-umass/slipcover to /src/slipcover
309314
310315
"""
316+
311317
def __init__(self, python_file):
312318
super().__init__(
313319
python_file=f"/src/slipcover/benchmarks/{python_file}",
314320
cur_dir="/src/slipcover",
315321
pip_args="six pyperf",
316322
)
317323

324+
318325
class PyVersion:
319326
"""A version of Python to use."""
320327

@@ -325,22 +332,26 @@ class PyVersion:
325332
# The tox environment to run this Python
326333
toxenv: str
327334

335+
328336
class Python(PyVersion):
329337
"""A version of CPython to use."""
330338

331339
def __init__(self, major, minor):
332340
self.command = self.slug = f"python{major}.{minor}"
333341
self.toxenv = f"py{major}{minor}"
334342

343+
335344
class PyPy(PyVersion):
336345
"""A version of PyPy to use."""
337346

338347
def __init__(self, major, minor):
339348
self.command = self.slug = f"pypy{major}.{minor}"
340349
self.toxenv = f"pypy{major}{minor}"
341350

351+
342352
class AdHocPython(PyVersion):
343353
"""A custom build of Python to use."""
354+
344355
def __init__(self, path, slug):
345356
self.command = f"{path}/bin/python3"
346357
self.slug = slug
@@ -350,33 +361,40 @@ def __init__(self, path, slug):
350361
@dataclasses.dataclass
351362
class Coverage:
352363
"""A version of coverage.py to use, maybe None."""
364+
353365
# Short word for messages, directories, etc
354366
slug: str
355367
# Arguments for "pip install ..."
356368
pip_args: Optional[str] = None
357369
# Tweaks to the .coveragerc file
358370
tweaks: Optional[Iterable[Tuple[str, Any]]] = None
359371

372+
360373
class CoveragePR(Coverage):
361374
"""A version of coverage.py from a pull request."""
375+
362376
def __init__(self, number, tweaks=None):
363377
super().__init__(
364378
slug=f"#{number}",
365379
pip_args=f"git+https://github.com/nedbat/coveragepy.git@refs/pull/{number}/merge",
366380
tweaks=tweaks,
367381
)
368382

383+
369384
class CoverageCommit(Coverage):
370385
"""A version of coverage.py from a specific commit."""
386+
371387
def __init__(self, sha, tweaks=None):
372388
super().__init__(
373389
slug=sha,
374390
pip_args=f"git+https://github.com/nedbat/coveragepy.git@{sha}",
375391
tweaks=tweaks,
376392
)
377393

394+
378395
class CoverageSource(Coverage):
379396
"""The coverage.py in a working tree."""
397+
380398
def __init__(self, directory, tweaks=None):
381399
super().__init__(
382400
slug="source",
@@ -398,6 +416,7 @@ class Env:
398416

399417
DIMENSION_NAMES = ["proj", "pyver", "cov"]
400418

419+
401420
class Experiment:
402421
"""A particular time experiment to run."""
403422

@@ -415,10 +434,10 @@ def __init__(
415434
def run(self, num_runs: int = 3) -> None:
416435
results = []
417436
total_runs = (
418-
len(self.projects) *
419-
len(self.py_versions) *
420-
len(self.cov_versions) *
421-
num_runs
437+
len(self.projects)
438+
* len(self.py_versions)
439+
* len(self.cov_versions)
440+
* num_runs
422441
)
423442
total_run_nums = iter(itertools.count(start=1))
424443

@@ -444,15 +463,17 @@ def run(self, num_runs: int = 3) -> None:
444463
for run_num in range(num_runs):
445464
total_run_num = next(total_run_nums)
446465
print(
447-
f"Running tests, cov={cov_ver.slug}, " +
448-
f"{run_num+1} of {num_runs}, " +
449-
f"total {total_run_num}/{total_runs}"
466+
f"Running tests, cov={cov_ver.slug}, "
467+
+ f"{run_num+1} of {num_runs}, "
468+
+ f"total {total_run_num}/{total_runs}"
450469
)
451470
if cov_ver.pip_args is None:
452471
dur = proj.run_no_coverage(env)
453472
else:
454473
dur = proj.run_with_coverage(
455-
env, cov_ver.pip_args, cov_ver.tweaks,
474+
env,
475+
cov_ver.pip_args,
476+
cov_ver.tweaks,
456477
)
457478
print(f"Tests took {dur:.3f}s")
458479
durations.append(dur)
@@ -487,6 +508,7 @@ def show_results(
487508
remap = [data_order.index(datum) for datum in DIMENSION_NAMES]
488509

489510
WIDTH = 20
511+
490512
def as_table_row(vals):
491513
return "| " + " | ".join(v.ljust(WIDTH) for v in vals) + " |"
492514

@@ -506,7 +528,7 @@ def as_table_row(vals):
506528
for col in dimensions[column]:
507529
key = (*tup, col)
508530
key = tuple(key[i] for i in remap)
509-
result_time = self.result_data[key] # type: ignore
531+
result_time = self.result_data[key] # type: ignore
510532
row.append(f"{result_time:.3f} s")
511533
col_data[col] = result_time
512534
for _, num, denom in ratios:
@@ -517,6 +539,7 @@ def as_table_row(vals):
517539

518540
PERF_DIR = Path("/tmp/covperf")
519541

542+
520543
def run_experiment(
521544
py_versions: List[PyVersion],
522545
cov_versions: List[Coverage],
@@ -534,21 +557,25 @@ def run_experiment(
534557
if any(rslug not in slugs for rslug in ratio_slugs):
535558
raise Exception(f"Ratio slug doesn't match a slug: {ratio_slugs}, {slugs}")
536559
if set(rows + [column]) != set(DIMENSION_NAMES):
537-
raise Exception(f"All of these must be in rows or column: {', '.join(DIMENSION_NAMES)}")
560+
raise Exception(
561+
f"All of these must be in rows or column: {', '.join(DIMENSION_NAMES)}"
562+
)
538563

539564
print(f"Removing and re-making {PERF_DIR}")
540565
rmrf(PERF_DIR)
541566

542567
with change_dir(PERF_DIR):
543-
exp = Experiment(py_versions=py_versions, cov_versions=cov_versions, projects=projects)
568+
exp = Experiment(
569+
py_versions=py_versions, cov_versions=cov_versions, projects=projects
570+
)
544571
exp.run(num_runs=int(sys.argv[1]))
545572
exp.show_results(rows=rows, column=column, ratios=ratios)
546573

547574

548575
if 0:
549576
run_experiment(
550577
py_versions=[
551-
#Python(3, 11),
578+
# Python(3, 11),
552579
AdHocPython("/usr/local/cpython/v3.10.5", "v3.10.5"),
553580
AdHocPython("/usr/local/cpython/v3.11.0b3", "v3.11.0b3"),
554581
AdHocPython("/usr/local/cpython/94231", "94231"),
@@ -578,14 +605,18 @@ def run_experiment(
578605
],
579606
cov_versions=[
580607
Coverage("701", "coverage==7.0.1"),
581-
Coverage("701.dynctx", "coverage==7.0.1", [("dynamic_context", "test_function")]),
608+
Coverage(
609+
"701.dynctx", "coverage==7.0.1", [("dynamic_context", "test_function")]
610+
),
582611
Coverage("702", "coverage==7.0.2"),
583-
Coverage("702.dynctx", "coverage==7.0.2", [("dynamic_context", "test_function")]),
612+
Coverage(
613+
"702.dynctx", "coverage==7.0.2", [("dynamic_context", "test_function")]
614+
),
584615
],
585616
projects=[
586-
#EmptyProject("empty", [1.2, 3.4]),
587-
#EmptyProject("dummy", [6.9, 7.1]),
588-
#ProjectDateutil(),
617+
# EmptyProject("empty", [1.2, 3.4]),
618+
# EmptyProject("dummy", [6.9, 7.1]),
619+
# ProjectDateutil(),
589620
ProjectAttrs(),
590621
],
591622
rows=["proj", "pyver"],

0 commit comments

Comments
 (0)