如何以编程方式调用 Locust 测试?
How to invoke Locust tests programmatically?
在我的本地主机 (127.0.0.1:8089) 上使用 Locust 进行了尝试,但它给出了 400 错误请求错误:
import requests
response = requests.post('http://127.0.0.1:8089/swarm', params={"locust_count":10, "hatch_rate":5})
print response.text
回复:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
我已经验证 http://127.0.0.1:8089/ is indeed up by browsing it from the browser. The code for locustfile.py
is available at this post。
如果您想从您的代码中启动 locust,那么您有 3 个选择:
(不是首选但有效)您可以 运行 Locust 使用 CLI(--no-web、-c、-r)
https://docs.locust.io/en/latest/running-locust-without-web-ui.html
自己实现locust加载逻辑:
https://github.com/locustio/locust/issues/222(使用消息线程中提供的代码)
第一个选项,尽管不是最多 "pythonic" 是最容易做到的。我仍在尝试第二种选择。
我发现这个解决方案最简单:
import os
from locust.main import main
def run_locust(**kwargs):
os.environ['LOCUST_HOST'] = kwargs.get('LOCUST_HOST')
os.environ['LOCUST_RUN_TIME'] = kwargs.get('LOCUST_RUN_TIME', '1m')
os.environ['LOCUST_NO_WEB'] = str(kwargs.get('LOCUST_NO_WEB', True))
os.environ['LOCUST_LOCUSTFILE'] = kwargs.get('LOCUST_LOCUSTFILE')
os.environ['LOCUST_CLIENTS'] = str(kwargs.get('LOCUST_CLIENTS'))
os.environ['LOCUST_HATCH_RATE'] = str(kwargs.get('LOCUST_HATCH_RATE'))
main()
根据 Locust 文档,配置参数也可以作为环境变量传递。因此,我们可以像我在上面的例子中展示的那样使用它们。
首先,我们使用 os.environ[<variable_name>]
设置所需的环境变量,其次,我们 运行 locust 的主要功能 main()
。
文档:https://docs.locust.io/en/stable/configuration.html#all-available-configuration-options
在我的本地主机 (127.0.0.1:8089) 上使用 Locust 进行了尝试,但它给出了 400 错误请求错误:
import requests
response = requests.post('http://127.0.0.1:8089/swarm', params={"locust_count":10, "hatch_rate":5})
print response.text
回复:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
我已经验证 http://127.0.0.1:8089/ is indeed up by browsing it from the browser. The code for locustfile.py
is available at this post。
如果您想从您的代码中启动 locust,那么您有 3 个选择:
(不是首选但有效)您可以 运行 Locust 使用 CLI(--no-web、-c、-r) https://docs.locust.io/en/latest/running-locust-without-web-ui.html
自己实现locust加载逻辑: https://github.com/locustio/locust/issues/222(使用消息线程中提供的代码)
第一个选项,尽管不是最多 "pythonic" 是最容易做到的。我仍在尝试第二种选择。
我发现这个解决方案最简单:
import os
from locust.main import main
def run_locust(**kwargs):
os.environ['LOCUST_HOST'] = kwargs.get('LOCUST_HOST')
os.environ['LOCUST_RUN_TIME'] = kwargs.get('LOCUST_RUN_TIME', '1m')
os.environ['LOCUST_NO_WEB'] = str(kwargs.get('LOCUST_NO_WEB', True))
os.environ['LOCUST_LOCUSTFILE'] = kwargs.get('LOCUST_LOCUSTFILE')
os.environ['LOCUST_CLIENTS'] = str(kwargs.get('LOCUST_CLIENTS'))
os.environ['LOCUST_HATCH_RATE'] = str(kwargs.get('LOCUST_HATCH_RATE'))
main()
根据 Locust 文档,配置参数也可以作为环境变量传递。因此,我们可以像我在上面的例子中展示的那样使用它们。
首先,我们使用 os.environ[<variable_name>]
设置所需的环境变量,其次,我们 运行 locust 的主要功能 main()
。
文档:https://docs.locust.io/en/stable/configuration.html#all-available-configuration-options