运行 用于负载测试的 k6 脚本 returns 一个错误

Running k6 script for load tests returns an error

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

"Request Failed [33merror[0m="Get https:///: stopped after 0 redirects"

脚本 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);
}

为什么会出现此错误,解决方案是什么?

您必须设置maxRedirects选项; maxRedirects 是 k6 在放弃请求并出现 ("Get $PATH stopped after $MAX_REDIRECT_VALUE redirects")

错误之前将遵循的最大 HTTP 重定向次数

您可以将选项作为 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"
    });
}