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

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

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

查看生成的数据。

相关推荐
芯联智造8 分钟前
【stm32协议外设篇】- SU03T 智能语音模块
c语言·开发语言·stm32·单片机·嵌入式硬件
川石课堂软件测试8 分钟前
Python | 高阶函数基本应用及Decorator装饰器
android·开发语言·数据库·python·功能测试·mysql·单元测试
lqqjuly11 分钟前
Matlab2025a实现双目相机标定~业余版
开发语言·matlab·相机标定·双目相机
Brianna Home15 分钟前
现代C++:从性能泥潭到AI基石
开发语言·c++·算法
2301_7965125226 分钟前
Rust编程学习 - 自动解引用的用处,如何进行“解引用”(Deref) 是“取引用”(Ref) 的反操作
开发语言·学习·rust
qq192572302730 分钟前
c++特性
开发语言·c++·算法
weixin_3077791335 分钟前
AWS Elastic Beanstalk 实现 Java 应用高可用部署指南
java·开发语言·云计算·aws·web app
Naiva36 分钟前
【小技巧】PyCharm建立项目,VScode+CodeX+WindowsPowerShell开发Python pyQT6
vscode·python·pycharm
nvd111 小时前
asyncio.run() vs asyncio.gather():启动器与聚合器, 为何Jupyter notebook里能直接使用await?
开发语言·python·jupyter
EEG小佬1 小时前
Jupyter选择内核时如何找到虚拟环境
ide·python·jupyter