Skip to content

Commit 46c7ec7

Browse files
authored
Support measuring incremental runs in perf_compare script (#18289)
Use `--incremental` to measure incremental instead of full self checks. The warmup runs are used to populate incremental caches.
1 parent 52888ae commit 46c7ec7

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

misc/perf_compare.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,17 @@ def clone(target_dir: str, commit: str | None) -> None:
5555
subprocess.run(["git", "checkout", commit], check=True, cwd=target_dir)
5656

5757

58-
def run_benchmark(compiled_dir: str, check_dir: str) -> float:
58+
def edit_python_file(fnam: str) -> None:
59+
with open(fnam) as f:
60+
data = f.read()
61+
data += "\n#"
62+
with open(fnam, "w") as f:
63+
f.write(data)
64+
65+
66+
def run_benchmark(compiled_dir: str, check_dir: str, *, incremental: bool) -> float:
5967
cache_dir = os.path.join(compiled_dir, ".mypy_cache")
60-
if os.path.isdir(cache_dir):
68+
if os.path.isdir(cache_dir) and not incremental:
6169
shutil.rmtree(cache_dir)
6270
env = os.environ.copy()
6371
env["PYTHONPATH"] = os.path.abspath(compiled_dir)
@@ -72,6 +80,10 @@ def run_benchmark(compiled_dir: str, check_dir: str) -> float:
7280
]
7381
cmd += glob.glob(os.path.join(abschk, "mypy/*.py"))
7482
cmd += glob.glob(os.path.join(abschk, "mypy/*/*.py"))
83+
if incremental:
84+
# Update a few files to force non-trivial incremental run
85+
edit_python_file(os.path.join(abschk, "mypy/__main__.py"))
86+
edit_python_file(os.path.join(abschk, "mypy/test/testcheck.py"))
7587
t0 = time.time()
7688
# Ignore errors, since some commits being measured may generate additional errors.
7789
subprocess.run(cmd, cwd=compiled_dir, env=env)
@@ -80,6 +92,12 @@ def run_benchmark(compiled_dir: str, check_dir: str) -> float:
8092

8193
def main() -> None:
8294
parser = argparse.ArgumentParser()
95+
parser.add_argument(
96+
"--incremental",
97+
default=False,
98+
action="store_true",
99+
help="measure incremental run (fully cached)",
100+
)
83101
parser.add_argument(
84102
"-n",
85103
metavar="NUM",
@@ -89,6 +107,7 @@ def main() -> None:
89107
)
90108
parser.add_argument("commit", nargs="+", help="git revision to measure (e.g. branch name)")
91109
args = parser.parse_args()
110+
incremental: bool = args.incremental
92111
commits = args.commit
93112
num_runs: int = args.n + 1
94113

@@ -127,7 +146,7 @@ def main() -> None:
127146
items = list(enumerate(commits))
128147
random.shuffle(items)
129148
for i, commit in items:
130-
tt = run_benchmark(target_dirs[i], self_check_dir)
149+
tt = run_benchmark(target_dirs[i], self_check_dir, incremental=incremental)
131150
# Don't record the first warm-up run
132151
if n > 0:
133152
print(f"{commit}: t={tt:.3f}s")

0 commit comments

Comments
 (0)