2

我们有 2 个 API,我们想测试负载影响,第二个 API 是所谓的动态目标,它建立在我们从第一个 API 的响应中获得的数据的基础上。

因此,我们希望按顺序运行此测试。我们怎样才能做到这一点?

import { check, sleep } from 'k6';
import http from 'k6/http';

    export default function() {
            let res, res_body, claim_url
            res = http.batch([req])
            check(res[0], {
                 "form data OK": function (res) {
                        console.log(res.status);
                        claim_url = JSON.parse(res.body)
                        console.log(claim_url.details.claim_uri)
                        return false;
                 }
    });

在不同的函数中分组不同的 API 有帮助吗?

4

1 回答 1

3

以任何方式,您不限于每个默认函数迭代的单个 http 请求。因此,您可以使用上一个请求中的任何内容并执行新请求。

http.post 文档中有一个示例,但这是另一个简单的示例:

import { check, sleep } from 'k6';
import http from 'k6/http';

export default function() {
        let res, res_body, claim_url
        res = http.get(req);

        check(res, { // check that we actually didn't get error when getting the url
                  "response code was 200": (res) => res.status == 200,
             });
        claim_url = JSON.parse(res.body) // if the body is "http://example.org" for example
        res2 = http.get(claim_url); // use the returned url
        check(res2, { // here it's res2 not res
                  "response code was 200": (res) => res.status == 200,
             });
        // do more requests or checks

});
于 2018-11-22T09:31:15.240 回答