utils
注意
此库的源代码可在 grafana/k6-jslib-utils GitHub 仓库中找到。
utils
模块包含许多在日常负载测试中有用的小型实用函数。
函数 | 描述 |
---|---|
check(val, sets, [tags]) | check 的直接替换,支持异步值。 |
randomIntBetween(min, max) | 给定范围内的随机整数 |
randomItem(array) | 给定数组中的随机项 |
randomString(length, [charset]) | 给定长度的随机字符串,可选择从自定义字符集中选取 |
uuidv4() | 字符串表示的随机 UUID v4 |
findBetween(content, left, right, [repeat]) | 提取两个包围字符串之间的字符串 |
normalDistributionStages(maxVUs, durationSeconds, [numberOfStages]) | 创建 Stages,为测试产生正态分布(钟形曲线)的 VU |
getCurrentStageIndex | 获取在 stages 数组选项中定义的当前运行 Stage 的索引。它只能用于支持 stages 选项的 Executors,例如 ramping-vus 或 ramping-arrival-rate。 |
tagWithCurrentStageIndex | 使用当前运行 Stage 的索引标记迭代中生成的所有指标。 |
tagWithCurrentStageProfile | 使用当前运行 Stage 的计算 Profile 标记迭代中生成的所有指标。 |
简单示例
import { sleep } from 'k6';
import http from 'k6/http';
import {
randomIntBetween,
randomString,
randomItem,
uuidv4,
findBetween,
} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
export default function () {
const res = http.post(`https://quickpizza.grafana.com/api/users`, {
username: `user_${randomString(10)}@example.com`, // random email address,
password: uuidv4(), // random password in form of uuid
});
// find a string between two strings to grab the username:
const username = findBetween(res.body, '"username":"', '"');
console.log('username from response: ' + username);
sleep(randomIntBetween(1, 5)); // sleep between 1 and 5 seconds.
}