Locust 运行方式

发布时间 2023-03-29 15:56:44作者: 莲(LIT)
 
 1、命令参数运行
# -*- coding: utf-8 -*-
from locust import TaskSet, task, User

'''
命令行参数运行示例代码
'''

class Task_1(TaskSet):

    @task
    def task_a(self):
        print('打开冰箱门')

    @task
    def task_b(self):
        print('把大象装进冰箱')

    @task
    def task_c(self):
        print('关上冰箱门')

class task_conf(User):
    tasks = [Task_1]
#进入当前模块文件目录,直接输入
locust

#非当前文件目录
locust -f  xx/xx.py

#指定host
locust -f  xx/xx.py  --host=https://www.cnblogs.com

#指定locust webui 界面地址,
locust -f  xx/xx.py  --web-host ="127.0.0.1"

#不带ui界面运行,
locust -f  xx/xx.py  --headless -u 1000 -r 100 --run-time 5m
--headless 没有webui的情况下运行
-u  要生成的用户数
-r  每秒启动的用户数
--run-time 5m 设置测试执行时间 这里是5分钟,时间到了就会停止运行

#多进程运行
主模式启动 8089端口用于web界面 ,5557用于从机交互
locust -f  xx/xx.py --master
启动一个进行执行
locust -f xx/xx.py  --worker --master-host=192.168.0.56
(如果单机器运行,则可以省略后面的参数 --master-host=xxx.xxx.xxx.xxx)

 

2、配置文件方式运行

# -*- coding: UTF-8 -*
'''
# locust 默认读取配置文件的优先级顺序(覆盖读取)
# ~/locust.conf -> ./locust.conf ->(file specified using --conf)
# 注意:如果同时存在 两个配置文件,locust.conf 和 diy.conf
# 此时使用 locust --config = diy.conf 读取的将还是locust.conf
'''

# 要运行的py文件
locustfile = ./my_locust_file.py
# 设置是否带webui运行
headless = false
# 设置weiui的地址
web-host= 127.0.0.1
# 设置 host
host = http://wthrcdn.etouch.cn
# 设置虚拟用户数
users = 500
# 设置每秒增加的用户数
spawn-rate = 10
# 设置运行时间,满足设置的运行时间后,将停止运行
run-time = 1m