locust 压测 websocket

* 安装 python 3.8

https://www.python.org/

py --version

* 安装 locust

pip install locust==2.5.1 -i http://pypi.douban.com/simple/

pip install locust==2.5.1 -i https://pypi.mirrors.ustc.edu.cn/simple/

locust -V

备注:-i 是切换下载源

* 安装依赖

pip install websockets

pip install websocket

pip install websocket_client

* 新建文件 main.py

python 复制代码
import time
import websocket
from locust import User, task, between, events

class WebSocketUser(User):
    wait_time = between(1, 2)

    def on_start(self):
        self.connection_status = False
        try:
            self.client = websocket.create_connection("ws://127.0.0.1:7777")
            self.connection_status = True
            events.request_success.fire(request_type="ws connect", name="connect Success",  response_time=0, response_length=0)
        except Exception as e:
            print(e)
            events.request_failure.fire(request_type="ws connect", name="connect Fail", exception=e, response_time=0, response_length=0)
        

    def on_stop(self):
        if self.connection_status:
            self.client.close()

    @task
    def ws_task(self):
        if self.connection_status:
            try:
                request = '{"cmd":"2","subcmd":"1","data":{"account":"daming","password":"daming"}}'
                start_time = time.time()
                self.client.send(request)
                response = self.client.recv()
                # print("response=", response)
                total_time = int((time.time() - start_time) * 1000)
                events.request_success.fire(request_type="ws send", name="Send Success", response_time=total_time, response_length=0)
            except Exception as e:
                total_time = int((time.time() - start_time) * 1000)
                events.request_failure.fire(request_type="ws send", name="Send Fail", exception=e, response_time=total_time, response_length=0)

* 进入到路径下

locust -f main.py

* 网页查看

http://localhost:8089/

搞1000个号去连接 每秒连接数为10个号

查看:RPS:每秒钟请求的个数, 在进行性能测试时,要参考设置的并发数进行分析

没有点击 停止的话会一直执行下去。

把服务器关闭试试 会有发送失败的出现

查看生成的数据。

相关推荐
zhangzeyuaaa2 分钟前
深入理解 Python GIL:从机制到释放时机
java·网络·python
PSLoverS5 分钟前
c++如何读取和修改可执行文件的PE头信息_IMAGE_NT_HEADERS解析【进阶】
jvm·数据库·python
gmaajt13 分钟前
React Native 单元测试中第三方依赖的正确 Mock 策略
jvm·数据库·python
a95114164218 分钟前
宝塔面板数据库查询响应慢_利用慢查询日志进行优化
jvm·数据库·python
zhangzeyuaaa24 分钟前
深入理解 Python 进程间通信:Queue 与 Pipe 实战解析
网络·python·中间件
2401_8314194436 分钟前
如何用 http 模块创建一个基础的 Web 服务器处理请求
jvm·数据库·python
pele36 分钟前
Redis如何防止AOF文件无限增大_触发BGREWRITEAOF命令进行日志重写
jvm·数据库·python
qq_4142565736 分钟前
golang如何设计HTTP中间件链_golang HTTP中间件链设计方法
jvm·数据库·python
m0_7467523037 分钟前
如何用方法简写语法在对象字面量中快速定义成员函数
jvm·数据库·python
qq_1898070339 分钟前
JavaScript 中高效定位二维数组间不匹配元素的行列索引
jvm·数据库·python