python websocket异步高并发_websocket长连接压力测试踩过的坑

Websocket协议压测记录

背景:

公司的行情系统是采用的websocket协议,有请求和订阅两种方式向服务器申请最新行情信息。请求方式是一次的,订阅方式是建立连接后,服务器定时向客户端推送行情信息。

初步测试方案:

因考虑到websocket是双工通讯,是长连接,并且本次压测的性能指标是系统能建立的最大连接数,并且是建立连接后服务器能持续向客户端推送行情信息。

基于以上原因考虑用python采用多线程建立连接,为了验证能否收到推送的信息,把返回的行情信息保存到文本文件中。Python脚本如下:

import websocket

import time

import threading

import gzip

#import json

#from threadpool import ThreadPool, makeRequests

#from websocket import create_connection

SERVER_URL = "ws://pro-web-new.devtest.exshell-dev.com/r1/main/ws"

#SERVER_URL = "wss://i.cg.net/wi/ws"

#SERVER_URL = "wss://www.exshell.com/r1/main/ws"

def on_message(ws, message):

print(message)

def on_error(ws, error):

print(error)

def on_close(ws):

print("### closed ###")

def on_open(ws):

def send_trhead():

send_info = '{"sub": "market.ethusdt.kline.1min","id": "id10"}'

#send_info = '{"event":"subscribe", "channel":"btc_usdt.deep"}'

while True:

#time.sleep(5)

#ws.send(json.dumps(send_info))

ws.send(send_info)

while (1):

compressData = ws.recv()

result = gzip.decompress(compressData).decode('utf-8')

if result[:7] == '{"ping"':

ts = result[8:21]

pong = '{"pong":' + ts + '}'

ws.send(pong)

ws.send(send_info)

else:

#print(result)

with open('./test_result.txt', 'a&

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值