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

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

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

查看生成的数据。

相关推荐
怀旧,几秒前
【C++】20. unordered_set和unordered_map
开发语言·c++
alibli5 分钟前
一文学会CMakeLists.txt: CMake现代C++跨平台工程化实战
开发语言·c++·系统架构
Florence2344 分钟前
GPU硬件架构和配置的理解
开发语言
李游Leo1 小时前
JavaScript事件机制与性能优化:防抖 / 节流 / 事件委托 / Passive Event Listeners 全解析
开发语言·javascript·性能优化
JJJJ_iii2 小时前
【左程云算法09】栈的入门题目-最小栈
java·开发语言·数据结构·算法·时间复杂度
枫叶丹42 小时前
【Qt开发】显示类控件(三)-> QProgressBar
开发语言·qt
三体世界2 小时前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
Python私教2 小时前
Django全栈班v1.04 Python基础语法 20250912 下午
后端·python·django
Bear on Toilet2 小时前
继承类模板:函数未在模板定义上下文中声明,只能通过实例化上下文中参数相关的查找找到
开发语言·javascript·c++·算法·继承
xchenhao2 小时前
Scikit-Learn 对糖尿病数据集(回归任务)进行全面分析
python·机器学习·回归·数据集·scikit-learn·特征·svm