https://grafana.com/docs/k6/latest/get-started
install and run
- install
# mac
brew install k6
- 当前目录下生成压测脚本
# create file script.js
k6 new [filename] # create file ‘script.js’ in the current directory
3 run
k6 run script.js
- reports
默认情况下,k6 将总结结果打印到 stdout 。
script.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
vus: 10,
duration: '30s',
};
export default function () {
const res = http.get('http://test.k6.io');
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
options
https://grafana.com/docs/k6/latest/using-k6/k6-options/reference/
- vus
虚拟用户数 - duration
持续时间 - rps
每秒请求数量
请求总数 = vus * rps * durance(s)
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
maxRedirects: 4,
duration: '300s',
vus: 10,
rps: 300
};
// stages : 逐步提升/降低
export const options = {
stages: [
{ target: 200, duration: '30s' },
{ target: 0, duration: '30s' },
],
};
export const options = {
stages: [
{ duration: '10s', target: 100 },
{ duration: '5m', target: 100 },
{ duration: '10s', target: 0 },
],
rps: 100,
};
最佳实践
VUs太大的情况下, 直接启动所有 VUs 会几乎同时发起请求,导致请求的瞬间激增。这样的突发性负载可能会导致系统未能及时响应,进而出现错误。
改为 分阶段增加 VUs , 系统逐步适应增加的负载
report 解析
默认report
/\ Grafana /‾‾/
/\ / \ |\ __ / /
/ \/ \ | |/ / / ‾‾\
/ \ | ( | (‾) |
/ __________ \ |_|\_\ \_____/
execution: local
script: script.js
output: -
scenarios: (100.00%) 1 scenario, 2000 max VUs, 1m15s max duration (incl. graceful stop):
* default: Up to 2000 looping VUs for 45s over 6 stages (gracefulRampDown: 30s, gracefulStop: 30s)
✓ status is 200
checks.........................: 100.00% 191878 out of 191878
data_received..................: 160 MB 3.5 MB/s
data_sent......................: 20 MB 443 kB/s
http_req_blocked...............: avg=5.84µs min=0s med=1µs max=9.49ms p(90)=4µs p(95)=7µs
http_req_connecting............: avg=2.71µs min=0s med=0s max=9.43ms p(90)=0s p(95)=0s
http_req_duration..............: avg=246.16ms min=383µs med=103.16ms max=6.91s p(90)=617.17ms p(95)=979.14ms
{ expected_response:true }...: avg=246.16ms min=383µs med=103.16ms max=6.91s p(90)=617.17ms p(95)=979.14ms
http_req_failed................: 0.00% 0 out of 191878
http_req_receiving.............: avg=40.53µs min=5µs med=18µs max=65.69ms p(90)=74µs p(95)=102.14µs
http_req_sending...............: avg=10.06µs min=1µs med=3µs max=53.75ms p(90)=15µs p(95)=23µs
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=246.11ms min=373µs med=103.09ms max=6.91s p(90)=617.11ms p(95)=979.11ms
http_reqs......................: 191878 4263.707029/s
iteration_duration.............: avg=246.24ms min=406µs med=103.26ms max=6.91s p(90)=617.2ms p(95)=979.24ms
iterations.....................: 191878 4263.707029/s
vus............................: 7 min=7 max=1990
vus_max........................: 2000 min=2000 max=2000
running (0m45.0s), 0000/2000 VUs, 191878 complete and 0 interrupted iterations
default ✓ [======================================] 0000/2000 VUs 45s
总请求量 191878
, 持续时间 45.0s
p(95)=979.14ms
: 95% 的请求响应时间在 979.14ms 以内