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 分钟前
SAM3:开放式分割,太强了(后面有SAM3权重下载方式)(单图测试、视频测试、实时跟踪)
图像处理·人工智能·python·深度学习·毕业设计·毕设·sam3
c++之路8 分钟前
C++ 面向对象编程(OOP)
开发语言·c++
weixin_424999369 分钟前
html如何修改备注
jvm·数据库·python
214396514 分钟前
HTML怎么创建时间轴布局_HTML结构化时间线写法【方法】
jvm·数据库·python
沐知全栈开发14 分钟前
CSS Backgrounds (背景)
开发语言
小草cys14 分钟前
树莓派4b + USRP B210 搭建反无人机(反无)系统( HTML + CDN )
开发语言·python·机器学习
gmaajt14 分钟前
HTML函数开发需要SSD吗_SSD对HTML函数开发效率影响【详解】
jvm·数据库·python
LiAo_1996_Y15 分钟前
p标签能嵌套div吗_HTML块级元素嵌套规则【解答】
jvm·数据库·python
2301_8166602117 分钟前
c++怎么将纯C的FILE-升级为C++的fstream_流缓冲绑定技巧【详解】
jvm·数据库·python
码界筑梦坊18 分钟前
89-基于Django的加利福尼亚州各县死亡概况分析系统
数据库·python·信息可视化·数据分析·django·毕业设计