1

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

"请求失败 [33merror[0m="Get 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);
}

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

4

1 回答 1

2

您必须设置maxRedirects选项;maxRedirects是 k6 在放弃请求并出错之前将遵循的 HTTP 重定向的最大数量(“在 $MAX_REDIRECT_VALUE 重定向之后停止 $PATH”)

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

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

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

这是一个重定向示例,用于测试它是如何工作的。

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"
    });
}
于 2017-11-20T10:14:24.973 回答