Skip to content

Commit 7abcffe

Browse files
authored
Enable type checking code fragment using perf_compare tool (#18291)
Previously the tool only supported measuring self-check performance. Now a code fragment can be passed using `-c "..."`. A typical use case would be something like `perf_compare.py -c "import torch" ...`, to measure the speed of processing `torch`.
1 parent 9db2368 commit 7abcffe

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

misc/perf_compare.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,27 @@ def edit_python_file(fnam: str) -> None:
6363
f.write(data)
6464

6565

66-
def run_benchmark(compiled_dir: str, check_dir: str, *, incremental: bool) -> float:
66+
def run_benchmark(
67+
compiled_dir: str, check_dir: str, *, incremental: bool, code: str | None
68+
) -> float:
6769
cache_dir = os.path.join(compiled_dir, ".mypy_cache")
6870
if os.path.isdir(cache_dir) and not incremental:
6971
shutil.rmtree(cache_dir)
7072
env = os.environ.copy()
7173
env["PYTHONPATH"] = os.path.abspath(compiled_dir)
7274
env["PYTHONHASHSEED"] = "1"
7375
abschk = os.path.abspath(check_dir)
74-
cmd = [
75-
sys.executable,
76-
"-m",
77-
"mypy",
78-
"--config-file",
79-
os.path.join(abschk, "mypy_self_check.ini"),
80-
]
81-
cmd += glob.glob(os.path.join(abschk, "mypy/*.py"))
82-
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"))
76+
cmd = [sys.executable, "-m", "mypy"]
77+
if code:
78+
cmd += ["-c", code]
79+
else:
80+
cmd += ["--config-file", os.path.join(abschk, "mypy_self_check.ini")]
81+
cmd += glob.glob(os.path.join(abschk, "mypy/*.py"))
82+
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"))
8787
t0 = time.time()
8888
# Ignore errors, since some commits being measured may generate additional errors.
8989
subprocess.run(cmd, cwd=compiled_dir, env=env)
@@ -112,12 +112,20 @@ def main() -> None:
112112
type=int,
113113
help="set maximum number of parallel builds (default=8)",
114114
)
115+
parser.add_argument(
116+
"-c",
117+
metavar="CODE",
118+
default=None,
119+
type=str,
120+
help="measure time to type check Python code fragment instead of mypy self-check",
121+
)
115122
parser.add_argument("commit", nargs="+", help="git revision to measure (e.g. branch name)")
116123
args = parser.parse_args()
117124
incremental: bool = args.incremental
118125
commits = args.commit
119126
num_runs: int = args.num_runs + 1
120127
max_workers: int = args.j
128+
code: str | None = args.c
121129

122130
if not (os.path.isdir(".git") and os.path.isdir("mypyc")):
123131
sys.exit("error: Run this the mypy repo root")
@@ -152,7 +160,7 @@ def main() -> None:
152160
items = list(enumerate(commits))
153161
random.shuffle(items)
154162
for i, commit in items:
155-
tt = run_benchmark(target_dirs[i], self_check_dir, incremental=incremental)
163+
tt = run_benchmark(target_dirs[i], self_check_dir, incremental=incremental, code=code)
156164
# Don't record the first warm-up run
157165
if n > 0:
158166
print(f"{commit}: t={tt:.3f}s")

0 commit comments

Comments
 (0)