如何配置 Locust 以使用 https?

How to configure Locust to use https?

我是 Locust 的新手,我正在尝试获取准系统 https GET 到 运行 请求服务器索引页。

locustfile.py

import time
from locust import HttpUser, task

class QuickstartUser(HttpUser):
    @task
    def index(self):
        self.client.get("/")

my.conf

locustfile = locustfile.py
headless = true
expect-workers = 5
host = https://localhost:8001/
users = 100
spawn-rate = 10
run-time = 10m
csv = out

要开始这个过程,我使用:locust --conf my.conf

我希望它能工作,但它似乎有身份验证问题。丢失率为100%

输出:

 Name                                                          # reqs      # fails  |     Avg     Min     Max  Median  |   req/s failures/s
--------------------------------------------------------------------------------------------------------------------------------------------
 GET //                                                           806 806(100.00%)  |     318      15     729     290  |   94.26   94.26
--------------------------------------------------------------------------------------------------------------------------------------------
 Aggregated                                                       806 806(100.00%)  |     318      15     729     290  |   94.26   94.26

Response time percentiles (approximated)
 Type     Name                                                              50%    66%    75%    80%    90%    95%    98%    99%  99.9% 99.99%   100% # reqs
--------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------|
 GET      //                                                                290    390    450    510    570    630    680    710    730    730    730    806
--------|------------------------------------------------------------|---------|------|------|------|------|------|------|------|------|------|------|------|
 None     Aggregated                                                        290    390    450    510    570    630    680    710    730    730    730    806


Error report
 # occurrences      Error
--------------------------------------------------------------------------------------------------------------------------------------------
 806                GET //: SSLError(MaxRetryError("HTTPSConnectionPool(host='localhost', port=8001): Max retries exceeded with url: // (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1123)')))"))
--------------------------------------------------------------------------------------------------------------------------------------------

如果您只想使用 https 访问端点,那么 Locust 可以很好地处理 https URL,而无需进行任何类型的配置,这就是为什么文档没有说明如何操作的原因;对于基本操作,无需配置。您甚至不必使用那些 tls-certtls-key 选项,因为这些选项用于强制使用特定的证书和密钥。如果那是你想要的,那太好了。它应该有效。

如果您想做一些更高级的事情,请查看此常见问题解答: https://github.com/locustio/locust/wiki/FAQ#control-headers-or-other-things-about-my-http-requests

这意味着您需要更准确地知道您要做什么。对于证书验证之类的东西,这可能会有所帮助:

https://2.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

编辑:

如果您在未指定证书时收到错误,则可以按预期工作。自签名证书不适用于生产环境,因此大多数合适的客户都会对此抛出错误。在这种情况下,由于您只是想获得概念证明,您可能希望在 .get() 调用中包含 verify=False(请参阅 Python Requests throwing SSLError for more details and warnings about keeping that around), or you can 以供客户验证。在这种情况下,您可以像以前一样在配置中使用 tls-cert 选项。确保包含它的路径。您可能想要 .pem 而不是 .crt

添加到 Solowalkers 答案:

万一有人想重现我的发现。

Locusts request methodverify=True 的情况下需要 cert 元组。 因此,如果您没有 .pem,请使用它。

locustfile.py(实际命名或使用 locust -f yourname.py

import time
from locust import HttpUser, task
import urllib3

# surpress verify warnings from urllib
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

class QuickstartUser(HttpUser):
    @task(10)
    def index(self):
        self.client.get('', verify=True, cert=('my.crt', 'my.key'))

client.conf(这样命名并不重要。但是 .conf 有一个级联加载顺序。Read the docs for more info.

locustfile = locustfile.py
headless = true
expect-workers = 5
host = https://localhost:8001/ # change for server
users = 1                      # one user for proof of concept
spawn-rate = 1
run-time = 10m
csv = out

从这些文件的目录开始:locust --conf client.conf Locust 找到 locustfile.py 本身,如果它被命名为