locust压测工具【学习】
1.安装:pip3 install locust
检验版本:locust -V
2.使用脚本:文章来源地址https://www.toymoban.com/news/detail-478287.html
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方法
这是获取用户特定测试数据的好地方。每个用户执行一次
"""
he
文章来源:https://www.toymoban.com/news/detail-478287.html
到了这里,关于locust压测工具【学习】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!