如何在K6 Check中添加自定义消息

Gei*_*eis 3 k6

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

\n\n
req = [req0, req1, req2, ...];\nlet res = http.batch(req);\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,我尝试对每个请求运行“检查”,并使用 while 循环来执行此操作。

\n\n
while (i < req.length) {\n    check(\n      res[i],\n      {" ${i} - status 200": (r) => r.status === 200 }\n    );\n    i++;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,K6 在单个测试中累积所有“检查”测试结果,因为测试消息不会解析我传递的变量。\n输出在测试结束时打印此消息:

\n\n
req = [req0, req1, req2, ...];\nlet res = http.batch(req);\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
{ i + " - status 200": (r) => r.status === 200 }\n{\' ${i} - status 200\': (r) => r.status === 200 }\n{` ${i} - status 200`: (r) => r.status === 200 }\n{" %d - status 200", i : (r) => r.status === 200 }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想知道是否有什么原因要在这里传递自定义消息。

\n

A. *_*ten 6

您可以使用括号表示法来指定属性名称。

\n

请参阅下面的示例和以下链接:如何从 JavaScript 中的变量值创建对象属性?

\n
import http from "k6/http";\nimport { check } from "k6";\n\nexport default function() {\n    let response = http.get("https://test.loadimpact.com");\n    let test = "x";\n    check(response, {\n        [test + " - status 200"]: (r) => r.status === 200\n    });\n};\n
Run Code Online (Sandbox Code Playgroud)\n

这会产生以下输出:

\n
done [==========================================================] 1 / 1i\n\n\xe2\x9c\x93 x - status 200 \n\nchecks.....................: 100.00% \xe2\x9c\x93 1   \xe2\x9c\x97 0\ndata_received..............: 6.8 kB  15 kB/s\ndata_sent..................: 527 B   1.2 kB/s\nhttp_req_blocked...........: avg=362.37ms min=362.37ms med=362.37ms max=362.37ms p(90)=362.37ms p(95)=362.37ms\nhttp_req_connecting........: avg=138.16ms min=138.16ms med=138.16ms max=138.16ms p(90)=138.16ms p(95)=138.16ms\nhttp_req_duration..........: avg=91.62ms  min=91.62ms  med=91.62ms  max=91.62ms  p(90)=91.62ms  p(95)=91.62ms\nhttp_req_receiving.........: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s\nhttp_req_sending...........: avg=992.2\xc2\xb5s  min=992.2\xc2\xb5s  med=992.2\xc2\xb5s  max=992.2\xc2\xb5s  p(90)=992.2\xc2\xb5s  p(95)=992.2\xc2\xb5s\nhttp_req_tls_handshaking...: avg=214.19ms min=214.19ms med=214.19ms max=214.19ms p(90)=214.19ms p(95)=214.19ms\nhttp_req_waiting...........: avg=90.63ms  min=90.63ms  med=90.63ms  max=90.63ms  p(90)=90.63ms  p(95)=90.63ms\nhttp_reqs..................: 1       2.207509/s\niteration_duration.........: avg=453.99ms min=453.99ms med=453.99ms max=453.99ms p(90)=453.99ms p(95)=453.99ms\niterations.................: 1       2.207509/s\nvus........................: 1       min=1 max=1\nvus_max....................: 1       min=1 max=1\n
Run Code Online (Sandbox Code Playgroud)\n