【问题标题】:How to configure Locust to use https?如何配置 Locust 以使用 https?
【发布时间】:2021-01-13 21:26:54
【问题描述】:

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

locustfile.py

import time
from locust import HttpUser, task

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

我的.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 不起作用。而且你没有提供很多关于你实际尝试做的事情和你看到的事情的信息。如果您直接运行,您的 locustfile.py 是否有效?你在运行分布式吗?如果是这样,你是如何管理工人的?

标签: python http openssl load-testing locust


【解决方案1】:

Locust 可以很好地使用 https URL,如果您只想使用 https 访问端点,则无需进行任何类型的配置,这就是为什么文档没有说明如何操作的原因;对于基本操作,无需配置。您甚至不必使用那些 tls-cert 和 tls-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),或者您可以specify your self-signed cert供客户端验证。在这种情况下,您可以像以前一样在配置中使用tls-cert 选项。确保包含它的路径。你可能想要.pem 而不是.crt。

【讨论】:

  • 所以我应该只刮掉 tls 的配置选项,它应该可以工作吗?我这样做了,但引用 ssl 问题的损失率是 100%。我相应地更新了问题
  • 更新了我的答案以解决您的更新问题。
  • 所以为了减少失败,我必须使用self.client.get("/", verify="my.crt") 并保留其余部分?
  • 对此进行了测试,但它没有按预期工作。我预计它会开始接受我的 http 请求,但测试没有得到牵引,似乎忽略了get()
  • 你试过self.client.get("/", verify=False)吗?您可以先这样做以确保其他一切正常,然后弄清楚如何让证书得到验证。确保您使用的证书是公钥(通常是.pem); .crt 通常比公钥拥有更多,所以我不肯定它会在这里工作。
【解决方案2】:

添加到 Solowalkers 答案:

如果有人想复制我的发现。

Locusts request method 在verify=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(命名并不重要。但是.confs 有一个级联加载顺序。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 如果命名为 locustfile.py,Locust 会自行查找。

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多