-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
50 lines (35 loc) · 1.27 KB
/
Copy pathbenchmark.py
File metadata and controls
50 lines (35 loc) · 1.27 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
import random
import sys
import time
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent / "src"))
from constants import BUY, SELL
from order import Order
from order_book import OrderBook
def generate_random_order(order_id):
side = random.choice([BUY, SELL])
price = random.randint(90, 110)
quantity = random.randint(1, 20)
timestamp = order_id
return Order(order_id, side, float(price), quantity, timestamp)
def run_benchmark(number_of_orders):
book = OrderBook()
total_trades = 0
start_time = time.perf_counter()
for order_id in range(1, number_of_orders + 1):
order = generate_random_order(order_id)
trades = book.add_order(order)
total_trades += len(trades)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
orders_per_second = number_of_orders / elapsed_time
print(f"\nBenchmark for {number_of_orders:,} orders")
print(f"Trades generated: {total_trades:,}")
print(f"Time taken: {elapsed_time:.4f} seconds")
print(f"Orders per second: {orders_per_second:,.0f}")
print(f"Final active orders: {book.active_order_count()}")
if __name__ == "__main__":
random.seed(42)
run_benchmark(1_000)
run_benchmark(10_000)
run_benchmark(100_000)