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

时间:2019-08-13 13:10:29

标签: k6

我正在使用request.batch运行K6测试,其中每个测试的请求数可以更改。

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

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

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

但是,由于测试消息无法解析我通过的变量,因此K6将所有“检查”测试的结果汇总在一个测试中。 在测试结束时,输出将显示以下消息:

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 }

我想知道为什么没有在这里传递自定义消息的原因。

1 个答案:

答案 0 :(得分:2)

不确定是否已经有了答案,但是对于其他人,可以使用方括号表示法来辅助属性名称。请参见下面的示例和以下链接: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