|
31 | 31 | import time |
32 | 32 | from collections.abc import Callable |
33 | 33 | from concurrent.futures import ThreadPoolExecutor, as_completed |
34 | | -from typing import Any |
| 34 | +from resource import struct_rusage as rusage |
35 | 35 |
|
36 | 36 |
|
37 | 37 | def winsorized_paired_stats( |
@@ -150,26 +150,36 @@ def run_benchmark( |
150 | 150 | # Update a few files to force non-trivial incremental run |
151 | 151 | edit_python_file(os.path.join(abschk, "mypy/__main__.py")) |
152 | 152 | 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 | + |
155 | 161 | 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) |
158 | 169 | 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) |
163 | 181 | else: |
164 | 182 | 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) |
173 | 183 |
|
174 | 184 |
|
175 | 185 | def main() -> None: |
|
0 commit comments