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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,347评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,435评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,509评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,611评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,837评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,987评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,730评论 0 267
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,194评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,525评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,664评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,334评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,944评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,764评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,997评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,389评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,554评论 2 349

推荐阅读更多精彩内容