|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Lightweight performance benchmark for mission planning. |
| 3 | +
|
| 4 | +Usage: |
| 5 | + python benchmark_mission_planner.py --segments 5 --repeat 3 |
| 6 | +
|
| 7 | +Reports average planning time; used as a sanity check so regression |
| 8 | +tests can watch for >2× slowdowns. |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import argparse |
| 13 | +import time |
| 14 | +from statistics import mean, stdev |
| 15 | + |
| 16 | +try: |
| 17 | + from impulse import IntegratedImpulseController, MissionWaypoint, ImpulseEngineConfig # type: ignore |
| 18 | +except Exception: # pragma: no cover |
| 19 | + import sys, pathlib |
| 20 | + sys.path.insert(0, str(pathlib.Path(__file__).parent)) |
| 21 | + from impulse import IntegratedImpulseController, MissionWaypoint, ImpulseEngineConfig # type: ignore |
| 22 | +from src.simulation.simulate_vector_impulse import Vector3D |
| 23 | + |
| 24 | + |
| 25 | +def build_waypoints(n_segments: int, distance: float = 20.0): |
| 26 | + wps = [MissionWaypoint(position=Vector3D(0,0,0), orientation=None)] |
| 27 | + pos = 0.0 |
| 28 | + for _ in range(n_segments): |
| 29 | + pos += distance |
| 30 | + wps.append(MissionWaypoint(position=Vector3D(pos,0,0), orientation=None)) |
| 31 | + return wps |
| 32 | + |
| 33 | + |
| 34 | +def bench(segments: int, repeat: int): |
| 35 | + cfg = ImpulseEngineConfig() |
| 36 | + ctrl = IntegratedImpulseController(cfg) |
| 37 | + wps = build_waypoints(segments) |
| 38 | + times = [] |
| 39 | + for _ in range(repeat): |
| 40 | + t0 = time.perf_counter() |
| 41 | + ctrl.plan_impulse_trajectory(wps) |
| 42 | + times.append(time.perf_counter() - t0) |
| 43 | + avg = mean(times) |
| 44 | + sd = stdev(times) if len(times) > 1 else 0.0 |
| 45 | + print(f"Segments: {segments}, repeat: {repeat}") |
| 46 | + print(f"Avg planning time: {avg*1000:.2f} ms (σ={sd*1000:.2f} ms)") |
| 47 | + # Simple threshold: flag if > 500 ms for given size |
| 48 | + if avg > 0.5: |
| 49 | + print("⚠️ Planning time exceeded 500 ms threshold") |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + ap = argparse.ArgumentParser() |
| 54 | + ap.add_argument("--segments", type=int, default=5) |
| 55 | + ap.add_argument("--repeat", type=int, default=3) |
| 56 | + args = ap.parse_args() |
| 57 | + bench(args.segments, args.repeat) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + main() |
0 commit comments