即时负载增加
负载测试工具的一种常见用法是所谓的阶梯式到达率。
在 k6 中,您可以使用 options
对象,并在不同的场景中配置迭代次数或 VU 数量来实现这一点。
以下是关于如何即时增加迭代次数并保持一段时间的示例。
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
scenarios: {
contacts: {
executor: 'ramping-arrival-rate',
preAllocatedVUs: 50,
timeUnit: '1s',
startRate: 50,
stages: [
{ target: 200, duration: '30s' }, // linearly go from 50 iters/s to 200 iters/s for 30s
{ target: 500, duration: '0' }, // instantly jump to 500 iters/s
{ target: 500, duration: '10m' }, // continue with 500 iters/s for 10 minutes
],
},
},
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
以下是关于如何即时增加 VU 数量并保持一段时间的示例。
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
scenarios: {
contacts: {
executor: 'ramping-vus',
startVUs: 3,
stages: [
{ target: 20, duration: '30s' }, // linearly go from 3 VUs to 20 VUs for 30s
{ target: 100, duration: '0' }, // instantly jump to 100 VUs
{ target: 100, duration: '10m' }, // continue with 100 VUs for 10 minutes
],
},
},
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
}