本人作为测试人员,对现有服务端的websocket协议进行测试
当前脚本保持100个websocket连接,从而发现测试服务器的瓶颈在网络
测试服务器的带宽也确实不高才1M
同时连接多个WK,,网络资源占用消耗比较高
#coding=utf-8
from ws4py.client.threadedclient import WebSocketClient
from ws4py import websocket
from framework.testCoin import loginByMobile,getTable,wsHost
from framework.logger2 import Logger
import json,threading,time
logger = Logger(logger='zhajinhua').getlog()
class Ws(WebSocketClient):
def __init__(self,url,userId,token,tableId):
super().__init__(url)
self.userId = userId
self.token = token
self.tableId = tableId
def opened(self): #socket连接后调用
ws101 = json.dumps({
"cammand":101,
"data":{
"userId":self.userId,
"token":self.token,
"tableId":self.tableId,
"gameType":"zhajinhua"
}
})
self.send(ws101)
def closed(self, code, reason=None): #wk关闭时调用
print(code,time.strftime('%Y-%m-%d %H:%M:%S'))
def received_message(self, message): #长连接时接收socket的所有消息,可根据消息做对应处理
ms = json.loads(str(message))
#加入桌子
ws99 = json.dumps({
"cammand": 99,
"gameType": "zhajinhua"
})
if ms['cammand'] == 101:
self.send(ws99)
'''省略部分'''
def start(mobile):
loginInfo = loginByMobile(mobile) #登录
tableId = getTable(loginInfo['token'],11) #获取桌子ID
ws = Ws(wsHost,loginInfo['userId'],loginInfo['token'],tableId) #创建websocket
ws.connect() #连接wk
websocket.Heartbeat(ws).run() #发送心跳
ws.run_forever() #运行
if __name__ == '__main__':
mobile = 13600000001
for i in range(100):
th = threading.Thread(target=start,args=(mobile,)) #创建线程
th.start() #启动线程
time.sleep(1)
mobile += 1