From 4ffa84a267e5ca4f380e3b7ead520f07a9181dde Mon Sep 17 00:00:00 2001 From: Andreas Grub Date: Mon, 18 Mar 2024 21:08:01 +0100 Subject: [PATCH] Do not call blocking content property and lazily load response on first access This reverts commit 7ce89a00 and improves it --- locust/contrib/fasthttp.py | 5 ++++- locust/test/test_fasthttp.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/locust/contrib/fasthttp.py b/locust/contrib/fasthttp.py index 66980c9247..47ef9b6408 100644 --- a/locust/contrib/fasthttp.py +++ b/locust/contrib/fasthttp.py @@ -578,7 +578,10 @@ class ResponseContextManager(FastResponse): def __init__(self, response, environment, request_meta): # copy data from response to this object self.__dict__ = response.__dict__ - self._cached_content = response.content + try: + self._cached_content = response._cached_content + except AttributeError: + pass # store reference to locust Environment self._environment = environment self.request_meta = request_meta diff --git a/locust/test/test_fasthttp.py b/locust/test/test_fasthttp.py index 93af2b603e..aea774d3d2 100644 --- a/locust/test/test_fasthttp.py +++ b/locust/test/test_fasthttp.py @@ -71,6 +71,28 @@ def test_streaming_response(self): # download the content of the streaming response (so we don't get an ugly exception in the log) _ = r.content + def test_streaming_response_catch_response(self): + """ + Test a request to an endpoint that returns a streaming response, and uses catch_response + """ + s = self.get_client() + + with s.get("/streaming/30", stream=True, catch_response=True) as r: + # typical usage of r when stream=True is to read the stream as desired, + # with the possibility to "fail fast" when some things are read early on + response_content = str(r.stream.read()) + r.failure("some error") + + self.assertRegex(response_content, "streaming response") + + stats = self.runner.stats.get("/streaming/30", "GET") + self.assertEqual(1, stats.num_requests) + self.assertEqual(1, stats.num_failures) + + # verify that response time does NOT include whole download time, when using stream=True + self.assertGreaterEqual(stats.avg_response_time, 0) + self.assertLess(stats.avg_response_time, 250) + def test_slow_redirect(self): s = self.get_client() url = "/redirect?url=/redirect&delay=0.5"