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 分钟前
【附完整代码】Python爬取古筝网曲谱图片一键生成PDF(下·PDF生成与GUI篇)
开发语言·python·pdf
lunareclipse10 分钟前
Python 填坑:消失的信号点 —— 详解“可变默认参数”陷阱
python
光之后裔12 分钟前
Numpy以及Pytorch中多维数组的维度数与维度值以及轴axis理解
pytorch·python·numpy
代码中介商13 分钟前
C语言操作符深度解析:从基础到高级应用
c语言·开发语言
z小天才b17 分钟前
Java 设计模式完全指南:从入门到精通
java·开发语言·设计模式
tangweiguo0305198718 分钟前
RAG 从零到一:让大模型读懂你的文档
python·langchain
挖AI金矿20 分钟前
(六)文件与搜索 - 信息处理的正确姿势
人工智能·python·开源·个人开发·ai编程
zs宝来了22 分钟前
网络篇15-网络收发包应用之iptable
开发语言·网络·php
烤麻辣烫23 分钟前
算法--二分搜索
java·开发语言·学习·算法·intellij-idea
编码浪子24 分钟前
《安全 Rust 的边界在哪?》— 中文解读
开发语言·安全·rust