Skip to content

Commit 978e9b4

Browse files
committed
SQUISH -> misc/perf_compare.py -- Workaround inability to return Any type
1 parent 6fba903 commit 978e9b4

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

misc/perf_compare.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import time
3232
from collections.abc import Callable
3333
from concurrent.futures import ThreadPoolExecutor, as_completed
34-
from typing import Any
34+
from resource import struct_rusage as rusage
3535

3636

3737
def winsorized_paired_stats(
@@ -150,26 +150,36 @@ def run_benchmark(
150150
# Update a few files to force non-trivial incremental run
151151
edit_python_file(os.path.join(abschk, "mypy/__main__.py"))
152152
edit_python_file(os.path.join(abschk, "mypy/test/testcheck.py"))
153-
stopwatch_func: Callable[[], Any]
154-
delta_func: Callable[[Any, Any], Any]
153+
154+
def run() -> None:
155+
# Ignore errors, since some commits being measured may generate additional errors.
156+
if foreign:
157+
subprocess.run(cmd, cwd=check_dir, env=env)
158+
else:
159+
subprocess.run(cmd, cwd=compiled_dir, env=env)
160+
155161
if metric == "wall":
156-
stopwatch_func = lambda: time.time()
157-
delta_func = lambda t0, t1: t1 - t0
162+
stopwatch_func_w: Callable[[], float] = lambda: time.time()
163+
delta_func_w: Callable[[float, float], float] = lambda t0, t1: t1 - t0
164+
165+
v0_w = stopwatch_func_w() # capture
166+
run()
167+
v1_w = stopwatch_func_w() # capture
168+
return delta_func_w(v0_w, v1_w)
158169
elif metric == "cpu":
159-
# NOTE: CPU time (user+sys) is far less sensitive than wall-clock to
160-
# background interference
161-
stopwatch_func = lambda: resource.getrusage(resource.RUSAGE_CHILDREN)
162-
delta_func = lambda r0, r1: (r1.ru_utime - r0.ru_utime) + (r1.ru_stime - r0.ru_stime)
170+
stopwatch_func_c: Callable[[], rusage] = lambda: resource.getrusage(
171+
resource.RUSAGE_CHILDREN
172+
)
173+
delta_func_c: Callable[[rusage, rusage], float] = lambda r0, r1: (
174+
r1.ru_utime - r0.ru_utime
175+
) + (r1.ru_stime - r0.ru_stime)
176+
177+
v0_c = stopwatch_func_c() # capture
178+
run()
179+
v1_c = stopwatch_func_c() # capture
180+
return delta_func_c(v0_c, v1_c)
163181
else:
164182
raise AssertionError(f"Unrecognized metric: {metric!r}")
165-
v0 = stopwatch_func() # capture
166-
# Ignore errors, since some commits being measured may generate additional errors.
167-
if foreign:
168-
subprocess.run(cmd, cwd=check_dir, env=env)
169-
else:
170-
subprocess.run(cmd, cwd=compiled_dir, env=env)
171-
v1 = stopwatch_func() # capture
172-
return delta_func(v0, v1)
173183

174184

175185
def main() -> None:

0 commit comments

Comments
 (0)