Here I have the async code in python-socketio from the docs https://python-socketio.readthedocs.io/
I'm able to establish the single connection between client and server and emit few messages. but i just want to create more number of clients connected to socket server and parallelly wants to observe the performance of the emit messages.
client.py
import random
import gevent
import time
import socketio
import asyncio
class ConnectionTest():
def __init__(self):
self.sio = socketio.AsyncClient(handle_sigint=True, logger=True, engineio_logger=True)
async def listen_to_redis(self):
await self.sio.connect('http://dfs.confec.co.in/?socket_qa2=True&session_id='+str(random.randint(1,100000)))
@self.sio.on('custom_event')
async def on_custom_event(data):
await self.sio.emit('message', '27 July 2015) was an Indian aerospace scientist')
@self.sio.on('client')
async def on_client(data):
pass
async def run(self):
await self.listen_to_redis()
async def main():
async def iterations(i):
print("connection :",i)
obj1 = ConnectionTest()
await obj1.run()
await obj1.sio.wait()
x = [iterations(i) for i in range(2000)]
await asyncio.gather(*x)
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())
Server.py
import uvicorn
import asyncio
import socketio
import aiohttp
url='redis://my_master:q2_gadse@10.1.7.4:6379/0'
channel='public_chat'
mgr = socketio.AsyncRedisManager(url=url, channel=channel, write_only=False)
sio = socketio.AsyncServer(async_mode='aiohttp',client_manager=mgr, async_handlers=True, logger=True, engineio_logger=True)
app = aiohttp.web.Application()
sio.attach(app)
@sio.on('connect')
async def test_connect(sid, environ):
print('connected', sid)
@sio.event
async def message(sid, message):
await sio.emit('client', message)
@sio.on('disconnect')
async def test_disconnect(sid):
print('Client disconnected')
if __name__=='__main__':
aiohttp.web.run_app(app, host='0.0.0.0', port=31451)
Update:
I'm able to get the performance for multiple asyncclient objects and i updated the code as well but I m not sure of why the cpu is 100% all the time with any of the deployment strategies. My target is to get atleast 5000 connections with a single processor and earlier my target was 10,000.
Hardware which I have is 4GB ram with 2 cores and 4 cores. I'm running client and server in different linux nodes. I see the client is quickly hitting the 100-200%cpu and server is somehow managing as I was using redis as a middleware to emit from outside script.
I have gone through the python-socketio document but I have not seen the section for performance tuning/Load testing/Stress like socket.io in nodejs. Here's the stats that I would like to show and how can i achieve the connections with atleast 5000 per process. Is there any code changes i could make for it. I need valuable suggestion as I was struck with it.