|
| 1 | +# Copyright (c) Brandon Pacewic |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import os |
| 5 | +import re |
| 6 | +import json |
| 7 | +from pathlib import Path |
| 8 | +from collections import defaultdict |
| 9 | + |
| 10 | +BUILD_DIR = "build" |
| 11 | +RESULTS_DIR = Path(BUILD_DIR) / "benchmarks" / "results" |
| 12 | + |
| 13 | + |
| 14 | +def extract_algo_name(filename: str) -> str: |
| 15 | + # Removes timestamp and UUID prefix |
| 16 | + # Matches: 2025-04-17_13-23-38_abcdef_algo.json |
| 17 | + return re.sub(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}_[^_]+_[^_]+_", "", filename) |
| 18 | + |
| 19 | + |
| 20 | +def print_benchmark_data(file_path: Path) -> None: |
| 21 | + try: |
| 22 | + with open(file_path, 'r') as f: |
| 23 | + data = json.load(f) |
| 24 | + |
| 25 | + for bench in data.get("benchmarks", []): |
| 26 | + name = bench.get("name", "") |
| 27 | + real_time = bench.get("real_time", 0.0) |
| 28 | + cpu_time = bench.get("cpu_time", 0.0) |
| 29 | + iterations = bench.get("iterations", 0) |
| 30 | + print(f"{name:<30} Time: {real_time:>10.2f} CPU: {cpu_time:>10.2f} Iter: {iterations}") |
| 31 | + except Exception as e: |
| 32 | + print(f"Error reading {file_path.name}: {e}") |
| 33 | + |
| 34 | + |
| 35 | +def main() -> None: |
| 36 | + print("Showing most recent benchmark outputs per algorithm...") |
| 37 | + if not RESULTS_DIR.is_dir(): |
| 38 | + print(f"Results directory '{RESULTS_DIR}' not found.") |
| 39 | + return |
| 40 | + |
| 41 | + algo_to_files = defaultdict(list) |
| 42 | + for file in sorted(RESULTS_DIR.glob("*.json"), key=os.path.getmtime, reverse=True): |
| 43 | + algo_name = extract_algo_name(file.name) |
| 44 | + algo_to_files[algo_name].append(file) |
| 45 | + |
| 46 | + for algo, files in algo_to_files.items(): |
| 47 | + latest = files[0] |
| 48 | + display_name = algo.replace(".json", "").replace(".cpp", "") |
| 49 | + |
| 50 | + print("────────────────────────────────────────────────────────────") |
| 51 | + print(f"Algorithm: {display_name}") |
| 52 | + print(f"File: {latest.name}\n") |
| 53 | + |
| 54 | + print_benchmark_data(latest) |
| 55 | + print() |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + main() |
0 commit comments