英文:
K6 - How to add elements to an empty array and calculate the average?
问题
以下是已翻译的内容:
我正在编写一个测试来计算每个请求的平均传输时间。
import http from 'k6/http';
import { sleep } from 'k6';
import { check } from 'k6';
export let options = {
vus: 1,
duration: '5s',
};
let transferTimes = [];
export default function () {
const res = http.get('<url>');
check(res, {
'成功的响应': (resp) => resp.status === 200,
});
const transferTime = res.timings.duration;
transferTimes.push(transferTime);
console.log(`传输时间:${transferTime} 毫秒`);
}
export function teardown() {
console.log(`数组的长度:${transferTimes.length}`);
for (let i = 0; i < transferTimes.length; i++) {
console.log(`索引为 ${i} 的元素:${transferTimes[i]}`);
}
const sum = transferTimes.reduce((a, b) => a + b, 0);
console.log(`总和:${sum}`);
const averageTransferTime = sum / transferTimes.length;
console.log(`平均传输时间:${averageTransferTime} 毫秒`);
}
我得到的输出是:
.
.
.
INFO[0005] 传输时间:1.164713 毫秒 来源=控制台
INFO[0005] 传输时间:1.163952 毫秒 来源=控制台
INFO[0005] 数组的长度:0 来源=控制台
INFO[0005] 总和:0 来源=控制台
INFO[0005] 平均传输时间:NaN 毫秒 来源=控制台
由于某种原因,transferTimes.push(transferTime);
看起来不起作用。
还有什么其他方法可以让它正常工作?谢谢。
英文:
I am writing a test to calculate the average transfer time for each request.
import http from 'k6/http';
import { sleep } from 'k6';
import { check } from 'k6';
export let options = {
vus: 1,
duration: 5s,
};
let transferTimes = [];
export default function () {
const res = http.get(<url>);
check(res, {
'Successful response': (resp) => resp.status === 200,
});
const transferTime = res.timings.duration;
transferTimes.push(transferTime);
console.log(`Transfer time: ${transferTime} ms`);
}
export function teardown() {
console.log(`Length of the array: ${transferTimes.length}`);
for (let i = 0; i < transferTimes.length; i++) {
console.log(`Element at index ${i}: ${transferTimes[i]}`);
}
const sum = transferTimes.reduce((a, b) => a + b, 0);
console.log(`Sum: ${sum}`);
const averageTransferTime = sum / transferTimes.length;
console.log(`Average transfer time: ${averageTransferTime} ms`);
}
I am getting the output as:
.
.
.
INFO[0005] Transfer time: 1.164713 ms source=console
INFO[0005] Transfer time: 1.163952 ms source=console
INFO[0005] Length of the array: 0 source=console
INFO[0005] Sum: 0 source=console
INFO[0005] Average transfer time: NaN ms source=console
For some reason the transferTimes.push(transferTime);
doesn't seem to work.
What else can be done to get this working? Thanks.
答案1
得分: 1
每个VU都有自己的初始化上下文,这意味着在您的情况下,每个虚拟用户都有自己的传输时间数组。这也适用于摘要:它是在一个单独的“进程”中创建的,无法访问其他虚拟用户的VU特定数据。
我建议使用k6的内置功能来跟踪自定义指标,例如Trend;无需手动实现响应时间跟踪和汇总。此外,您无需担心内存用尽的问题,因为并非每个数据点都被推送到数组中。
在您的情况下,您甚至可以依赖默认指标,这些指标已经跟踪了HTTP请求的数量和持续时间。
通过使用handleSummary
函数实现自定义摘要,您可以访问您的指标和阈值,并生成您想要的输出,例如data.metrics['http_req_duration{expected_response:true}']
。metrics
映射将包含类型和值(例如,计数、平均、最小、最大)。
英文:
Each VU has its own init context, which means in your case that every VU (virtual user) has its own transfer times array. This applies to the summary too: it is created in a separate "process", without access to VU-specific data of the other VUs.
I recommend using builtin functionality of k6 to track custom metrics, such as Trend; no need to manually implement response time tracking and aggregation. Also, you don't have to worry about running out of memory, since not every data point is pushed to an array.
In your case, you can even rely on the default metrics, which already track the number and duration of HTTP requests.
By implementing a custom summary with the handleSummary
function, you can access your metrics and thresholds and generate your desired output, e.g. data.metrics['http_req_duration{expected_response:true}']
. The metrics
map will contain the type and values (e.g. count, avg, min, max).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论