【问题标题】:Running k6 script for load tests returns an error为负载测试运行 k6 脚本会返回错误
【发布时间】:2017-11-17 11:56:03
【问题描述】:

我是第一次使用 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);
}

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

【问题讨论】:

  • 为什么你有|| fail()检查,而不是设置options.throwtrue
  • 好问题。我在 k6 页面上找到了这个例子,对我来说似乎很有用,这就是为什么我用它来看看它是如何工作的。不幸的是,我不知道如何使用 options.throw,但会查看文档。
  • @Sander & jurijk 我认为这些文档是在 options.throw 存在之前编写的。感谢您指出这一点,我会看看我是否可以更新文档!

标签: redirect request k6


【解决方案1】:

您必须设置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,因此可能存在跳过分配的默认值的错误。

这是一个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"
    });
}

【讨论】:

  • ppcano:这拯救了我的一天!谢谢!现在我终于没有错误了,其余的工作完美无缺。
  • @jurijk:乐于助人。我以为你在 Slack 对话后已经解决了这个问题,所以我没有早点回答这个问题。无论如何,如果您有任何问题或需要帮助,请告诉我们。我还为错误 github.com/loadimpact/k6/issues/369 打开了一个 github 问题
猜你喜欢
  • 2022-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
相关资源
最近更新 更多