Locust 官方文档 6:使用更快的 HTTP 客户端提高 Locust 性能

Locust’s default HTTP client uses python-requests.

Locust 默认的 HTTP 客户端使用 python-requests.

The reason for this is that requests is a very well-maintained python package, that provides a really nice API, that many python developers are familiar with.

原因是 requests 库是一个维护良好的 python 程序库,它提供了许多 python 开发人员都熟悉的优雅的 API。

Therefore, in many cases, we recommend that you use the default HttpUser which uses requests.

因此,在很多案例中,我们推荐使用默认的 HttpUser 类,它使用的是 requests 库实现的。

However, if you’re planning to run really large scale tests, Locust comes with an alternative HTTP client, FastHttpUser which uses geventhttpclient instead of requests.

然而,如果你打算运行真正的大规模负载测试,那么 Locust 附带了一个备用 HTTP 客户端 FastHttpUser ,它使用的是 geventhttpclient

This client is significantly faster, and we’ve seen 5x-6x performance increases for making HTTP-requests.

使用 geventhttpclient 客户端的速度能获得明显提高,我们发现 HTTP 请求的性能提高了 5 到 6 倍。

This does not necessarily mean that the number of users one can simulate per CPU core will automatically increase 5x-6x, since it also depends on what else the load testing script does.

这并不一定意味着每个 CPU 内核可以模拟的用户数量会自动增加 5 到 6 倍,因为这还取决于负载测试脚本的其他功能(如果测试脚本中有大量处理数据等其他逻辑,也会影响 Locust 的性能)。

However, if your locust scripts are spending most of their CPU time in making HTTP-requests, you are likely to see significant performance gains.

但是,如果 Locust 脚本花费大量的 CPU 时间进行 HTTP 请求,则可能会看到明显的性能提升。

怎么使用 FastHttpUser

Subclass FastHttpUser instead of HttpUser:

自定义的 User 类 直接继承 FastHttpUser 代替 HttpUser。

from locust import task, between
from locust.contrib.fasthttp import FastHttpUser

class MyUser(FastHttpUser):
    wait_time = between(2, 5)

    @task
    def index(self):
        response = self.client.get("/")

Note

Because FastHttpUser uses a different client implementation with a slightly different API, it may not always work as a drop-in replacement for HttpUser.

注意:

因为 FastHttpUser 使用不同的 API 实现客户端,所以它并不是在所有清下都与 HttpUser 一致。

API

FastHttpUser class

  • class FastHttpUser ( environment )

FastHttpUser uses a different HTTP client (geventhttpclient) compared to HttpUser (python-requests). It’s significantly faster, but not as capable.

与 HttpUser(python requests)相比,FastHttpUser 使用不同的 HTTP 客户端(gevent http client)。它的速度要快得多,但功能没有 HttpUser 强大。

The behaviour of this user is defined by it’s tasks.

该用户的行为由其任务定义。

Tasks can be declared either directly on the class by using the @task decorator on the methods, or by setting the tasks attribute.

可以使用 @task 装饰器标识任务,或者用 tasks 属性来指定任务。

This class creates a client attribute on instantiation which is an HTTP client with support for keeping a user session between requests.

此类在实例化时创建一个 client 属性,该属性是一个 HTTP client,支持在请求之间保持用户会话。

  • connection_timeout= 60.0

Parameter passed to FastHttpSession

连接超时时间参数

  • insecure = True

Parameter passed to FastHttpSession. Default True, meaning no SSL verification.

默认值为 True,表示不进行 SSL 验证。

  • max_redirects = 5

Parameter passed to FastHttpSession. Default 5, meaning 4 redirects.

运行最大的重定向次数。默认值 5,表示 4 次重定向

  • max_retries= 1

Parameter passed to FastHttpSession. Default 1, meaning zero retries.

最大重试次数,默认为 1,表示不重试

  • network_timeout= 60.0

Parameter passed to FastHttpSession

网络超时时间

FastHttpSession class

classFastHttpSession ( environment: locust.env.Environment, base_url: str, insecure=True, **kwargs )

  • get(path, kwargs)Sends a GET requesthead(path, **kwargs)

Sends a HEAD request

  • options(path, **kwargs)

Sends a OPTIONS request

  • patch(path, data=None, **kwargs)

Sends a POST request

  • post(path, data=None, **kwargs)

Sends a POST request

  • put(path, data=None, **kwargs)

Sends a PUT request

  • request(method: str, path: str, name: str = None, data: str = None, catch_response: bool = False, stream: bool = False, headers: dict = None, auth=None, json: dict = None, allow_redirects=True, **kwargs)

Send and HTTP request

Returns locust.contrib.fasthttp.FastResponse object.

Parameters:

method – method for the new Request object.

path – Path that will be concatenated with the base host URL that has been specified. Can also be a full URL, in which case the full URL will be requested, and the base host is ignored.

name – (optional) An argument that can be specified to use as label in Locust’s statistics instead of the URL path. This can be used to group different URL’s that are requested into a single entry in Locust’s statistics.

catch_response – (optional) Boolean argument that, if set, can be used to make a request return a context manager to work as argument to a with statement. This will allow the request to be marked as a fail based on the content of the response, even if the response code is ok (2xx). The opposite also works, one can use catch_response to catch a request and then mark it as successful even if the response code was not (i.e 500 or 404).

data – (optional) String/bytes to send in the body of the request

.json – (optional) Dictionary to send in the body of the request. Automatically sets Content-Type and Accept headers to “application/json”. Only used if data is not set.

headers – (optional) Dictionary of HTTP Headers to send with the request.

auth – (optional) Auth (username, password) tuple to enable Basic HTTP Auth.

stream – (optional) If set to true the response body will not be consumed immediately and can instead be consumed by accessing the stream attribute on the Response object. Another side effect of setting stream to True is that the time for downloading the response content will not be accounted for in the request time that is reported by Locust.

class FastResponse ( ghc_response, request=None, sent_request=None )

  • content

Unzips if necessary and buffers the received body. Careful with large files!

  • headers = None

Dict like object containing the response headers

  • json() → dictParses the response as json and returns a dict

  • text

Returns the text content of the response as a decoded string

Next Previous

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章