我有一个 K6 测试,其中对流程进行负载测试,并且我正在对每个页面请求进行分组。我想做的是检查每个组的持续时间,比如
group('Home page', function(){
http.get('localhost:8080')
});
//something like this
check(group.duration, function()=>{'performs check logic'})
group('search page', function(){
http.get('localhost:8080/Search?term=mysearch')
})
check(group.duration, function()=>{'performs check logic'})
文档指出 group 发出 group_duration 但没有说明如何监听它。 JS 不强,所以如果这很简单,我深表歉意。
最佳答案
群组目前不返回其持续时间。
在这种情况下发出意味着如果您使用 json output/influxdb/stastsd/load impact's cloud 进行度量存储,而不是将发送组持续时间的度量,您可以稍后...从内部做任何您想做的事度量存储。
check
的语法也是
check(variable_name_to_use, map_of_name_to_function_to_calls)
类似
check(group.duraiton, {
"checking duration is less than 20 as normal function": function(d){ return d<20},
"checking duration is less than 20 as arrow function": (d)=>{ return d<20},
"checking duration is less than 20 as arrow function without the brackets": (d)=>d<20,
})
使用 http.*
时,您还需要提供 url 的方案。
我还想指出 group
的想法是测量给定的请求或操作组(!)需要多少。例如,它们可用于对构成您的“登录”、“将商品添加到购物车”、“结帐和付款”等的所有操作进行分组。它们绝对有用,但是当您的所有组都是单个请求时对单个请求进行分组不会添加任何新数据:)
解决方案 1: 你可以像这样自己做:
import { check, group } from "k6";
import http from 'k6/http';
export default function() {
var group_duration;
group('Home page', function(){
var start = new Date();
http.get('http://localhost:8080');
group_duration = new Date() - start;
});
check(group_duration, {
"Name of group check": function(d){return d<200}
})
group('search page', function(){
var start = new Date();
http.get('http://localhost:8080/Search?term=mysearch');
group_duration = new Date() - start;
})
console.log(group_duration);
check(group_duration, {
"Name of group check": (d)=>d<200
})
}
您当然可以将其移动到一个函数中,并将持续时间返回为:
function mygroup(name, f) {
var start = new Date();
group(name, f);
return new Date() - start;
}
然后调用 mygroup
并获取持续时间
var group_duration = mygroup('Home page', function(){
http.get('http://localhost:8080');
});
解决方案 2: 如果你在每个组中只使用一个请求,你可以使用请求确实返回他们花费了多少的事实。所以
resp = http.get("google.com");
check (resp.timings.duration, {
"check response time", (d) => d<200
});
将检查给定的请求花费的时间是否少于 200 毫秒。您可以在 docs 的响应中看到更多内容
作为一名 k6 开发人员,我认为你提出的建议不是一个坏主意,我希望你真的 open an issue
关于k6 - 如何获取组的组持续时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56262646/