From efc65e2fdc5ccd634f0d6809f0b48b561484f021 Mon Sep 17 00:00:00 2001 From: Askill Date: Sat, 23 Apr 2022 15:51:11 +0200 Subject: [PATCH] testing max connections and performance websockets --- .gitignore | 1 + clients.py | 25 +++++++++++++++++++++++++ server.py | 14 ++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 .gitignore create mode 100644 clients.py create mode 100644 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06898b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/venv/* \ No newline at end of file diff --git a/clients.py b/clients.py new file mode 100644 index 0000000..dc9286f --- /dev/null +++ b/clients.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +import asyncio +import time +import websockets + +async def hello(x, string): + async with websockets.connect("ws://localhost:8765") as websocket: + for _ in range(100): + await websocket.send(string) + await websocket.recv() + +async def main(x): + string = "Did you ever hear the tragedy of Darth Plagueis The Wise? I thought not. It’s not a story the Jedi would tell you. It’s a Sith legend. Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could use the Force to influence the midichlorians to create life… He had such a knowledge of the dark side that he could even keep the ones he cared about from dying. The dark side of the Force is a pathway to many abilities some consider to be unnatural. He became so powerful… the only thing he was afraid of was losing his power, which eventually, of course, he did. Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep. Ironic. He could save others from death, but not himself." + string2 = "Hi" + coros = [hello(i, string) for i in range(x)] + await asyncio.gather(*coros) + +if __name__ == '__main__': + for i in range(100, 12000, 1000): + t1 = time.time() + loop = asyncio.get_event_loop() + loop.run_until_complete(main(i)) + used = time.time() - t1 + print(i, used/i) \ No newline at end of file diff --git a/server.py b/server.py new file mode 100644 index 0000000..4e2e934 --- /dev/null +++ b/server.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +import asyncio +import websockets + +async def echo(websocket): + async for message in websocket: + await websocket.send(message) + +async def main(): + async with websockets.serve(echo, "localhost", 8765): + await asyncio.Future() # run forever + +asyncio.run(main()) \ No newline at end of file