From 860cd2677e13492a0b0b190a2f9f6ac631a1ced0 Mon Sep 17 00:00:00 2001 From: myzhan Date: Thu, 11 Jul 2019 13:45:22 +0800 Subject: [PATCH 1/2] stop looking for proxy settings --- docs/quickstart.rst | 4 ++++ locust/core.py | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index b6d8dddca0..91f7f6635e 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -51,6 +51,10 @@ The :py:class:`HttpLocust ` class inherits from the :py:class:`Locust ` class, and it adds a client attribute which is an instance of :py:class:`HttpSession ` that can be used to make HTTP requests. +By default, we stop looking for proxy settings to improve performance. If you really want the test requests +go through a HTTP proxy, you can inherit from the :py:class:`HttpLocust ` class and +set the trust_env field to True. For further details, refer to the documentation of requests. + Another way we could declare tasks, which is usually more convenient, is to use the ``@task`` decorator. The following code is equivalent to the above: diff --git a/locust/core.py b/locust/core.py index aa429b3486..d074c66e81 100644 --- a/locust/core.py +++ b/locust/core.py @@ -187,13 +187,22 @@ class HttpLocust(Locust): Instance of HttpSession that is created upon instantiation of Locust. The client support cookies, and therefore keeps the session between HTTP requests. """ + + trust_env = False + """ + Look for proxy settings will slow down the default http client. + It's the default behavior of the requests library. + We don't need this feature most of the time, so disable it by default. + """ def __init__(self): super(HttpLocust, self).__init__() if self.host is None: raise LocustError("You must specify the base host. Either in the host attribute in the Locust class, or on the command line using the --host option.") - - self.client = HttpSession(base_url=self.host) + + session = HttpSession(base_url=self.host) + session.trust_env = self.trust_env + self.client = session class TaskSetMeta(type): From c266c1d24aeebdae57b2580021a99906cefbe97b Mon Sep 17 00:00:00 2001 From: myzhan Date: Fri, 12 Jul 2019 11:25:25 +0800 Subject: [PATCH 2/2] The send_file returns response with proper content length now, so we need to remove it explicitly. --- locust/test/testcases.py | 1 + 1 file changed, 1 insertion(+) diff --git a/locust/test/testcases.py b/locust/test/testcases.py index 5e997d7419..88645a8f4f 100644 --- a/locust/test/testcases.py +++ b/locust/test/testcases.py @@ -80,6 +80,7 @@ def no_content_length(): r = send_file(BytesIO("This response does not have content-length in the header".encode('utf-8')), add_etags=False, mimetype='text/plain') + r.headers.remove("Content-Length") return r @app.errorhandler(404)