文章目錄
- install and run
- script.js
- options
- 最佳實踐
- report 解析
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: localscript: script.jsoutput: -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 200checks.........................: 100.00% 191878 out of 191878data_received..................: 160 MB 3.5 MB/sdata_sent......................: 20 MB 443 kB/shttp_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.14mshttp_req_failed................: 0.00% 0 out of 191878http_req_receiving.............: avg=40.53μs min=5μs med=18μs max=65.69ms p(90)=74μs p(95)=102.14μshttp_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.11mshttp_reqs......................: 191878 4263.707029/siteration_duration.............: avg=246.24ms min=406μs med=103.26ms max=6.91s p(90)=617.2ms p(95)=979.24msiterations.....................: 191878 4263.707029/svus............................: 7 min=7 max=1990vus_max........................: 2000 min=2000 max=2000running (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 以內