|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import random |
| 4 | +import string |
| 5 | +import sys |
| 6 | +import textwrap |
| 7 | +from dataclasses import dataclass |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from runner import VirtualEnv |
| 11 | + |
| 12 | +script_dir = Path(__file__).resolve().parent |
| 13 | +repo_root = script_dir.parent |
| 14 | + |
| 15 | +log = logging.getLogger("runner") |
| 16 | +logging.basicConfig(format="[%(name)s] [%(levelname)s] %(message)s", level=logging.DEBUG) |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class BenchmarkConfig: |
| 21 | + seed: int |
| 22 | + filename_length: int |
| 23 | + depth: int |
| 24 | + num_python_editable_packages: int |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def default() -> "BenchmarkConfig": |
| 28 | + return BenchmarkConfig( |
| 29 | + seed=0, |
| 30 | + filename_length=10, |
| 31 | + depth=10, |
| 32 | + num_python_editable_packages=100, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def random_name(rng: random.Random, length: int) -> str: |
| 37 | + return "".join(rng.choices(string.ascii_lowercase, k=length)) |
| 38 | + |
| 39 | + |
| 40 | +def random_path(rng: random.Random, root: Path, depth: int, name_length: int) -> Path: |
| 41 | + path = root |
| 42 | + for _ in range(depth): |
| 43 | + path = path / random_name(rng, name_length) |
| 44 | + return path |
| 45 | + |
| 46 | + |
| 47 | +def create_python_package(root: Path) -> tuple[str, Path]: |
| 48 | + root.mkdir(parents=True, exist_ok=False) |
| 49 | + src_dir = root / "src" / root.name |
| 50 | + src_dir.mkdir(parents=True) |
| 51 | + (src_dir / "__init__.py").write_text( |
| 52 | + textwrap.dedent(f"""\ |
| 53 | + def get_name(): |
| 54 | + return "{root.name}" |
| 55 | + """) |
| 56 | + ) |
| 57 | + (root / "pyproject.toml").write_text( |
| 58 | + textwrap.dedent(f"""\ |
| 59 | + [project] |
| 60 | + name = "{root.name}" |
| 61 | + version = "0.1.0" |
| 62 | +
|
| 63 | + [tool.setuptools.packages.find] |
| 64 | + where = ["src"] |
| 65 | +
|
| 66 | + [build-system] |
| 67 | + requires = ["setuptools", "wheel"] |
| 68 | + build-backend = "setuptools.build_meta" |
| 69 | + """) |
| 70 | + ) |
| 71 | + return root.name, src_dir |
| 72 | + |
| 73 | + |
| 74 | +def create_benchmark_environment(root: Path, config: BenchmarkConfig) -> None: |
| 75 | + rng = random.Random(config.seed) |
| 76 | + |
| 77 | + log.info("creating benchmark environment at %s", root) |
| 78 | + root.mkdir(parents=True, exist_ok=False) |
| 79 | + venv = VirtualEnv.create(root / "venv", Path(sys.executable)) |
| 80 | + |
| 81 | + venv.install_editable_package(repo_root) |
| 82 | + |
| 83 | + python_package_names = [] |
| 84 | + python_package_paths = [] |
| 85 | + |
| 86 | + packages_root = random_path(rng, root, config.depth, config.filename_length) |
| 87 | + name, src_dir = create_python_package(packages_root) |
| 88 | + python_package_names.append(name) |
| 89 | + python_package_paths.append(src_dir) |
| 90 | + |
| 91 | + for _ in range(config.num_python_editable_packages): |
| 92 | + path = random_path(rng, packages_root, config.depth, config.filename_length) |
| 93 | + name, src_dir = create_python_package(path) |
| 94 | + python_package_names.append(name) |
| 95 | + python_package_paths.append(src_dir) |
| 96 | + |
| 97 | + python_package_paths_str = ", ".join(f'"{path.parent}"' for path in python_package_paths) |
| 98 | + import_python_packages = "\n".join(f"import {name}" for name in python_package_names) |
| 99 | + (root / "run.py").write_text(f"""\ |
| 100 | +import time |
| 101 | +import logging |
| 102 | +import sys |
| 103 | +import maturin_import_hook |
| 104 | +
|
| 105 | +sys.path.extend([{python_package_paths_str}]) |
| 106 | +
|
| 107 | +# logging.basicConfig(format='%(asctime)s %(name)s [%(levelname)s] %(message)s', level=logging.DEBUG) |
| 108 | +# maturin_import_hook.reset_logger() |
| 109 | +
|
| 110 | +maturin_import_hook.install() |
| 111 | +
|
| 112 | +start = time.perf_counter() |
| 113 | +
|
| 114 | +{import_python_packages} |
| 115 | +
|
| 116 | +end = time.perf_counter() |
| 117 | +print(f'took {{end - start:.6f}}s') |
| 118 | +""") |
| 119 | + |
| 120 | + |
| 121 | +def main() -> None: |
| 122 | + parser = argparse.ArgumentParser() |
| 123 | + parser.add_argument("root", type=Path, help="the location to write the benchmark data to") |
| 124 | + args = parser.parse_args() |
| 125 | + |
| 126 | + config = BenchmarkConfig.default() |
| 127 | + create_benchmark_environment(args.root, config) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments