如何在 K6 Check 中放置自定义消息

How to put a customized message in K6 Check

我正在 运行使用 request.batch 进行 K6 测试,其中每个测试的请求数量可能会发生变化。

req = [req0, req1, req2, ...];
let res = http.batch(req);

然后,我尝试为每个请求 运行 一个 "check",并且我使用 while 循环来执行此操作。

while (i < req.length) {
    check(
      res[i],
      {" ${i} - status 200": (r) => r.status === 200 }
    );
    i++;
}

然而,K6 将所有 "check" 测试结果累积在一个测试中,因为测试消息不解析我传递的变量。 输出在测试结束时打印此消息:

done [===============] 10s / 10s
✓  ${i} - status 200

我尝试过使用不同的方式添加参数,但没有用:

{ i + " - status 200": (r) => r.status === 200 }
{' ${i} - status 200': (r) => r.status === 200 }
{` ${i} - status 200`: (r) => r.status === 200 }
{" %d - status 200", i : (r) => r.status === 200 }

我想知道为什么要在这里传递自定义消息。

不确定您是否已经有了答案,但对于其他人,您可以使用括号符号来指定 属性 名称。请参阅下面的示例和以下 link:How to create an object property from a variable value in JavaScript?

import http from "k6/http";
import { check } from "k6";

export default function() {
    let response = http.get("https://test.loadimpact.com");
    let test = "x";
    check(response, {
        [test + " - status 200"]: (r) => r.status === 200
    });
};

这导致以下输出:

done [==========================================================] 1 / 1i

✓ x - status 200 

checks.....................: 100.00% ✓ 1   ✗ 0
data_received..............: 6.8 kB  15 kB/s
data_sent..................: 527 B   1.2 kB/s
http_req_blocked...........: avg=362.37ms min=362.37ms med=362.37ms max=362.37ms p(90)=362.37ms p(95)=362.37ms
http_req_connecting........: avg=138.16ms min=138.16ms med=138.16ms max=138.16ms p(90)=138.16ms p(95)=138.16ms
http_req_duration..........: avg=91.62ms  min=91.62ms  med=91.62ms  max=91.62ms  p(90)=91.62ms  p(95)=91.62ms
http_req_receiving.........: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s
http_req_sending...........: avg=992.2µs  min=992.2µs  med=992.2µs  max=992.2µs  p(90)=992.2µs  p(95)=992.2µs
http_req_tls_handshaking...: avg=214.19ms min=214.19ms med=214.19ms max=214.19ms p(90)=214.19ms p(95)=214.19ms
http_req_waiting...........: avg=90.63ms  min=90.63ms  med=90.63ms  max=90.63ms  p(90)=90.63ms  p(95)=90.63ms
http_reqs..................: 1       2.207509/s
iteration_duration.........: avg=453.99ms min=453.99ms med=453.99ms max=453.99ms p(90)=453.99ms p(95)=453.99ms
iterations.................: 1       2.207509/s
vus........................: 1       min=1 max=1
vus_max....................: 1       min=1 max=1