HTTP 请求
创建新的负载测试时,首先要定义您想要测试的 HTTP 请求。
发出 HTTP 请求
GET 请求示例如下:
import http from 'k6/http';
export default function () {
http.get('http://test.k6.io');
}
对于稍微复杂一些的场景,此示例展示了带有电子邮件/密码认证载荷的 POST 请求
import http from 'k6/http';
export default function () {
const url = 'http://test.k6.io/login';
const payload = JSON.stringify({
email: 'aaa',
password: 'bbb',
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
http.post(url, payload, params);
}
可用方法
该http 模块处理各种 HTTP 请求和方法。
名称 | 值 |
---|---|
batch() | 并行发出多个 HTTP 请求(例如浏览器通常会这样做)。 |
del() | 发出 HTTP DELETE 请求。 |
get() | 发出 HTTP GET 请求。 |
head() | 发出 HTTP HEAD 请求。 |
options() | 发出 HTTP OPTIONS 请求。 |
patch() | 发出 HTTP PATCH 请求。 |
post() | 发出 HTTP POST 请求。 |
put() | 发出 HTTP PUT 请求。 |
request() | 发出任何类型的 HTTP 请求。 |
跟随重定向
默认情况下,k6 会自动跟随一定数量的重定向,然后停止并返回最后一个响应。您可以使用以下方法进行自定义:
- 使用
maxRedirects
选项全局自定义重定向数量。请参阅最大重定向了解更多详情。 - 使用
Params.redirects
属性自定义特定请求的重定向数量,这将覆盖maxRedirects
选项。请参阅Params了解更多详情。
HTTP 请求标签
k6 会自动应用标签到您的 HTTP 请求。您可以使用这些标签来过滤结果并组织分析。
名称 | 描述 |
---|---|
expected_response | 默认情况下,响应状态码在 200 到 399 之间为 true 。使用setResponseCallback更改默认行为。 |
group | 当请求在group中运行时,标签值为组名。默认为空。 |
name | 默认为请求的 URL |
method | 请求方法(GET , POST , PUT 等) |
scenario | 当请求在scenario中运行时,标签值为场景名称。默认为 default 。 |
status | 响应状态码 |
url | 默认为请求的 URL |
以下代码片段展示了测试结果数据点的 JSON 示例。在此示例中,指标是 HTTP 请求的持续时间。
请注意 tags
对象如何对数据进行分组。
{
"type": "Point",
"metric": "http_req_duration",
"data": {
"time": "2017-06-02T23:10:29.52444541+02:00",
"value": 586.831127,
"tags": {
"expected_response": "true",
"group": "",
"method": "GET",
"name": "http://test.k6.io",
"scenario": "default",
"status": "200",
"url": "http://test.k6.io"
}
}
}
将 URL 分组到同一个标签下
默认情况下,标签有一个 name
字段,其中包含请求 URL 的值。如果您的测试具有动态 URL 路径,您可能不希望出现此行为,这可能会导致指标流中出现大量唯一的 URL。例如,以下代码访问 100 个不同的 URL
import http from 'k6/http';
export default function () {
for (let id = 1; id <= 100; id++) {
http.get(`http://example.com/posts/${id}`);
}
}
// tags.name=\"http://example.com/posts/1\",
// tags.name=\"http://example.com/posts/2\",
您可能更希望将这些数据报告为单个指标:要聚合动态 URL 的数据,请显式设置一个 name
标签
import http from 'k6/http';
export default function () {
for (let id = 1; id <= 100; id++) {
http.get(`http://example.com/posts/${id}`, {
tags: { name: 'PostsItemURL' },
});
}
}
// tags.name=\"PostsItemURL\",
// tags.name=\"PostsItemURL\",
该代码会产生如下所示的 JSON 输出
// For http://example.com/1, note that the url is not present in the JSON.
{
"type":"Point",
"metric":"http_req_duration",
"data": {
"time":"2017-06-02T23:10:29.52444541+02:00",
"value":586.831127,
"tags": {
"method":"GET",
"name":"PostsItemURL",
"status":"200",
"url":"PostsItemURL"
}
}
}
// and for http://example.com/2
{
"type":"Point",
"metric":"http_req_duration",
"data": {
"time":"2017-06-02T23:10:29.58582529+02:00",
"value":580.839273,
"tags": {
"method":"GET",
"name":"PostsItemURL",
"status":"200",
"url":"PostsItemURL"
}
}
}
请注意,尽管这两个对象具有不同的 URL,但它们的 name
相同。如果您按标签 name: PostsItemURL
过滤结果,结果将包含来自所有 100 个 URL 的所有数据点。
作为替代方案,您也可以使用 http.url
包装器通过字符串模板值设置 name
标签
import http from 'k6/http';
export default function () {
for (let id = 1; id <= 100; id++) {
http.get(http.url`http://example.com/posts/${id}`);
}
}
// tags.name="http://example.com/posts/${}",
// tags.name="http://example.com/posts/${}",