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:每秒钟请求的个数, 在进行性能测试时,要参考设置的并发数进行分析

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

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

查看生成的数据。

相关推荐
遇见你的雩风5 分钟前
【Golang】--- Goroutine
开发语言·golang
v沙加v6 分钟前
Java Rendering Engine Unknown
java·开发语言
张3蜂7 分钟前
python知识点点亮
开发语言·python
好学且牛逼的马7 分钟前
【Hot100|26-LeetCode 21. 合并两个有序链表 - 完整解法详解】
开发语言·python
Katecat996638 分钟前
矿井地雷检测与识别:Yolo11-HAFB-1模型应用详解
python
星月总相伴9 分钟前
python作用域
开发语言·python
阿里嘎多学长11 分钟前
2026-02-15 GitHub 热点项目精选
开发语言·程序员·github·代码托管
维度攻城狮14 分钟前
Python控制系统仿真案例-串联PID控制
python·simulink·pid·串级pid
嵌入式×边缘AI:打怪升级日志16 分钟前
第十一章:主控访问多个传感器(Modbus 网关/桥接器设计)
开发语言·javascript·ecmascript
~央千澈~22 分钟前
抖音弹幕游戏开发之第10集:整合 - 弹幕触发键盘操作·优雅草云桧·卓伊凡
开发语言·python·计算机外设