Locust V1.5.3压测websocket接口

本文介绍了如何利用Locust V1.5.3进行WebSocket接口的性能测试,通过webUI界面展示测试过程,并引用了locust-plugins插件作为参考。
摘要由CSDN通过智能技术生成
# -*- coding: utf-8 -*-#
# file:         socketio
# Author:       ShunZhe
# Date:         2021/6/26


import json
import logging
import re
import time

import gevent
import websocket
from locust import User

"""自定义websocket客户端"""

header = {
   
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json;charset=UTF-8',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/87.0.4280.88 Safari/537.36 '

}


class SocketIO(object):
    _locust_environment = None
    start_time = 0

    def __init__(self):
        self.events = None
        self.ws = None

    def _require_failure(self, name, response_time, message, action, context, **kwargs):
        """统计失败请求数"""
        self.events.request_failure.fire(
            request_type="接收数据",
            name=name,
            response_time=response_time,
            response_length=len(message),
            exception=f'{
     action} Content Error!',
            context=context,
        )

    def _require_success(self, name, response_time, message, **kwargs):
        """统计成功请求数"""
        se
以下是一个使用 Locust API 接口实现阶梯式压测的示例代码: ```python from locust import HttpUser, task, between import random class MyUser(HttpUser): wait_time = between(5, 9) min_users = 10 max_users = 100 step_users = 10 step_time = 5 @task def my_task(self): self.client.get("/") def on_start(self): self.user_count = self.min_users self.set_user_count() def set_user_count(self): self.environment.runner.user_count = self.user_count def on_stop(self): self.user_count = 0 self.set_user_count() def on_step(self, step): if (step + 1) % self.step_time == 0 and self.user_count < self.max_users: self.user_count += self.step_users self.set_user_count() ``` 这个示例代码定义了一个名为 MyUser 的用户类,其中定义了压测任务 my_task,用于模拟用户访问网站的行为。在 on_start 方法中,初始化用户数为最小用户数 min_users,并调用 set_user_count 方法设置 Locust 运行时的用户数。在 on_stop 方法中,停止用户数,并将 Locust 运行时的用户数设置为 0。 在 on_step 方法中,每隔一定时间(由 step_time 参数控制),将用户数增加 step_users(由 step_users 参数控制),并调用 set_user_count 方法,重新设置 Locust 运行时的用户数。 需要注意的是,这个示例代码仅作为演示使用,实际使用时需要根据测试场景和网站实际情况进行调整。同时,需要使用命令行启动 Locust 进行压测,并传递相关参数。具体命令行参数可以参考 Locust 的官方文档。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值