Events Hook
Locust提供了事件钩子函数,它们可以在特定的时间点执行,例如test_start
,其类似与pytest
中的setup_module
使用方法举例
- 使用时需要引入events模块
from locust import events
- 自定义一个方法,若不清楚需要接收什么参数,一般来说直接给一个可变关键字参数
**kwargs
即可(除非你需要用到接收的参数来做某些事情)「官方文档」 - 加上装饰器@events.xxx.add_listener,(xxx指你希望使用的events,比如
test_start
、init
等等)
以下介绍Locust自带的一些常用的events对象
test_start&test_stop
类似pytest
中的setup_module
和teardown_module
,在整个模块开始执行测试之前(后)执行,例子:
from locust import task, HttpUser, events
@events.test_start.add_listener
def on_test_start(**kwargs):
print(kwargs['environment'])
print('test is start')
@events.test_stop.add_listener
def on_test_stop(environment): # 知道要接受什么参数就填,不知道就用可变关键字参数,效果一样
print(environment)
print('test is stop')
class User1(HttpUser):
@task
def test_01(self):
print('test_01')
self.stop()
class User2(HttpUser):
@task
def test_02(self):
print('test_02')
self.stop()
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_test_start.py -u 4 -r 4 -t 2 --headless --host https://www.baidu.com --only-summary
[2023-08-04 12:03:58,402] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Run time limit set to 2 seconds
[2023-08-04 12:03:58,402] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
<locust.env.Environment object at 0x105b778e0>
test is start
[2023-08-04 12:03:58,403] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 4 users at a rate of 4.00 per second
[2023-08-04 12:03:58,403] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"User1": 2, "User2": 2} (4 total users)
test_01
test_01
test_02
test_02
[2023-08-04 12:04:00,278] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: --run-time limit reached, shutting down
<locust.env.Environment object at 0x105b778e0>
test is stop
- ❕结果可看到设置4个用户,两个钩子在模块执行测试之前(后)只执行一次❕
init
Locust创建好Environment和locust runner实例后执行,在test_start之前,例子
from locust import task, HttpUser, events
@events.init.add_listener
def on_test_init(**kwargs):
print(kwargs.get('environment'))
print(kwargs.get('runner'))
print(kwargs.get('web_ui'))
print('test init fire')
@events.test_start.add_listener
def on_test_start(**kwargs):
print(kwargs['environment'])
print('test is start')
class User(HttpUser):
@task
def test_01(self):
print('test_01')
self.stop()
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_init.py -u 4 -r 4 -t 2 --headless --host https://www.baidu.com --only-summary
<locust.env.Environment object at 0x106bd5d50>
<locust.runners.LocalRunner object at 0x106bd5b40>
None
test init fire
[2023-08-04 12:19:06,528] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Run time limit set to 2 seconds
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
<locust.env.Environment object at 0x106bd5d50>
test is start
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 4 users at a rate of 4.00 per second
[2023-08-04 12:19:06,529] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"User": 4} (4 total users)
test_01
test_01
test_01
test_01
[2023-08-04 12:19:08,443] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: --run-time limit reached, shutting down
[2023-08-04 12:19:08,444] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Shutting down (exit code 0)
init_command_line_parser
自定义locust运行命令行参数,例子
import os
import sys
from locust import task, HttpUser, events
@events.init_command_line_parser.add_listener
def _(parser):
# "--custom-argument" 即命令行参数名
# type 指定入参类型
# env_var 指本地环境变量中的key值
# env_var 、 default 和 命令行输入的参数之间的关系是,若输入该参数并传入非空值,则使用该值,若是空值,则从环境变量中查找env_var值(os.environ[env_var])
# 入参为空,环境变量os.environ.get(env_var)也是空,才使用default值
parser.add_argument("--custom-argument", type=str, env_var="HOME", default="kami", help="自己自定义的命令行参数")
parser.add_argument("--custom-argument2", type=str, env_var="CA2", default="kami2", help="自己自定义的命令行参数")
parser.add_argument("--custom-argument3", type=str, env_var="HOME", default="kami3", help="自己自定义的命令行参数")
class WebsiteUser(HttpUser):
host = 'https://www.baidu.com'
@task
def my_task(self):
print(f"HOME={os.environ.get('HOME')}")
print(f"CA2={os.environ.get('CA2')}")
print(f"custom_argument={self.environment.parsed_options.custom_argument}")
print(f"custom_argument2={self.environment.parsed_options.custom_argument2}")
print(f"custom_argument3={self.environment.parsed_options.custom_argument3}")
sys.exit(1)
(mylocust) kamiyeung@HUAWEI-MateBook-X-Pro mylocust % locust -f locustfiles/hook_init_command_line_parser.py --headless --custom-argument3 "custom-argument3 传参数了" --only-summary
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: No run time limit set, use CTRL+C to interrupt
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.main: Starting Locust 2.15.1
[2023-08-04 18:10:11,362] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: Ramping to 1 users at a rate of 1.00 per second
[2023-08-04 18:10:11,363] HUAWEI-MateBook-X-Pro.local/INFO/locust.runners: All users spawned: {"WebsiteUser": 1} (1 total users)
HOME=/Users/kamiyeung
CA2=None
custom_argument=/Users/kamiyeung
custom_argument2=kami2
custom_argument3=custom-argument3 传参数了