Skip to content

Commit 017e155

Browse files
authored
Add Windows support (#30)
Add support for running benchmarks using runbench.py on Windows by: - using the correct extension for binary files (instead of hardcoding '.so') - using sys.executable to run python instead of hardcoding 'python3' - running mypyc as 'python scripts/mypyc' instead of 'scripts/mypyc' because the latter requires using the shebang `--priority` currently isn't supported on Windows. Fixes #10.
1 parent 53a17ee commit 017e155

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

runbench.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import time
99
import subprocess
1010
import statistics
11+
from pathlib import Path
1112

1213
from benchmarking import BenchmarkInfo, benchmarks
14+
from typing_extensions import Final
1315

1416

1517
# Minimum total time (seconds) to run a benchmark
@@ -18,6 +20,8 @@
1820
MIN_ITER = 10
1921

2022

23+
BINARY_EXTENSION: Final = 'pyd' if sys.platform == 'win32' else 'so'
24+
2125
def run_in_subprocess(benchmark: BenchmarkInfo,
2226
binary: Optional[str],
2327
compiled: bool,
@@ -30,7 +34,7 @@ def run_in_subprocess(benchmark: BenchmarkInfo,
3034

3135
if not compiled and binary:
3236
os.rename(binary, binary + '.tmp')
33-
cmd = ['python3', '-c', program]
37+
cmd = [sys.executable, '-c', program]
3438
if priority:
3539
# Use nice to increase process priority.
3640
cmd = ['sudo', 'nice', '-n', '-5'] + cmd
@@ -140,8 +144,8 @@ def compile_benchmark(module: str, raw_output: bool, mypy_repo: Optional[str]) -
140144
else:
141145
# Find 'mypyc' via PATH and use PYTHONPATH set by caller.
142146
cmd = 'mypyc'
143-
subprocess.run([cmd, fnam], check=True)
144-
pattern = module.replace('.', '/') + '.*.so'
147+
subprocess.run([sys.executable, cmd, fnam], check=True)
148+
pattern = module.replace('.', '/') + f'.*.{BINARY_EXTENSION}'
145149
paths = glob.glob(pattern)
146150
assert len(paths) == 1
147151
return paths[0]
@@ -151,15 +155,18 @@ def import_all() -> None:
151155
files = glob.glob('microbenchmarks/*.py')
152156
files += glob.glob('benchmarks/*.py')
153157
for fnam in files:
154-
if fnam.endswith('__init__.py') or not fnam.endswith('.py'):
158+
filepath = Path(fnam).resolve()
159+
if filepath.name == '__init__.py' or filepath.suffix != '.py':
155160
continue
156-
module = re.sub(r'[.]py$', '', fnam).replace('/', '.')
161+
benchmarks_root_dir = Path(__file__).parent.resolve()
162+
module_parts = filepath.with_suffix("").relative_to(benchmarks_root_dir).parts
163+
module = ".".join(module_parts)
157164
import_module(module)
158165

159166

160167
def delete_binaries() -> None:
161-
files = glob.glob('microbenchmarks/*.so')
162-
files += glob.glob('benchmarks/*.so')
168+
files = glob.glob(f'microbenchmarks/*.{BINARY_EXTENSION}')
169+
files += glob.glob(f'benchmarks/*.{BINARY_EXTENSION}')
163170
for fnam in files:
164171
os.remove(fnam)
165172

0 commit comments

Comments
 (0)