使用k6 压测cube.js 请求接口

参考js 脚本

app.js

// Creator: WebInspector 537.36
 
import { sleep, group,check } from "k6";
import http from "k6/http";
 
export const options = {
  vus: 2000,
  duration: '1m',
  thresholds: {
    http_req_duration: ['p(90)<10000'], // 99% of requests must complete below 1.5s
  },
};
 
export default function main() {
  let response;
 
  group("page_1 - http://localhost:4000/", function () {
 
    response = http.get(
      "http://localhost:4000/cubejs-api/v1/load?query=%7B%22measures%22%3A%5B%22CustomerDemographics.count%22%5D%2C%22timeDimensions%22%3A%5B%5D%2C%22order%22%3A%7B%22CustomerDemographics.count%22%3A%22desc%22%7D%2C%22dimensions%22%3A%5B%22CustomerDemographics.cdMaritalStatus%22%2C%22CustomerDemographics.cdCreditRating%22%5D%7D&queryType=multi",
      {
        headers: {
          Host: "localhost:4000",
          Connection: "keep-alive",
          "sec-ch-ua":
            '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
          authorization:
            "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiZGFsb25nIiwiYWdlIjozMzMsImlhdCI6MTYxODQxNDQ4Mn0.I6ED9Zp3e9YSHwJ8hvn9KkMKeWkH0hUsLxtu47dHOyY",
          "sec-ch-ua-mobile": "?0",
          "User-Agent":
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36",
          "x-request-id": "d3292037-adb5-4f7a-a9cc-72be5975fa56-span-1",
          Accept: "*/*",
          "Sec-Fetch-Site": "same-origin",
          "Sec-Fetch-Mode": "cors",
          "Sec-Fetch-Dest": "empty",
          "Accept-Encoding": "gzip, deflate, br",
          "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
        },
      }
    );
    check(response, {
      'logged in successfully': (resp) => resp.status<400,
    })
    // Automatically added sleep
    sleep(1);
  })
}

运行

k6 run app.js

说明

以上只是一个简单的压测,实际我们应该包含很多流程上的压测,包括冒烟测试,压力测试,浸泡测试。。。

参考资料

https://k6.io/docs/test-types/introduction/

上一篇:开发一个cube.js schemaVersion 包装


下一篇:cube.js 自定义checkAuth 响应状态码