一个高并发的websocket测试脚本

xingyun86 2021-5-26 1446

Win10开启注册表修改MaxUserPort:

计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters:
MaxUserPort=65534

运行环境:python3.7.3

安装依赖:

pip3 install websocket
pip3 install threadpool
pip3 install websocket-client
pip3 install multiprocess

编写脚本代码:

#!/usr/bin/python
#-*- coding:utf-8 -*
 
import websocket
import time
import threading
import json
import multiprocessing
from threadpool import ThreadPool, makeRequests
 
#修改成自己的websocket地址
WS_URL = "ws://127.0.0.1:8080/ws" 
#定义进程数
processes=3
#定义线程数(每个文件可能限制1024个,可以修改fs.file等参数)
thread_num=2000
 
def on_message(ws, message):
     print(message)
     pass
 
def on_error(ws, error):
    print(error)
    pass
def on_close(ws):
    print("### closed ###")
    pass
def on_open(ws):
    def send_trhead():
        #设置你websocket的内容
        send_info = {"cmd": "refresh", "data": {"room_id": "58", "wx_user_id": 56431}}
        #每隔10秒发送一下数据使链接不中断
        while True:
            time.sleep(10)
            ws.send(json.dumps(send_info))
 
    t = threading.Thread(target=send_trhead)
    t.start()
 
def on_start(num):
    time.sleep(num%20)
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(WS_URL,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()
 
def thread_web_socket():
    #线程池
    pool = ThreadPool(thread_num)
    num = list()
    #设置开启线程的数量
    for ir in range(thread_num):
        num.append(ir)
    requests = makeRequests(on_start, num)
    [pool.putRequest(req) for req in requests]
    pool.wait()
 
if __name__ == "__main__":
    pool = multiprocessing.Pool(processes=processes)
    for i in range(processes):
        pool.apply_async(thread_web_socket)
    pool.close()
    pool.join()


×
打赏作者
最新回复 (0)
查看全部
全部楼主
返回