3

我已经启动并运行了 k6,但现在每次我尝试运行测试时都会收到此错误:ReferenceError: regeneratorRuntime is not defined。

我曾尝试安装和导入 babel,有人建议这样做,但没有奏效。

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

async function registerHandlers() {
    const dataForBody = 'client_id=LoadTesting&grant_type=client_credentials&' +
      `scope=${encodeURI('StitchApi')}&` +
      `client_secret=${encodeURI(process.env.REACT_APP_CLIENT_SECRET)}`;
    const messageHeaders = {
      'Content-Type': 'application/x-www-form-urlencoded',
    };
    axios({
      method: 'post',
      url: process.env.REACT_APP_STITCH_AUTH_URL,
      headers: messageHeaders,
      data: dataForBody,
    }).then((response) => {
        return response;
    }).catch((error) =>
      global.console.log('axios error: ', error)
    )
  }
// const queries = JSON.parse(open("./easygraphql-load-tester-queries.json"));
const url = "https://mywebsite.net/Api/GraphQL/";
const authorization = registerHandlers();
console.log("AUTH!!!!!!", authorization);
const payload = JSON.stringify({
    query: `
    {
        student(id: "5asdfasdfasdfasdf") {
        name
        }
    }
    ` });
const params = {
    headers: {
        "authorization": "asdfasdfasdfasdfasdfasdfasdf",
        "content-type": "application/json",
    }
}

export default function () {
    // console.log('query: ', queries);
    let res = http.post(url, payload, params);
    check(res, {
        "status is 200": (r) => r.status === 200,
        "is authenticated": (r) => r.json().authenticated === true,
        "is correct user": (r) => r.json().user === "user",
        "caption is correct": (r) => r.html("h1").text() == "Example Domain",
    });
};

我只希望我的负载测试能够工作!

编辑:

我在用 "@babel/core": "^7.4.0",

我的 babel.rc 文件看起来像:

{
  "presets": [ "es2015", "stage-0" ]
}
4

1 回答 1

1

我看到您正在使用 Promises,但 k6 目前没有事件循环。所以你的代码将无法工作,不幸的是,babel 在这里无能为力:(。

此外,您正在尝试使用axios,但您没有导入它,并且它也(可能)不起作用,因为它不支持 k6,因为它既不是浏览器也不是 node.js ;)。

你需要让它工作的只是使用 k6 的 http 库而不是 axios 来获得你的授权并且不使用async. 也是globalnode.js 特定的 AFAIK>

于 2019-07-30T14:24:11.227 回答