AdSense

網頁

2021/7/21

K6 負載測試工具簡介 load testing tool intro

K6是款Go語言撰寫的開源負載測試工具(load testing tool)。本篇簡單介紹安裝及使用方式。


簡介 Intro

K6和JMeter都是普遍拿來做負載壓力測試的工具,兩者也經常被拿來比較。K6的優勢在於較好的效能與較少的資源消耗、使用命令列控制、test as code,測試腳本以JavaScript撰寫維護容易、容易與專案一起加入版控如github及與CICD pipeline整合。不過JMeter也有其他優勢如支援豐富的通訊協議(protocols)、GUI操作介面(對不會寫程式的測試人員友善)、歷史悠久資源豐富、可執行分布式負載測試,內建多樣化的測試報表等。



安裝 Install

在Mac安裝非常容易,但要裝好Homebrew,在終端機命令列輸入brew install k6即可完成安裝。

$ brew install k6
...
==> Downloading https://ghcr.io/v2/homebrew/core/k6/manifests/0.33.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/k6/blobs/sha256:9630025b40877144
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha
######################################################################## 100.0%
==> Pouring k6--0.33.0.big_sur.bottle.tar.gz
🍺  /usr/local/Cellar/k6/0.33.0: 5 files, 28.2MB

Windows系統可從官網下載安裝檔進行安裝。



測試 Test

例如本機一個系統(React應用程式)首頁url為http://localhost:3000/

K6使用JavaScript來撰寫測試腳本。在任意目錄建立一個test.js內容對http://localhost:3000/進行測試。測試只對回應的HTTP status code檢查是否為200(請求成功)。

test.js

import http from 'k6/http';
import {
  sleep,
  check
} from 'k6';

export default function() {
  let res = http.get('http://localhost:3000/');

  check(res, {
    'status was 200': (r) => r.status == 200
  })
  sleep(1);
}

test.js所在目錄位置以命令列輸入k6 run test.js開始測試。執行結果如下,可看到請求的資料大小及連線時間等資訊。

$ k6 run test.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)


running (00m01.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [=========================] 1 VUs  00m01.0s/10m0s  1/1 iters, 1 per VU

     ✓ status was 200

     checks.........................: 100.00% ✓ 1        ✗ 0
     data_received..................: 2.1 kB  2.1 kB/s
     data_sent......................: 80 B    80 B/s
     http_req_blocked...............: avg=859µs  min=859µs  med=859µs  max=859µs  p(90)=859µs  p(95)=859µs
     http_req_connecting............: avg=230µs  min=230µs  med=230µs  max=230µs  p(90)=230µs  p(95)=230µs
     http_req_duration..............: avg=2.3ms  min=2.3ms  med=2.3ms  max=2.3ms  p(90)=2.3ms  p(95)=2.3ms
       { expected_response:true }...: avg=2.3ms  min=2.3ms  med=2.3ms  max=2.3ms  p(90)=2.3ms  p(95)=2.3ms
     http_req_failed................: 0.00%   ✓ 0        ✗ 1
     http_req_receiving.............: avg=83µs   min=83µs   med=83µs   max=83µs   p(90)=83µs   p(95)=83µs
     http_req_sending...............: avg=122µs  min=122µs  med=122µs  max=122µs  p(90)=122µs  p(95)=122µs
     http_req_tls_handshaking.......: avg=0s     min=0s     med=0s     max=0s     p(90)=0s     p(95)=0s
     http_req_waiting...............: avg=2.09ms min=2.09ms med=2.09ms max=2.09ms p(90)=2.09ms p(95)=2.09ms
     http_reqs......................: 1       0.993561/s
     iteration_duration.............: avg=1s     min=1s     med=1s     max=1s     p(90)=1s     p(95)=1s
     iterations.....................: 1       0.993561/s
     vus............................: 1       min=1      max=1
     vus_max........................: 1       min=1      max=1



沒有留言:

AdSense