HTTP 使用者
概觀
LoadDensity 內含兩個 HTTP user 模板,皆透過 request_executor 與 scenario_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,其餘欄位忽略。
欄位 |
意義 |
|---|---|
|
|
|
目標 URL(絕對或相對 |
|
Locust 事件名;預設等同 URL。 |
|
請求 headers dict。 |
|
Query string 參數(dict 或 list of pairs)。 |
|
以 JSON 序列化的 body。 |
|
Form-encoded body(dict / list / str)。 |
|
cookies dict。 |
|
單請求 timeout(秒)。 |
|
直接轉送 client。 |
|
|
|
回應斷言(見 斷言與擷取)。 |
|
回應擷取(見 參數解析器)。 |
|
情境流程控制(見 情境模式)。 |
範例
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}]},
],
)