【问题标题】:How to avoid logging request in Locust without using context manager?如何在不使用上下文管理器的情况下避免在 Locust 中记录请求?
【发布时间】:2021-03-15 20:35:16
【问题描述】:

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

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

有没有办法在不使用上下文管理器的情况下达到同样的效果?

【问题讨论】:

    标签: python locust


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      演示蝗虫文件:

      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,
              )
      

      再一次,这样做并不能节省太多。

      【讨论】:

      • 所以只使用catch_response=True 会使请求不被记录;不需要像我最初想的那样使用 with 语句。
      • 嗯。我没想到。我认为这种行为是一个错误(我什至尝试使用 catch_response=True 而不使用 with 块抛出异常,以帮助人们避免意外地这样做并在这种意外情况下着陆,但还没有找到方法)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 2023-01-18
      • 1970-01-01
      • 2017-09-11
      • 2017-05-07
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多