运行k6脚本进行负载测试会返回错误

时间:2017-11-17 11:56:04

标签: redirect request k6

我是第一次使用k6的用户,我已经设法在运行脚本时遇到错误:

  

“请求失败[33merror [0m =”获取https:///:已停止   在0重定向后“

脚本k6.js:

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

export let options = {
 stages: [
    { duration: "30s", target: 20 },
    { duration: "1m30s", target: 10  },
    { duration: "20s", target: 0 },
  ]
};

export default function() {
  let res = http.get("https://<our_page_URL>/");
  check(res, {
    "status code MUST be 200": (res) => res.status == 200,
  }) || fail("status code was *not* 200");
  sleep(1);
}

为什么我会收到此错误以及解决方案是什么?

1 个答案:

答案 0 :(得分:2)

您必须设置maxRedirects选项; maxRedirects是在放弃请求和错误输出之前k6将遵循的最大HTTP重定向数(&#34;在$ MAX_REDIRECT_VALUE重定向&#34之后停止获取$ PATH;)

您可以将该选项作为CLI参数或脚本选项传递。有关此选项的更多信息,请访问https://docs.k6.io/docs/options

export let options = {
    // Max redirects to follow (default is 10)
    maxRedirects: 10
};

默认值为10,因此可能会跳过分配默认值的错误。

这是一个redirect example来测试它是如何工作的。

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

export let options = {
    // Max redirects to follow (default is 10)
    maxRedirects: 5
};

export default function() {
    // If redirecting more than options.maxRedirects times, the last response will be returned
    let res = http.get("https://httpbin.org/redirect/6");
    check(res, {
        "is status 302": (r) => r.status === 302
    });

    // The number of redirects to follow can be controlled on a per-request level as well
    res = http.get("https://httpbin.org/redirect/1", {redirects: 1});
    console.log(res.status);
    check(res, {
        "is status 200": (r) => r.status === 200,
        "url is correct": (r) => r.url === "https://httpbin.org/get"
    });
}