locust 压测 websocket

没有服务器的看这

express 下搞一个 websocket 长连接-CSDN博客

1.安装 python 3.8

    https://www.python.org/

    py --version

2.安装 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

3.新建文件 main.py

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)


3.1进入到路径下 

        locust -f main.py

4.网页查看 

        http://localhost:8089/

4.1搞1000个号去连接 

每秒连接数为10个号

查看:RPS:每秒钟请求的个数, 在进行性能测试时,要参考设置的并发数进行分析

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

4.2把服务器关闭试试 

会有发送失败的出现

查看生成的数据。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值