-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworking_benchmark.py
More file actions
172 lines (140 loc) · 6.12 KB
/
working_benchmark.py
File metadata and controls
172 lines (140 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Working RAG Benchmark Suite
Tests Naive, Optimized, and Hyper RAG systems.
"""
import time
from pathlib import Path
import json
import csv
from datetime import datetime
from app.rag_naive import NaiveRAG
from app.rag_optimized import OptimizedRAG
from app.working_hyper_rag import WorkingHyperRAG
from app.no_compromise_rag import NoCompromiseHyperRAG
class WorkingBenchmark:
"""Comprehensive benchmark for all RAG systems."""
def __init__(self):
self.test_queries = [
"What is machine learning?",
"Explain artificial intelligence",
"How do neural networks work?",
"What is deep learning used for?",
"Describe natural language processing"
]
self.systems = [
("Naive RAG", NaiveRAG),
("Optimized RAG", OptimizedRAG),
("Hyper RAG", WorkingHyperRAG),
("NO-COMPROMISE RAG", NoCompromiseHyperRAG)
]
def run(self):
"""Run the comprehensive benchmark."""
print("\n" + "=" * 60)
print("🧪 WORKING RAG BENCHMARK SUITE")
print("=" * 60)
print("This will test Naive, Optimized, and Hyper RAG systems.")
print("Estimated time: 30-60 seconds\n")
all_results = {}
for system_name, system_class in self.systems:
print(f"\n📊 Testing {system_name}...")
system = system_class()
system.initialize()
times = []
chunks_used = []
for query in self.test_queries:
print(f" Query: {query[:30]}...")
start = time.perf_counter()
answer, chunks = system.query(query)
latency = (time.perf_counter() - start) * 1000
times.append(latency)
chunks_used.append(chunks)
print(f" Time: {latency:.1f}ms, Chunks: {chunks}")
system.close()
all_results[system_name] = {
"avg_ms": sum(times) / len(times),
"min_ms": min(times),
"max_ms": max(times),
"avg_chunks": sum(chunks_used) / len(chunks_used),
"all_times": times,
"all_chunks": chunks_used
}
# Save results
self._save_results(all_results)
# Print summary
self._print_summary(all_results)
return all_results
def _save_results(self, results):
"""Save results to CSV and JSON."""
timestamp = int(time.time())
# Save CSV
csv_dir = Path("working_benchmarks")
csv_dir.mkdir(exist_ok=True)
csv_file = csv_dir / f"benchmark_{timestamp}.csv"
with open(csv_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["System", "Avg Latency (ms)", "Avg Chunks", "Min (ms)", "Max (ms)"])
for system_name, data in results.items():
writer.writerow([
system_name,
f"{data['avg_ms']:.1f}",
f"{data['avg_chunks']:.1f}",
f"{data['min_ms']:.1f}",
f"{data['max_ms']:.1f}"
])
# Save JSON
json_file = csv_dir / f"benchmark_{timestamp}.json"
with open(json_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\n📁 CSV results saved to: {csv_file}")
print(f"📁 JSON results saved to: {json_file}")
def _print_summary(self, results):
"""Print a beautiful summary of results."""
print("\n" + "=" * 60)
print("📋 BENCHMARK SUMMARY")
print("=" * 60)
print("\nSystem Performance:")
print("-" * 60)
print(f"{'System':<20} {'Avg Latency':<12} {'Chunks Used':<12} {'Speed'}")
print("-" * 60)
for system_name in ["Naive RAG", "Optimized RAG", "Hyper RAG", "NO-COMPROMISE RAG"]:
if system_name in results:
data = results[system_name]
avg_latency = data['avg_ms']
avg_chunks = data['avg_chunks']
# Determine speed emoji
if avg_latency < 100:
speed = "⚡ Fast"
elif avg_latency < 200:
speed = "🚀 Good"
else:
speed = "🐢 Slow"
print(f"{system_name:<20} {avg_latency:<12.1f} {avg_chunks:<12.1f} {speed}")
print("\n" + "=" * 60)
# Calculate improvements
if "Naive RAG" in results and "NO-COMPROMISE RAG" in results:
naive_avg = results["Naive RAG"]["avg_ms"]
hyper_avg = results["NO-COMPROMISE RAG"]["avg_ms"]
if naive_avg > 0:
improvement = ((naive_avg - hyper_avg) / naive_avg) * 100
speedup = naive_avg / hyper_avg
print(f"🎯 IMPROVEMENTS (No-Compromise vs Naive):")
print("-" * 60)
print(f"Latency Improvement: {improvement:.1f}% faster")
print(f"Chunk Reduction: {(5 - results['NO-COMPROMISE RAG']['avg_chunks'])/5*100:.1f}% fewer chunks")
print(f"Speedup Factor: {speedup:.1f}x faster")
print("\n💼 BUSINESS IMPACT:")
print("-" * 60)
if speedup >= 2.0:
print("✅ EXCELLENT: 2x+ speedup achieved!")
print(" This is production-ready and investor-worthy.")
elif speedup >= 1.5:
print("📈 GOOD: 1.5x+ speedup")
print(" Solid foundation for production deployment.")
else:
print("⚠️ NEEDS WORK: Below 1.5x")
print(" Check configuration and optimization settings.")
print("\n" + "=" * 60)
print("✅ BENCHMARK COMPLETE")
if __name__ == "__main__":
benchmark = WorkingBenchmark()
results = benchmark.run()