"""Capture routes and deterministic work on the legacy corpus and holdouts.""" import argparse import json import platform import subprocess from dataclasses import asdict, replace from importlib.metadata import version from pathlib import Path from statistics import median from time import perf_counter import shapely from inire import route from inire.tests.routing_corpus import automatic_options, benchmark_weighted_length, profile_options, select_cases def capture(repeat: int, revision: str | None) -> dict: cases = select_cases(("corpus", "retry-holdout")) runs = {} for case in cases: options = automatic_options(profile_options(case, "corpus-regression"), "corpus-regression") attempts = {"automatic": options} if case.problem.initial_paths: attempts["verify-only"] = replace(options, search=replace(options.search, node_limit=0)) for mode, settings in attempts.items(): key = f"{case.name}:{mode}" timings = [] reference = None stable = True for _ in range(repeat): started = perf_counter() run = route(case.problem, options=settings) timings.append(perf_counter() - started) metrics = asdict(run.metrics) sample = { "options": repr(settings), "metrics": {key: value for key, value in metrics.items() if not key.endswith("_ns")}, "results": { net_id: { "verified": result.is_valid, "outcome": result.outcome, "effective_net": repr(result.effective_net), "path": [ { "move": repr(move.move_spec), "start": move.start_port.as_tuple(), "end": move.end_port.as_tuple(), "length": move.length, "physical_wkb": [shape.wkb_hex for shape in move.physical_geometry], } for move in result.path ], } for net_id, result in run.results_by_net.items() }, "benchmark_weighted_length": sum( benchmark_weighted_length(result) for result in run.results_by_net.values() if result.is_valid ), } if reference is None: reference = sample elif reference != sample: stable = False assert reference is not None elapsed = median(timings) has_verified = any(result["verified"] for result in reference["results"].values()) runs[key] = { **reference, "wall_seconds": elapsed, "wall_samples": timings, "stable": stable, "time_cost_squared": elapsed * reference["benchmark_weighted_length"]**2 if has_verified else None, } return { "revision": revision or subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip(), "tracked_worktree": ( subprocess.check_output(["git", "status", "--short", "--untracked-files=no"], text=True).splitlines() if revision is None else [] ), "versions": { "python": platform.python_version(), "geos": shapely.geos_version_string, **{name: version(name) for name in ("numpy", "scipy", "shapely", "rtree")}, }, "runs": runs, } def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("output", type=Path) parser.add_argument("--repeat", type=int, default=1) parser.add_argument("--revision", help="source revision when running against a Git archive") args = parser.parse_args() if args.repeat < 1: parser.error("--repeat must be positive") args.output.write_text(json.dumps(capture(args.repeat, args.revision), indent=2) + "\n") if __name__ == "__main__": main()