locust压测工具【学习】

locust压测工具【学习】

1.安装:pip3 install locust
检验版本:locust -V
2.使用脚本:

from locust import task, HttpUser, constant_pacing
from locust import between, constant, tag
from locust import events
from locust.runners import MasterRunner
import csv
import time
import json
from json import JSONDecodeError
from loguru import logger


@events.test_start.add_listener
# 压测开始的时候执行
def on_test_start(environment, **kwargs):
    if not isinstance(environment.runner, MasterRunner):
        print("Beginning test setup")
    else:
        print("Started test from Master node")


@events.test_stop.add_listener
# 压测结束的时候执行
def on_test_stop(environment, **kwargs):
    if not isinstance(environment.runner, MasterRunner):
        print("Cleaning up test data")
    else:
        print("Stopped test from Master node")


# 请求完成后,触发监听器:定义了输出响应的相关内容,这个可以放到locufile文件里面
@events.request.add_listener
def my_request_handler(request_type, name, response_time, response_length, response,
                       context, exception, start_time, url, **kwargs):
    if exception:
        print(f"Request to {
     name} failed with exception {
     exception}")
    else:
        print(f"request_type : {
     request_type}")
        print(f"response_time : {
     response_time}")
        print(f"response_length : {
     response_length}")
        print(f"context : {
     context}")
        print(f"start_time : {
     start_time}")
        print(f"url : {
     url}")
        print(f"Successfully made a request to: {
     name}")
        print(f"The response : {
     response.text}")


class User1(HttpUser):
    weight = 1  # user1类被执行的概率是25%,user2类被执行的概率是4分之3
    host = "https://xxx.com"  # 要加载的url的前缀
    wait_time = between(2, 5)  # 每个用户结束,等待2-5秒
    # wait_time = constant(3)  # 每个用户操作完成后,等待3秒
    # wait_time = constant_pacing(10)  # 强制只等待10秒,优先级大于@task标记方法自定义的的sleep(20)
    # wait_time = constant_throughput(0.1)  # pacing的反例,这个还是等待10秒,1/值(0.1) = 10

    def on_start(self):
        """
        每个user启动前调用on_start方法
        这是获取用户特定测试数据的好地方。每个用户执行一次
        """
        head
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值