我正在尝试在 js 中为 k6 工具脚本创建一个函数,这将使我能够为“交易名称”创建多种类型的指标作为输入,然后创建另一个函数来填充这些指标。这将有助于避免为不同的事务名称编写类似的代码,并有助于保持指标名称的一致性。
// line of code to define the metrics
let Search_RT_Trend = new Trend("Search_duration");
let Search_PassRate = new Rate("Search_PassRate");
let Search_PassCount = new Counter("Search_PassCount");
let Search_FailCount = new Counter("Search_FailCount");
// line of code populating the data in metrics
Search_RT_Trend.add(res.timings.duration);
Search_PassRate.add(1);
Search_PassCount.add(1);
Search_FailCount.add(1);
希望创建两个函数来接受交易名称的输入,可能如下:
CreateMetric ("Search")
PopulateMetric ("Search")
如何实现这一点?
最佳答案
类似这样的吗?
function MetaMetric(name) {
this.RT_Trend = new Trend(`${name}_duration`);
this.PassRate = new Rate(`${name}_PassRate`);
this.PassCount = new Counter(`${name}_PassCount`);
this.FailCount = new Counter(`${name}_FailCount`);
}
MetaMetric.prototype.track = function (req) {
this.RT_Trend.add(req.timings.duration);
if (req.timings.duration < 200 /* or whatever */) {
this.PassRate.add(1);
this.PassCount.add(1);
} else {
this.PassRate.add(0);
this.FailCount.add(1);
}
};
let myMetaMetric = new MetaMetric("Search")
export default function () {
let resp = http.get("https://httpbin.test.loadimpact.com/");
myMetaMetric.track(resp);
sleep(3 * Math.random());
}
需要考虑的一些事项:
- 当您拥有
Rate
指标时,您就不需要通过和失败Counter
指标。比率
本质上是通过与失败之间的比率,所以它基本上是这两个计数器的组合:) - 您可能会找到 k6 checks和 thresholds有用。
关于javascript - 用于创建和填充 k6 指标的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57214244/