2020
Aug
09
用 shell script 寫一個簡單的 web 壓測工具。
1. 先建一個 curl-format 設定我們要的 curl response
- curl-format.txt
Example
- http_code: %{http_code}\n
- time_namelookup: %{time_namelookup}\n
- time_connect: %{time_connect}\n
- time_appconnect: %{time_appconnect}\n
- time_pretransfer: %{time_pretransfer}\n
- time_redirect: %{time_redirect}\n
- time_starttransfer: %{time_starttransfer}\n
- ----------\n
- time_total: %{time_total}\n
2. 用 Shell script 壓同一個 url 30 次
Example
- for i in {1..30}
- do
- s="$s\n$i"
- done
- echo -e $s | xargs -n 1 -P8 -I% curl -w "@curl-format.txt" -I -k "https://www.yourhost.com/path?offset=0&limit=10" --max-time 30 2>&1
執行結果
Example
- HTTP/1.1 200 OK
- Content-Type: application/json;charset=utf-8
- Content-Length: 3311
- Date: Sun, 09 Aug 2020 15:22:54 GMT
- Age: 0
- Connection: keep-alive
- http_code: 200
- time_namelookup: 0.001633
- time_connect: 0.020480
- time_appconnect: 0.066257
- time_pretransfer: 0.066334
- time_redirect: 0.000000
- time_starttransfer: 0.084286
- ----------
- time_total: 0.084404
- .
- .
- .
同時對不同的 url 壓測
先建立一個 list.txt ,每一行填一個 path, QPS 設定 10 ,每次送 10 個 Requests,然後 SLEEP_SEC=1 等一秒。
Example
- QPS=10
- SLEEP_SEC=1
- serviceBase="http://www.yourhost.com"
- file="list.txt"
- declare -a allUrl
- echo "To load test urls"
- i=0
- while IFS= read -r line
- do
- url="$serviceBase$line"
- allUrl[$i]="$url"
- echo "will test $url\n"
- i=$((i+1))
- if [ $i -gt $QPS ]; then
- break;
- fi
- done < "$file"
- while true;
- do
- all=""
- for url in ${allUrl[@]}
- do
- all="$all\n$url";
- done
- echo -e "$all" | xargs -n 1 -t -P$QPS -I% curl -I -w "@curl-format.txt" -k "%" --max-time 5 2>&1
- echo -e "\n\ncomplete $QPS requests, sleep $SLEEP_SEC seconds\n\n"
- if [ $SLEEP_SEC -gt 0 ];then
- sleep $SLEEP_SEC
- fi
- done