batch( requests )
将多个 HTTP 请求批量处理,以便通过多个 TCP 连接并行发出它们。要设置批次大小,请使用 batch per host 选项。
参数 | 类型 | 描述 |
---|---|---|
requests | 数组 | 对象 | 包含请求的数组或对象,可以是字符串或对象形式 |
请求定义
您有多种方式来构建批量请求
- 在数组的数组中
- 作为对象或对象数组
- 作为 URL 字符串数组
将批量请求定义为 URL 字符串是 GET 请求的快捷方式。您可以在对象中使用此 GET 快捷方式——命名一个键并为其指定一个 URL 值(请参阅后续章节了解示例语法)。
数组和对象
您可以使用以下参数定义指定为数组或对象的请求。
注意
当您将请求定义为数组时,必须使用特定的项目顺序。请注意
Position
列以了解正确的顺序。
数组位置 | 名称 | 类型 | 描述 |
---|---|---|---|
1 | method | 字符串 | 必需。请求的 HTTP 方法。GET、POST、PUT、PATCH、DELETE、HEAD 或 OPTION 之一。 |
2 | url | 字符串 / HTTP URL | 必需。要请求的 URL。 |
3 | body(可选) | 字符串 / 对象 / ArrayBuffer | 如果相关,则为请求体。如果不需要设置请求体但想设置最后一个 params 参数,可以设置为 null 。 |
4 | params(可选) | 对象 | 诸如认证、自定义头部和标签之类的Params。 |
字符串
如果您传递一个字符串值数组,k6 会自动将它们解析为一批 GET
请求,其中目标就是这些字符串的值。
返回值
类型 | 描述 |
---|---|
对象 | 返回的对象包含 Response 对象。 当用户将数组作为 requests 传入时,它是一个数组;当使用命名请求(见下文)时,它是一个带有字符串键的普通对象。 |
数组示例
此示例将三个 URL 批量处理为数组以进行并行获取
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const responses = http.batch([
['GET', 'https://test.k6.io', null, { tags: { ctype: 'html' } }],
['GET', 'https://test.k6.io/style.css', null, { tags: { ctype: 'css' } }],
['GET', 'https://test.k6.io/images/logo.png', null, { tags: { ctype: 'images' } }],
]);
check(responses[0], {
'main page status was 200': (res) => res.status === 200,
});
}
请求对象示例
此示例使用对象定义一批 POST 请求(同时在 Params 对象中为请求设置自定义 HTTP 头部)
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const req1 = {
method: 'GET',
url: 'https://quickpizza.grafana.com/api/get',
};
const req2 = {
method: 'DELETE',
url: 'https://quickpizza.grafana.com/api/delete',
};
const req3 = {
method: 'POST',
url: 'https://quickpizza.grafana.com/api/post',
body: JSON.stringify({ hello: 'world!' }),
params: {
headers: { 'Content-Type': 'application/json' },
},
};
const responses = http.batch([req1, req2, req3]);
// QuickPizza should return our POST data in the response body, so
// we check the third response object to see that the POST worked.
check(responses[2], {
'form data OK': (res) => JSON.parse(res.body)['hello'] == 'world!',
});
}
注意
在前面的示例中,
req1
可能在req2
或req3
之前发生。
字符串数组示例
此示例使用 URL 字符串数组发送一批 GET 请求。
import { check } from 'k6';
import http from 'k6/http';
export default function () {
const responses = http.batch(['http://test.k6.io', 'http://test.k6.io/pi.php']);
check(responses[0], {
'main page 200': (res) => res.status === 200,
});
check(responses[1], {
'pi page 200': (res) => res.status === 200,
'pi page has right content': (res) => res.body === '3.14',
});
}
带有命名属性的对象示例
最后,您还可以通过使用对象而不是数组作为 http.batch()
的参数来发送命名请求。此示例混合使用了字符串 URL 和请求对象。
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const requests = {
'front page': 'https://k6.io',
'features page': {
method: 'GET',
url: 'https://k6.io/features',
params: { headers: { 'User-Agent': 'k6' } },
},
};
const responses = http.batch(requests);
// when accessing results, we use the name of the request as index
// in order to find the corresponding Response object
check(responses['front page'], {
'front page status was 200': (res) => res.status === 200,
});
}