我在 Mac OS ventura 13.2.1 上使用 k6 版本 k6 v0.43.1((开发)、go1.20.1、darwin/amd64),我的要求是让组为请求计时。我正在尝试这个示例脚本
import http from 'k6/http';
export default function () {
group('my-group', function () {
http.get('https://test.k6.io');
console.log(group.duration('my-group'));
});
}```
but everytime getting either ''TypeError: Value is not an object: undefined" error or “TypeError: Object has no member ‘duration’”
Please help.
group
是一项功能,可让您组织测试脚本并自动记录指标。指标用 group
标记,并将组名作为值。嵌套组用 ::
. 分隔它们的组名
您无法直接在脚本中读取单个组执行的持续时间。
如果您需要特定组的持续时间指标,请将它们添加到您的阈值定义中:
import { group } from 'k6';
import http from 'k6/http';
export const options = {
thresholds: {
'group_duration{group:::my-group}': [],
},
};
export default function () {
group('my-group', function () {
http.get('https://test.k6.io');
});
}
这将在您的测试结束摘要中打印以下行:
group_duration.................: avg=363.79ms min=363.79ms med=363.79ms max=363.79ms p(90)=363.79ms p(95)=363.79ms
{ group:::my-group }.........: avg=363.79ms min=363.79ms med=363.79ms max=363.79ms p(90)=363.79ms p(95)=363.79ms
如果你真的需要单个代码路径的存活时间,你必须自己测量时间:
export default function() {
const start = Date.now();
// ... your logic here ...
const duration = Date.now() - start;
console.log(`Duration: ${duration}`);
}
当然,完全可以定义您自己的自定义函数来简化此类测量:
function measure(fn) {
const start = Date.now();
fn(); // optionally wrap in a group here
return Date.now() - start;
}
export default function() {
const duration = measure(function () {
http.get('https://test.k6.io');
});
console.log(`Duration: ${duration}`);
}