-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
62 lines (48 loc) · 1.98 KB
/
benchmark.py
File metadata and controls
62 lines (48 loc) · 1.98 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
import time
import os
import shutil
from minidb import MiniDB
def run_benchmark():
test_dir = "benchmark_data"
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
db = MiniDB(data_dir=test_dir)
# 1. Setup tables
db.execute_query("CREATE TABLE users (id, name)")
db.execute_query("CREATE TABLE orders (order_id, user_id, amount)")
# 2. Insert data
NUM_USERS = 500
NUM_ORDERS = 1000
print(f"Inserting {NUM_USERS} users and {NUM_ORDERS} orders...")
for i in range(NUM_USERS):
db.execute_query(f"INSERT INTO users VALUES ({i}, 'User {i}')")
for i in range(NUM_ORDERS):
user_id = i % NUM_USERS
db.execute_query(f"INSERT INTO orders VALUES ({i}, {user_id}, {i*10.5})")
# query
query = "SELECT * FROM users JOIN orders ON users.id = orders.user_id"
# Benchmark Hash Join (current implementation)
start_time = time.time()
results_hash = db.execute_query(query)
hash_duration = time.time() - start_time
print(f"Hash Join took: {hash_duration:.4f} seconds (found {len(results_hash)} rows)")
# Temporarily force Nested Loop Join to compare
orig_hash_join = db._hash_join
db._hash_join = db._nested_loop_join
start_time = time.time()
results_nl = db.execute_query(query)
nl_duration = time.time() - start_time
print(f"Nested Loop Join took: {nl_duration:.4f} seconds (found {len(results_nl)} rows)")
# Verify results are same
if len(results_hash) == len(results_nl):
print("[v] Success: Both algorithms returned the same number of rows.")
else:
print(f"[x] Error: Row count mismatch! Hash: {len(results_hash)}, NL: {len(results_nl)}")
# Summary
speedup = nl_duration / hash_duration if hash_duration > 0 else float('inf')
print(f"\nSpeedup: {speedup:.2f}x faster with Hash Join!")
# Cleanup
if os.path.exists(test_dir):
shutil.rmtree(test_dir)
if __name__ == "__main__":
run_benchmark()