|
| 1 | +"""Benchmark results reporting.""" |
| 2 | + |
| 3 | +import csv |
| 4 | +import os |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +from tabulate import tabulate # type: ignore[import-untyped] |
| 8 | + |
| 9 | + |
| 10 | +def print_results(results: list[dict], table_format: str): |
| 11 | + """Print benchmark results in a formatted table.""" |
| 12 | + table_data = [] |
| 13 | + for result in results: |
| 14 | + op = result["operation"] |
| 15 | + if "top_k" in result: |
| 16 | + op = f"{op} (k={result['top_k']})" |
| 17 | + |
| 18 | + count = result.get("count", result.get("iterations", "-")) |
| 19 | + time_val = result.get("time", result.get("avg_time", 0)) |
| 20 | + ops_per_sec = result.get("ops_per_sec", result.get("searches_per_sec", 0)) |
| 21 | + |
| 22 | + table_data.append([op, count, f"{time_val:.4f}", f"{ops_per_sec:.2f}"]) |
| 23 | + |
| 24 | + print( |
| 25 | + "\n" |
| 26 | + + tabulate( |
| 27 | + table_data, |
| 28 | + headers=["Operation", "Count", "Time (s)", "Ops/sec"], |
| 29 | + tablefmt=table_format, |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def print_summary( |
| 35 | + all_results: dict[str, dict[int, list[dict]]], |
| 36 | + dataset_sizes: list[int], |
| 37 | + table_format: str, |
| 38 | +): |
| 39 | + """Print summary table of all benchmark results.""" |
| 40 | + for db_mode, mode_results in all_results.items(): |
| 41 | + print("\n" + "=" * 80) |
| 42 | + print(f"SUMMARY - Operations per Second by Dataset Size ({db_mode.upper()} DB)") |
| 43 | + print("=" * 80) |
| 44 | + |
| 45 | + operations = [ |
| 46 | + "add", |
| 47 | + "get_many", |
| 48 | + "similarity_search", |
| 49 | + "update_many", |
| 50 | + "get_all", |
| 51 | + "delete_many", |
| 52 | + ] |
| 53 | + summary_data = [] |
| 54 | + for op in operations: |
| 55 | + row = [op] |
| 56 | + for size in dataset_sizes: |
| 57 | + matching = [r for r in mode_results[size] if r["operation"] == op] |
| 58 | + if matching: |
| 59 | + ops_per_sec = matching[0].get( |
| 60 | + "ops_per_sec", matching[0].get("searches_per_sec", 0) |
| 61 | + ) |
| 62 | + row.append(f"{ops_per_sec:,.0f}") |
| 63 | + else: |
| 64 | + row.append("N/A") |
| 65 | + summary_data.append(row) |
| 66 | + |
| 67 | + headers = ["Operation"] + [f"{s:,}" for s in dataset_sizes] |
| 68 | + print(tabulate(summary_data, headers=headers, tablefmt=table_format)) |
| 69 | + print("=" * 80) |
| 70 | + |
| 71 | + |
| 72 | +def export_to_csv( |
| 73 | + all_results: dict[str, dict[int, list[dict]]], |
| 74 | + dataset_sizes: list[int], |
| 75 | + output_dir: str, |
| 76 | +): |
| 77 | + """Export benchmark results to CSV files.""" |
| 78 | + os.makedirs(output_dir, exist_ok=True) |
| 79 | + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 80 | + |
| 81 | + for db_mode, mode_results in all_results.items(): |
| 82 | + for size in dataset_sizes: |
| 83 | + filename = os.path.join( |
| 84 | + output_dir, f"benchmark_{db_mode}_{size}_{timestamp}.csv" |
| 85 | + ) |
| 86 | + |
| 87 | + with open(filename, "w", newline="") as f: |
| 88 | + writer = csv.writer(f) |
| 89 | + writer.writerow(["Operation", "ops_per_sec", "time_sec"]) |
| 90 | + |
| 91 | + operations = [ |
| 92 | + "add", |
| 93 | + "get_many", |
| 94 | + "similarity_search", |
| 95 | + "update_many", |
| 96 | + "get_all", |
| 97 | + "delete_many", |
| 98 | + ] |
| 99 | + for op in operations: |
| 100 | + matching = [r for r in mode_results[size] if r["operation"] == op] |
| 101 | + if matching: |
| 102 | + ops_per_sec = matching[0].get( |
| 103 | + "ops_per_sec", matching[0].get("searches_per_sec", 0) |
| 104 | + ) |
| 105 | + time_val = matching[0].get( |
| 106 | + "time", matching[0].get("avg_time", 0) |
| 107 | + ) |
| 108 | + writer.writerow([op, f"{ops_per_sec:.2f}", f"{time_val:.4f}"]) |
| 109 | + else: |
| 110 | + writer.writerow([op, "N/A", "N/A"]) |
| 111 | + |
| 112 | + print(f"Exported {db_mode} ({size} records) to: {filename}") |
0 commit comments