forked from aden-hive/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_storage.py
More file actions
63 lines (46 loc) · 1.79 KB
/
benchmark_storage.py
File metadata and controls
63 lines (46 loc) · 1.79 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
import asyncio
import shutil
import tempfile
import time
from framework.schemas.run import Run
from framework.storage.concurrent import ConcurrentStorage
async def measure_ops(storage: ConcurrentStorage, num_ops: int, concurrency: int):
"""Run concurrent save/load operations."""
async def worker(worker_id):
timings = []
for i in range(num_ops // concurrency):
run_id = f"bench_{worker_id}_{i}"
run = Run(id=run_id, goal_id="bench", input_data={"test": i})
start = time.perf_counter()
await storage.save_run(run)
save_dur = time.perf_counter() - start
start = time.perf_counter()
await storage.load_run(run_id)
load_dur = time.perf_counter() - start
timings.append(save_dur + load_dur)
return timings
start_total = time.perf_counter()
tasks = [worker(i) for i in range(concurrency)]
results = await asyncio.gather(*tasks)
total_dur = time.perf_counter() - start_total
flat_timings = [t for r in results for t in r]
avg_latency = sum(flat_timings) / len(flat_timings)
ops_sec = num_ops / total_dur
return ops_sec, avg_latency
async def main():
tmp_dir = tempfile.mkdtemp()
try:
print(f"Benchmarking ConcurrentStorage in {tmp_dir}")
storage = ConcurrentStorage(tmp_dir, batch_interval=0.05)
await storage.start()
# Warmup
await measure_ops(storage, 100, 10)
print("\nRunning Benchmark (1000 ops, 50 concurrency)...")
ops, lat = await measure_ops(storage, 1000, 50)
print(f"Throughput: {ops:.2f} OPS")
print(f"Avg Latency: {lat * 1000:.2f} ms")
await storage.stop()
finally:
shutil.rmtree(tmp_dir)
if __name__ == "__main__":
asyncio.run(main())