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

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