-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
58 lines (49 loc) · 1.81 KB
/
test.py
File metadata and controls
58 lines (49 loc) · 1.81 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
import random
import asyncio
import time
SERVER_HOST = "localhost"
SERVER_PORT = 8800
NUM_CLIENTS = 10
ORDERS_PER_CLIENT = 50000 # Not used, we will run for a fixed time
RUN_DURATION = 60 # seconds
total_orders_sent = 0
orders_lock = asyncio.Lock()
def generate_order(client_prefix="C"):
script_codes = ["ITC", "RELIANCE", "TCS", "INFY"]
sides = ["BUY", "SELL"]
client_id = f"{client_prefix}{random.randint(1, 1000)}"
broker_id = f"B{random.randint(1, 100)}"
script_code = random.choice(script_codes)
side = random.choice(sides)
qty = random.randint(1, 1000)
price = round(random.uniform(245, 255), 2)
return f"{client_id},{broker_id},{script_code},{side},{qty},{price}"
async def send_orders_async(client_num, stop_time):
global total_orders_sent
try:
reader, writer = await asyncio.open_connection(SERVER_HOST, SERVER_PORT)
while time.time() < stop_time:
order_line = generate_order(client_prefix=f"C{client_num}_")
writer.write((order_line + "\n").encode())
await writer.drain()
response = b""
while not response.endswith(b"\n"):
chunk = await reader.read(4096)
if not chunk:
break
response += chunk
async with orders_lock:
total_orders_sent += 1
writer.close()
await writer.wait_closed()
except Exception:
pass # Optionally log errors if needed
async def main():
stop_time = time.time() + RUN_DURATION
tasks = []
for i in range(NUM_CLIENTS):
tasks.append(send_orders_async(i+1, stop_time))
await asyncio.gather(*tasks)
print(f"All clients finished. Total orders sent in {RUN_DURATION} seconds: {total_orders_sent}")
if __name__ == "__main__":
asyncio.run(main())