我目前正在使用环境变量将自定义参数传递给我的负载测试.例如,我的测试类看起来像这样:
from locust import HttpLocust, TaskSet, task
import os
class UserBehavior(TaskSet):
@task(1)
def login(self):
test_dir = os.environ['BASE_DIR']
auth=tuple(open(test_dir + '/PASSWORD).read().rstrip().split(':'))
self.client.request(
'GET',
'/myendpoint',
auth=auth
)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
Run Code Online (Sandbox Code Playgroud)
然后我正在运行我的测试:
locust -H https://myserver --no-web --clients=500 --hatch-rate=500 --num-request=15000 --print-stats --only-summary
Run Code Online (Sandbox Code Playgroud)
有没有更多locust方法可以将自定义参数传递给locust命令行应用程序?
Cyb*_*wiz 12
现在可以向 Locust 添加自定义参数(最初提出这个问题时这是不可能的,此时使用环境变量可能是最好的选择)。
从 2.2 版本开始,自定义参数甚至会转发给分布式运行中的工作人员。
https://docs.locust.io/en/stable/extending-locust.html#custom-arguments
from locust import HttpUser, task, events
@events.init_command_line_parser.add_listener
def _(parser):
parser.add_argument("--my-argument", type=str, env_var="LOCUST_MY_ARGUMENT", default="", help="It's working")
# Set `include_in_web_ui` to False if you want to hide from the web UI
parser.add_argument("--my-ui-invisible-argument", include_in_web_ui=False, default="I am invisible")
@events.test_start.add_listener
def _(environment, **kw):
print("Custom argument supplied: %s" % environment.parsed_options.my_argument)
class WebsiteUser(HttpUser):
@task
def my_task(self):
print(f"my_argument={self.environment.parsed_options.my_argument}")
print(f"my_ui_invisible_argument={self.environment.parsed_options.my_ui_invisible_argument}")
Run Code Online (Sandbox Code Playgroud)
你可以在locust脚本中使用like env <parameter>=<value> locust <options>和use <parameter>来使用它的值
例如,
env IP_ADDRESS=100.0.1.1 locust -f locust-file.py --no-web --clients=5 --hatch-rate=1 --num-request=500在locust脚本中使用IP_ADDRESS来访问其值,在本例中为100.0.1.1.
| 归档时间: |
|
| 查看次数: |
5653 次 |
| 最近记录: |