import os
import sys
import matplotlib.pyplot as plt
import pandas as pd
def analyze_results(csv_file):
if not os.path.exists(csv_file):
print(f"File {csv_file} not found.")
return
df = pd.read_csv(csv_file)
print("=== Benchmark Analysis ===")
print(f"Total runs: {len(df)}")
success_rate = df.groupby("strategy")["success"].mean()
print("\nSuccess Rate by Strategy:")
print(success_rate)
avg_steps = df[df["success"] == True].groupby("strategy")["steps"].mean()
print("\nAverage Steps (Successful runs):")
print(avg_steps)
avg_time = df.groupby("strategy")["time_ms"].mean()
print("\nAverage Time (ms):")
print(avg_time)
summary = df.pivot_table(
index="strategy",
values=["steps", "time_ms", "success"],
aggfunc={"steps": "mean", "time_ms": "mean", "success": "mean"},
)
print("\nSummary Table:")
print(summary)
try:
plt.figure(figsize=(10, 6))
summary["success"].plot(kind="bar", title="Success Rate by Strategy")
plt.tight_layout()
plt.savefig("success_rate.png")
print("Saved success_rate.png")
plt.figure(figsize=(10, 6))
summary["steps"].plot(kind="bar", title="Avg Steps to Solve")
plt.tight_layout()
plt.savefig("avg_steps.png")
print("Saved avg_steps.png")
except Exception as e:
print(f"Could not save plots: {e}")
if __name__ == "__main__":
base_dir = os.path.dirname(os.path.abspath(__file__))
default_csv = os.path.join(base_dir, "benchmark_results.csv")
csv_file = sys.argv[1] if len(sys.argv) > 1 else default_csv
analyze_results(csv_file)