|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import zipfile |
| 6 | +import urllib.request |
| 7 | +import json |
| 8 | +import time |
| 9 | +import argparse |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +# Configuration |
| 13 | +CACHE_DIR = Path("cache") |
| 14 | +MAPS_ZIP_URL = "https://www.movingai.com/benchmarks/mapf/mapf-map.zip" |
| 15 | +SCEN_ZIP_URL = "https://www.movingai.com/benchmarks/mapf/mapf-scen-random.zip" |
| 16 | +DEFAULT_AGENT_COUNTS = [2, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50] |
| 17 | + |
| 18 | +def download_file(url, dest): |
| 19 | + if dest.exists(): |
| 20 | + return |
| 21 | + print(f"Downloading {url} to {dest}...") |
| 22 | + urllib.request.urlretrieve(url, dest) |
| 23 | + |
| 24 | +def unzip_file(zip_path, extract_to): |
| 25 | + print(f"Unzipping {zip_path} to {extract_to}...") |
| 26 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 27 | + zip_ref.extractall(extract_to) |
| 28 | + |
| 29 | +def run_benchmark(map_path, scen_path, num_agents, timeout): |
| 30 | + cmd = [ |
| 31 | + "cargo", "run", "-p", "mapf-bench", "--release", "--", |
| 32 | + "--map", str(map_path), |
| 33 | + "--scen", str(scen_path), |
| 34 | + "--num-agents", str(num_agents), |
| 35 | + "--timeout", str(timeout) |
| 36 | + ] |
| 37 | + |
| 38 | + start_time = time.time() |
| 39 | + try: |
| 40 | + # Strict timeout enforcement via subprocess |
| 41 | + result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout + 5) |
| 42 | + duration = time.time() - start_time |
| 43 | + |
| 44 | + success = "Negotiation successful" in result.stdout |
| 45 | + return { |
| 46 | + "success": success, |
| 47 | + "duration": duration, |
| 48 | + "stdout": result.stdout, |
| 49 | + "stderr": result.stderr |
| 50 | + } |
| 51 | + except subprocess.TimeoutExpired: |
| 52 | + return { |
| 53 | + "success": False, |
| 54 | + "duration": timeout, |
| 55 | + "error": "Timeout" |
| 56 | + } |
| 57 | + except Exception as e: |
| 58 | + return { |
| 59 | + "success": False, |
| 60 | + "error": str(e) |
| 61 | + } |
| 62 | + |
| 63 | +def main(): |
| 64 | + parser = argparse.ArgumentParser() |
| 65 | + parser.add_argument("--timeout", type=int, default=30, help="Timeout in seconds per run") |
| 66 | + parser.add_argument("--max-scenarios", type=int, default=1, help="Max random scenarios per map") |
| 67 | + parser.add_argument("--maps", nargs="+", default=["empty-32-32.map", "room-32-32-4.map", "maze-32-32-2.map"]) |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + CACHE_DIR.mkdir(exist_ok=True) |
| 71 | + |
| 72 | + maps_zip = CACHE_DIR / "mapf-map.zip" |
| 73 | + scen_zip = CACHE_DIR / "mapf-scen-random.zip" |
| 74 | + |
| 75 | + download_file(MAPS_ZIP_URL, maps_zip) |
| 76 | + download_file(SCEN_ZIP_URL, scen_zip) |
| 77 | + |
| 78 | + maps_dir = CACHE_DIR / "maps" |
| 79 | + scen_dir = CACHE_DIR / "scenarios" |
| 80 | + |
| 81 | + if not maps_dir.exists(): |
| 82 | + unzip_file(maps_zip, maps_dir) |
| 83 | + if not scen_dir.exists(): |
| 84 | + unzip_file(scen_zip, scen_dir) |
| 85 | + |
| 86 | + report = {} |
| 87 | + |
| 88 | + print("Building mapf-bench in release mode...") |
| 89 | + subprocess.run(["cargo", "build", "-p", "mapf-bench", "--release"], check=True) |
| 90 | + |
| 91 | + for map_name in args.maps: |
| 92 | + map_path = maps_dir / map_name |
| 93 | + if not map_path.exists(): |
| 94 | + print(f"Map {map_name} not found.") |
| 95 | + continue |
| 96 | + |
| 97 | + base_name = map_name.replace(".map", "") |
| 98 | + |
| 99 | + # Find all matching scenario files |
| 100 | + scen_pattern = f"{base_name}-random-*.scen" |
| 101 | + scen_files = list((scen_dir / "scen-random").glob(scen_pattern)) |
| 102 | + scen_files.sort() |
| 103 | + |
| 104 | + if not scen_files: |
| 105 | + print(f"No scenarios found for {map_name} with pattern {scen_pattern}") |
| 106 | + continue |
| 107 | + |
| 108 | + selected_scens = scen_files[:args.max_scenarios] |
| 109 | + |
| 110 | + for scen_path in selected_scens: |
| 111 | + scen_name = scen_path.name |
| 112 | + print(f"\nBenchmarking {map_name} with scenario {scen_name}...") |
| 113 | + |
| 114 | + key = f"{map_name}:{scen_name}" |
| 115 | + report[key] = [] |
| 116 | + |
| 117 | + for count in DEFAULT_AGENT_COUNTS: |
| 118 | + print(f" Agents: {count}", end=" ", flush=True) |
| 119 | + res = run_benchmark(map_path, scen_path, count, args.timeout) |
| 120 | + if res["success"]: |
| 121 | + print(f"✅ ({res['duration']:.2f}s)") |
| 122 | + else: |
| 123 | + error_msg = res.get("error", "FAILED") |
| 124 | + print(f"❌ ({error_msg})") |
| 125 | + |
| 126 | + report[key].append({ |
| 127 | + "agents": count, |
| 128 | + "success": res["success"], |
| 129 | + "duration": res.get("duration", 0), |
| 130 | + "error": res.get("error") |
| 131 | + }) |
| 132 | + |
| 133 | + # Save report |
| 134 | + with open("benchmark_report.json", "w") as f: |
| 135 | + json.dump(report, f, indent=2) |
| 136 | + |
| 137 | + # Print summary table |
| 138 | + print("\nBenchmark Summary:") |
| 139 | + print(f"{'Scenario':<40} | {'Agents':<6} | {'Status':<8} | {'Time':<8}") |
| 140 | + print("-" * 75) |
| 141 | + for key, results in report.items(): |
| 142 | + for res in results: |
| 143 | + status = "SUCCESS" if res["success"] else (res.get("error") if res.get("error") else "FAILED") |
| 144 | + print(f"{key[:40]:<40} | {res['agents']:<6} | {status:<8} | {res['duration']:>7.2f}s") |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments