|
| 1 | +#!/usr/bin/env python |
| 2 | +"""P6.5 Performance Benchmark & Runtime Profiling. Usage: |
| 3 | + python Scripts/run_benchmark.py --all --verbose |
| 4 | + python Scripts/run_benchmark.py --modules --queries --graph |
| 5 | + python Scripts/run_benchmark.py --large-file --max-synthetic-rows 100000 |
| 6 | + python Scripts/run_benchmark.py --agent --json |
| 7 | + python Scripts/run_benchmark.py --all --skip-large-file --export |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import argparse |
| 13 | +import json |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) |
| 18 | + |
| 19 | + |
| 20 | +def build_arg_parser() -> argparse.ArgumentParser: |
| 21 | + p = argparse.ArgumentParser( |
| 22 | + description="P6.5 Performance Benchmark & Runtime Profiling", |
| 23 | + ) |
| 24 | + # Category flags |
| 25 | + p.add_argument("--all", action="store_true", help="Run all benchmarks") |
| 26 | + p.add_argument("--modules", action="store_true", help="Run module loading benchmark") |
| 27 | + p.add_argument("--queries", action="store_true", help="Run query latency benchmark") |
| 28 | + p.add_argument("--graph", action="store_true", help="Run graph benchmark") |
| 29 | + p.add_argument("--datasets", action="store_true", help="Run dataset benchmark") |
| 30 | + p.add_argument("--agent", action="store_true", help="Run agent benchmark (evidence_only)") |
| 31 | + p.add_argument("--large-file", action="store_true", help="Run large-file safety benchmark") |
| 32 | + |
| 33 | + # Skip flags |
| 34 | + p.add_argument("--skip-modules", action="store_true") |
| 35 | + p.add_argument("--skip-queries", action="store_true") |
| 36 | + p.add_argument("--skip-graph", action="store_true") |
| 37 | + p.add_argument("--skip-datasets", action="store_true") |
| 38 | + p.add_argument("--skip-agent", action="store_true") |
| 39 | + p.add_argument("--skip-large-file", action="store_true") |
| 40 | + p.add_argument("--skip-vector", action="store_true", help="Skip vector search in queries") |
| 41 | + |
| 42 | + # Options |
| 43 | + p.add_argument("--include-llm", action="store_true", help="Allow LLM usage in agent benchmark") |
| 44 | + p.add_argument("--max-synthetic-rows", type=int, default=100_000, |
| 45 | + help="Max synthetic CSV rows for large-file test (default: 100000)") |
| 46 | + p.add_argument("--root", type=Path, default=None, help="Project root path") |
| 47 | + p.add_argument("--json", action="store_true", help="Output results as JSON") |
| 48 | + p.add_argument("--verbose", action="store_true", help="Verbose output") |
| 49 | + p.add_argument("--export", action="store_true", help="Export results to 09_Exports/benchmarks/") |
| 50 | + |
| 51 | + return p |
| 52 | + |
| 53 | + |
| 54 | +def main() -> int: |
| 55 | + args = build_arg_parser().parse_args() |
| 56 | + |
| 57 | + from scientra.benchmark.benchmark_runner import BenchmarkRunner |
| 58 | + from scientra.benchmark.benchmark_report_builder import _serialize_run |
| 59 | + |
| 60 | + # Determine what to run |
| 61 | + run_all = args.all |
| 62 | + if not any([run_all, args.modules, args.queries, args.graph, args.datasets, args.agent, args.large_file]): |
| 63 | + # If no specific flag, default to --all |
| 64 | + run_all = True |
| 65 | + |
| 66 | + runner = BenchmarkRunner(root=args.root) |
| 67 | + |
| 68 | + try: |
| 69 | + benchmark_run = runner.run_all( |
| 70 | + skip_modules=args.skip_modules or (not run_all and not args.modules), |
| 71 | + skip_queries=args.skip_queries or (not run_all and not args.queries), |
| 72 | + skip_graph=args.skip_graph or (not run_all and not args.graph), |
| 73 | + skip_datasets=args.skip_datasets or (not run_all and not args.datasets), |
| 74 | + skip_agent=args.skip_agent or (not run_all and not args.agent), |
| 75 | + skip_large_file=args.skip_large_file or (not run_all and not args.large_file), |
| 76 | + include_llm=args.include_llm, |
| 77 | + max_synthetic_rows=args.max_synthetic_rows, |
| 78 | + keep_synthetic_files=True, |
| 79 | + ) |
| 80 | + except Exception as exc: |
| 81 | + print(f"Fatal benchmark error: {exc}", file=sys.stderr) |
| 82 | + return 1 |
| 83 | + |
| 84 | + # Generate reports |
| 85 | + try: |
| 86 | + reports = runner.generate_reports() |
| 87 | + except Exception as exc: |
| 88 | + print(f"Warning: Report generation failed: {exc}", file=sys.stderr) |
| 89 | + reports = {} |
| 90 | + |
| 91 | + # Export if requested |
| 92 | + if args.export: |
| 93 | + try: |
| 94 | + exported = runner.export_results() |
| 95 | + if args.verbose: |
| 96 | + for path, name in exported.items(): |
| 97 | + print(f" Exported: {path}") |
| 98 | + except Exception as exc: |
| 99 | + print(f"Warning: Export failed: {exc}", file=sys.stderr) |
| 100 | + |
| 101 | + # Output |
| 102 | + if args.json: |
| 103 | + print(json.dumps(_serialize_run(benchmark_run), ensure_ascii=False, indent=2)) |
| 104 | + else: |
| 105 | + print(f"\n{'='*60}") |
| 106 | + print(f" Scientra Copilot P6.5 Performance Benchmark") |
| 107 | + print(f" Run ID: {benchmark_run.run_id}") |
| 108 | + print(f" Duration: {benchmark_run.total_duration_ms:.1f} ms") |
| 109 | + print(f"{'='*60}") |
| 110 | + print(f"\n Summary:") |
| 111 | + print(f" Tasks: {benchmark_run.total_tasks}") |
| 112 | + print(f" Passed: {benchmark_run.total_passed} ✅") |
| 113 | + print(f" Warnings: {benchmark_run.total_warnings} ⚠️") |
| 114 | + print(f" Failed: {benchmark_run.total_failed} ❌") |
| 115 | + print(f"\n Slowest Module: {benchmark_run.slowest_module} ({benchmark_run.slowest_module_ms:.1f} ms)") |
| 116 | + print(f" Slowest Query: {benchmark_run.slowest_query} ({benchmark_run.slowest_query_ms:.1f} ms)") |
| 117 | + print(f" Large-File Safety: {'✅ Passed' if benchmark_run.large_file_safety_passed else '❌ Failed'}") |
| 118 | + |
| 119 | + if args.verbose: |
| 120 | + print(f"\n Per-Category Results:") |
| 121 | + for key, result in benchmark_run.results.items(): |
| 122 | + status = "✅" if result.success else "❌" |
| 123 | + print(f" [{status}] {key}: {result.passed} passed, {result.warning_count} warnings, {result.failed} failed ({result.total_duration_ms:.1f} ms)") |
| 124 | + |
| 125 | + if benchmark_run.cache_recommendations: |
| 126 | + print(f"\n Cache Recommendations ({len(benchmark_run.cache_recommendations)}):") |
| 127 | + for rec in benchmark_run.cache_recommendations[:5]: |
| 128 | + print(f" [{rec['priority']}] {rec['target']}: {rec['recommendation'][:80]}...") |
| 129 | + |
| 130 | + if benchmark_run.incremental_update_recommendations: |
| 131 | + print(f"\n Incremental Update Recommendations ({len(benchmark_run.incremental_update_recommendations)}):") |
| 132 | + for rec in benchmark_run.incremental_update_recommendations[:5]: |
| 133 | + print(f" [{rec['priority']}] {rec['target']}: {rec['recommendation'][:80]}...") |
| 134 | + |
| 135 | + if benchmark_run.p7_readiness_warnings: |
| 136 | + print(f"\n P7 Readiness Warnings ({len(benchmark_run.p7_readiness_warnings)}):") |
| 137 | + for w in benchmark_run.p7_readiness_warnings: |
| 138 | + print(f" ⚠️ {w}") |
| 139 | + |
| 140 | + if reports: |
| 141 | + print(f"\n Reports Generated:") |
| 142 | + for name, path in reports.items(): |
| 143 | + print(f" {name}: {path}") |
| 144 | + |
| 145 | + if benchmark_run.errors: |
| 146 | + print(f"\n Errors:") |
| 147 | + for e in benchmark_run.errors: |
| 148 | + print(f" ❌ {e}") |
| 149 | + |
| 150 | + print(f"\n Full report: 10_System/benchmarks/benchmark_report.md\n") |
| 151 | + |
| 152 | + # Return non-zero if there were hard failures |
| 153 | + if benchmark_run.total_failed > 0: |
| 154 | + return 1 |
| 155 | + return 0 |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + raise SystemExit(main()) |
0 commit comments