如何在不使用上下文管理器的情况下避免在 Locust 中记录请求?

How to avoid logging request in Locust without using context manager?

Locust documentation 解释了可以通过使用上下文管理器并引发异常来阻止请求被记录。例如:

try:
    with self.client.get('/wont_be_logged', catch_response=True) as response:
        raise RuntimeError
catch RuntimeError
    pass

有没有不用上下文管理器就能达到同样目的的方法?

自己做请求(不使用self.client

例如使用requests.get(...)

(请注意,这将使用不同的会话,因此不会使用相同的 cookie 或基础 http 连接)

不会影响 cyberwiz 的回答,因为最终您可以执行自己的请求并获得相同的行为,但如果您真的只想使用 Locust 的客户端而不是自己管理另一个客户端,则可以。 catch_response=True 应该足以让它不会自动触发 successfailure 事件。之后您可以手动触发事件。

演示 Locust 文件:

from locust import HttpUser
from locust.user.task import task


class TestUser(HttpUser):

    host = "http://localhost:8089"

    @task
    def test_call(self):
        r = self.client.get("/", catch_response=True)
        print("Test")
        print(r.elapsed)
        self.environment.events.request_success.fire(
            request_type="POST",
            name="/somewhere_new",
            response_time=r.elapsed.microseconds / 1000,
            response_length=13,
        )

同样,这样做并不能节省多少它的工作原理。