-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
59 lines (35 loc) · 1.79 KB
/
Copy pathbot.py
File metadata and controls
59 lines (35 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
import socket
import time
import random
HOST = '127.0.0.1' # standard loopback address for ipv4 (used for localhost always)
PORT = 8080 # cpp server port
Total_orders = 10000 # orders to fire
def run_market_maker():
print(f"Connecting to Exchange at {HOST}:{PORT}...")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # creating a tcp socket
try:
s.connect((HOST, PORT))
except ConnectionRefusedError:
print("ERROR: Connection refused.")
return
received = s.recv(1024).decode() # recv receives the message from socket and decodes it from byte language to python
print(f"Server is saying: {received}")
print(f"FIRING {Total_orders} ORDERS!")
start_time = time.time() # starting network timer to check time
for _ in range(Total_orders):
side = random.choice(["BUY", "SELL"])
price = random.randint(10000, 10100) # prices set between $100.00 and $101.00
quantity = random.randint(10, 100)
message = f"{side} {price} {quantity}\n"
s.sendall(message.encode()) # convert the message back to byte form by encoding and sending to all
acknowledgment = s.recv(1024)
end_time = time.time()
duration = end_time - start_time
orders_per_second = Total_orders/duration
print("\n--- STRESS TEST COMPLETE ---")
print(f"Sent {Total_orders} orders in {duration:.2f} seconds.")
print(f"Network Throughput: {orders_per_second:.2f} orders/second.")
s.close()
if __name__ == "__main__": # __name__ is a hidden variable created by python, and is set to __main__ when script directly run through terminal
run_market_maker()
# __name__ is set to (filename without py), if it is imported to some other file, and then ran