在我的本地主机 (127.0.0.1:8089) 上尝试使用 Locust,但它给出了 400 bad request 错误:
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/确实是通过浏览器浏览起来的。 locustfile.py
的代码可在 this post 获取。 .
最佳答案
我发现这个解决方案最简单:
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()
的main函数.
文档:https://docs.locust.io/en/stable/configuration.html#all-available-configuration-options
关于python - 如何以编程方式调用 Locust 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46397580/