Skip to content

Support measuring incremental runs in perf_compare script #18289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions misc/perf_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ def clone(target_dir: str, commit: str | None) -> None:
subprocess.run(["git", "checkout", commit], check=True, cwd=target_dir)


def run_benchmark(compiled_dir: str, check_dir: str) -> float:
def edit_python_file(fnam: str) -> None:
with open(fnam) as f:
data = f.read()
data += "\n#"
with open(fnam, "w") as f:
f.write(data)


def run_benchmark(compiled_dir: str, check_dir: str, *, incremental: bool) -> float:
cache_dir = os.path.join(compiled_dir, ".mypy_cache")
if os.path.isdir(cache_dir):
if os.path.isdir(cache_dir) and not incremental:
shutil.rmtree(cache_dir)
env = os.environ.copy()
env["PYTHONPATH"] = os.path.abspath(compiled_dir)
Expand All @@ -72,6 +80,10 @@ def run_benchmark(compiled_dir: str, check_dir: str) -> float:
]
cmd += glob.glob(os.path.join(abschk, "mypy/*.py"))
cmd += glob.glob(os.path.join(abschk, "mypy/*/*.py"))
if incremental:
# Update a few files to force non-trivial incremental run
edit_python_file(os.path.join(abschk, "mypy/__main__.py"))
edit_python_file(os.path.join(abschk, "mypy/test/testcheck.py"))
t0 = time.time()
# Ignore errors, since some commits being measured may generate additional errors.
subprocess.run(cmd, cwd=compiled_dir, env=env)
Expand All @@ -80,6 +92,12 @@ def run_benchmark(compiled_dir: str, check_dir: str) -> float:

def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--incremental",
default=False,
action="store_true",
help="measure incremental run (fully cached)",
)
parser.add_argument(
"-n",
metavar="NUM",
Expand All @@ -89,6 +107,7 @@ def main() -> None:
)
parser.add_argument("commit", nargs="+", help="git revision to measure (e.g. branch name)")
args = parser.parse_args()
incremental: bool = args.incremental
commits = args.commit
num_runs: int = args.n + 1

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