This commit is contained in:
Casper V. Kristensen 2024-06-15 19:43:10 +02:00
commit 1adf9b4632
5 changed files with 93 additions and 0 deletions

8
README.md Normal file
View file

@ -0,0 +1,8 @@
```shell
# server
python server.py
# client
python client.py | tee data.txt
python plot.py
```

37
client.py Normal file
View file

@ -0,0 +1,37 @@
import concurrent.futures
import itertools
import socket
import sys
from threading import Lock
from time import time
SERVER = "159.69.4.2"
PORT = 1337
NUM_WORKERS = 100
start = time()
lock = Lock()
count = itertools.count()
def worker():
while True:
try:
with lock:
i = next(count)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((SERVER, PORT))
data = str(i).encode("ascii")
sock.sendall(data)
response = sock.recv(1024)
print(int(response.decode("ascii")), float(time() - start))
except KeyboardInterrupt:
break
except Exception as e:
print(e, file=sys.stderr)
with concurrent.futures.ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
for _ in range(NUM_WORKERS):
executor.submit(worker)

24
plot.py Normal file
View file

@ -0,0 +1,24 @@
import plotly.express as px
ys = []
xs = []
with open("data.txt") as file:
for line in file.readlines():
print(line)
try:
y, x = map(float, line.split(" "))
except Exception as e:
print(e)
ys.append(y)
xs.append(x)
fig = px.scatter(
x=xs,
y=ys,
labels=dict(
x="connected at (s)",
y="connection number",
),
)
fig.show()

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
plotly

23
server.py Normal file
View file

@ -0,0 +1,23 @@
import socketserver
PORT = 1337
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
"""Echo"""
data = self.request.recv(1024)
print(data)
self.request.sendall(data)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
with ThreadedTCPServer(("0.0.0.0", PORT), ThreadedTCPRequestHandler) as server:
try:
server.serve_forever()
except KeyboardInterrupt:
server.shutdown()