2020
Aug
09

網頁好讀版

用 shell script 寫一個簡單的 web 壓測工具。

1. 先建一個 curl-format 設定我們要的 curl response

  • curl-format.txt
Example
  1. http_code: %{http_code}\n
  2. time_namelookup: %{time_namelookup}\n
  3. time_connect: %{time_connect}\n
  4. time_appconnect: %{time_appconnect}\n
  5. time_pretransfer: %{time_pretransfer}\n
  6. time_redirect: %{time_redirect}\n
  7. time_starttransfer: %{time_starttransfer}\n
  8. ----------\n
  9. time_total: %{time_total}\n

2. 用 Shell script 壓同一個 url 30 次


Example
  1. for i in {1..30}
  2. do
  3. s="$s\n$i"
  4. done
  5.  
  6. 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
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json;charset=utf-8
  3. Content-Length: 3311
  4. Date: Sun, 09 Aug 2020 15:22:54 GMT
  5. Age: 0
  6. Connection: keep-alive
  7.  
  8. http_code: 200
  9. time_namelookup: 0.001633
  10. time_connect: 0.020480
  11. time_appconnect: 0.066257
  12. time_pretransfer: 0.066334
  13. time_redirect: 0.000000
  14. time_starttransfer: 0.084286
  15. ----------
  16. time_total: 0.084404
  17. .
  18. .
  19. .

同時對不同的 url 壓測

先建立一個 list.txt ,每一行填一個 path, QPS 設定 10 ,每次送 10 個 Requests,然後 SLEEP_SEC=1 等一秒。

Example
  1. QPS=10
  2. SLEEP_SEC=1
  3.  
  4. serviceBase="http://www.yourhost.com"
  5. file="list.txt"
  6. declare -a allUrl
  7. echo "To load test urls"
  8. i=0
  9. while IFS= read -r line
  10. do
  11. url="$serviceBase$line"
  12. allUrl[$i]="$url"
  13. echo "will test $url\n"
  14. i=$((i+1))
  15. if [ $i -gt $QPS ]; then
  16. break;
  17. fi
  18. done < "$file"
  19.  
  20. while true;
  21. do
  22. all=""
  23. for url in ${allUrl[@]}
  24. do
  25. all="$all\n$url";
  26. done
  27. echo -e "$all" | xargs -n 1 -t -P$QPS -I% curl -I -w "@curl-format.txt" -k "%" --max-time 5 2>&1
  28. echo -e "\n\ncomplete $QPS requests, sleep $SLEEP_SEC seconds\n\n"
  29. if [ $SLEEP_SEC -gt 0 ];then
  30. sleep $SLEEP_SEC
  31. fi
  32. done

網頁好讀版