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

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

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

查看生成的数据。

相关推荐
阿标的博客2 分钟前
Python学习(三):Python程序的运行方式
开发语言·python·学习
IMPYLH3 分钟前
Linux 的 split 命令
linux·运维·python·bash·运维开发·unix
0xDevNull3 分钟前
分布式事务实战指南:从理论到Seata落地
java·开发语言·后端
z4424753266 分钟前
MySQL如何配置自动清理失效事务锁_结合定时任务清理
jvm·数据库·python
t***54411 分钟前
如何在 Dev-C++ 中配置 Clang 编译器
开发语言·c++
码云数智-大飞15 分钟前
TLS 1.3的革新:更快的速度与更强的安全性
开发语言·php
KIHU快狐17 分钟前
快狐KIHU|86寸落地触控一体机G+G电容屏HarmonyOS鸿蒙酒吧查询终端
python·华为·harmonyos
南境十里·墨染春水19 分钟前
linux学习进展 线程同步——条件变量
java·开发语言·学习
qq_3729069322 分钟前
怎么通过宝塔面板对网站数据库进行深度碎片整理_使用Optimize命令优化表空间资源占用
jvm·数据库·python
财经资讯数据_灵砚智能23 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月24日
人工智能·python·信息可视化·自然语言处理·ai编程