k6简介
k6 是用 Go 语言编写的一种高性能的负载测试工具
性能测试工具比较
k6官方文档
测试实践
编写脚本保存为k6TestJs.js,注:因为想在cloud上生成报告,最多接受50个VUs,target=50
import http from 'k6/http';
import { check, group, sleep } from 'k6';
export const options = {
stages: [
{ duration: '5s', target: 50 }, // simulate ramp-up of traffic from 1 to 100 users over 5 minutes.
{ duration: '10s', target: 50 }, // stay at 100 users for 10 minutes
{ duration: '5s', target: 0 }, // ramp-down to 0 users
],
thresholds: {
'http_req_duration': ['p(99)<1500'], // 99% of requests must complete below 1.5s
'logged in successfully': ['p(99)<1500'], // 99% of requests must complete below 1.5s
},
};
const BASE_URL = 'https://test-api.k6.io';
const USERNAME = 'TestUser';
const PASSWORD = 'SuperCroc2020';
export default () => {
const loginRes = http.post(`${BASE_URL}/auth/token/login/`, {
username: USERNAME,
password: PASSWORD,
});
check(loginRes, {
'logged in successfully': (resp) => resp.json('access') !== '',
});
const authHeaders = {
headers: {
Authorization: `Bearer ${loginRes.json('access')}`,
},
};
const myObjects = http.get(`${BASE_URL}/my/crocodiles/`, authHeaders).json();
check(myObjects, { 'retrieved crocodiles': (obj) => obj.length > 0 });
sleep(1);
};
-
登录k6 cloud
登录,我使用的谷歌邮箱登录,【我的】--【API-token】获取token
使用token登录,k6 login cloud --token <YOUR_K6_CLOUD_API_TOKEN>
-
执行测试
使用k6 run --out cloud k6TestJs.js,cloud上查看报告