HTTP 使用者

概觀

LoadDensity 內含兩個 HTTP user 模板,皆透過 request_executorscenario_runner 連線:

  • http_user — 封裝 locust.HttpUser``(底層 ``requests)。

  • fast_http_user — 封裝 ``locust.FastHttpUser``(底層 geventhttpclient,吞吐高得多)。

高負載情境選 fast_http_user。需要 requests 特性或 middleware 時用 http_user

Task 欄位

每個 HTTP task 是 dict;runner 將下列欄位轉送底層 client,其餘欄位忽略。

欄位

意義

method

get / post / put / patch / delete / head / ``options``(不分大小寫)。

request_url / url

目標 URL(絕對或相對 host)。

name

Locust 事件名;預設等同 URL。

headers

請求 headers dict。

params

Query string 參數(dict 或 list of pairs)。

json

以 JSON 序列化的 body。

data

Form-encoded body(dict / list / str)。

cookies

cookies dict。

timeout

單請求 timeout(秒)。

allow_redirectsverifyfiles

直接轉送 client。

auth

{"type": "basic", "username": "...", "password": "..."}{"type": "bearer", "token": "..."}

assertions

回應斷言(見 斷言與擷取)。

extract

回應擷取(見 參數解析器)。

weightrun_ifskip_if

情境流程控制(見 情境模式)。

範例

from je_load_density import start_test

start_test(
    user_detail_dict={"user": "fast_http_user"},
    user_count=50,
    spawn_rate=10,
    test_time=60,
    variables={"base": "https://api.example.com"},
    tasks=[
        {"method": "post", "request_url": "${var.base}/login",
         "json": {"email": "u@example.com", "password": "secret"},
         "extract": [
             {"var": "auth", "from": "json_path", "path": "data.token"}
         ]},
        {"method": "get", "request_url": "${var.base}/profile",
         "headers": {"Authorization": "Bearer ${var.auth}"},
         "assertions": [{"type": "status_code", "value": 200}]},
    ],
)