一、siege的基本介紹
Siege是一個開源的壓力測試和基準工具,屬於Linux系統下的一個測試工具,完全使用C語言實現,可以對HTTP和FTP服務器進行負載和性能測試,siege的設計主要用於WEB的性能測試。
Siege是由多線程實現的同步壓測工具,它實現的是模擬n個用戶不停地訪問某個URL的場景。由於多線程開銷會比多進程小一些,因此該壓測工具比多進程的壓測工具在系統開銷上會好很多。支持同時壓測多個URL支持ftp、http、https,也能夠隨機選取URL進行壓測,可以發送GET、POST、HEAD等多種請求,並且程序中特意增加了許多解決不同平臺上兼容性的代碼。運行比較穩定,相對來說是比較好的工具,並且到目前位置,Siege的版本依然在更新中。
但是siege也有一些缺點。Siege的時間控制並不精確,如:想要測試5s,但是實際輸出的測試時間爲4.89s,另外一個小的缺點是,由於使用多線程實現,一個進程可以開啓的線程數量本身是有限的,並且線程過多的情況下CPU在線程間切換也是一筆不小的開銷,十分影響效率。因此Siege的使用過程中還要注意開啓的併發用戶數不能太多
二、下載安裝
1、下載安裝
wget http://download.joedog.org/siege/siege-3.1.4.tar.gz
tar -zxvf siege-3.1.4.tar.gz
./configure --prefix=/cdy/ceshi/test/siege --mandir=/cdy/ceshi/test/man --with-ssl=/cdy/ceshi/test/ssl
mkdir -p /cdy/ceshi/test/siege/etc
mkdir -p /cdy/ceshi/test/siege/var
make
make install
2、查看是否安裝成功/cdy/ceshi/test/siege/bin/siege -C
三、Siege命令常用參數
-c 200 指定併發數200
-r 5 指定測試的次數5
-f urls.txt 制定url的文件
-i internet系統,隨機發送url
-b 請求無需等待 delay=0
-t 5 持續測試5分鐘
# -r和-t一般不同時使用
四、實例
get請求:
1、直接運行請求一個接口
/cdy/cdy/siege/bin/siege -c 1 -t 5S "http://10.20.0.56:8081/api/v3/home/recommendV2?hasPhoto=0&isChange=0&page=1&limit=3"
2、url存儲在文檔裏,可以多個一起運行
/cdy/cdy/siege/bin/siege -c 10 -t 10s -f /cdy/cdy.txt
post請求:
1、不用傳header和body
/cdy/cdy/siege/bin/siege -c 10 -r 1 "http://10.20.0.56:8081/api/v3/home/interested/save? POST deptIds=1"
2、要傳header和body
#header:Content-Type: application/json
#若body是json形式,body文件postfile.json已經定義header是Content-Type: application/json
如:/cdy/ceshi/test/siege/bin/siege -c 10 "http://10.20.0.56:8081/api/v3/patient/wechat/patient/save POST </cdy/test/postfile.json" -t 10s
3、登錄接口
/cdy/ceshi/test/siege/bin/siege -c 10 -r 100 "http://10.20.0.56:8081/api/v3/auth/login?cellphone=15757164410&password=e10adc3949ba59abbe56e057f20f883e POST"
4、需要在header中傳token
/cdy/ceshi/test/siege/bin/siege -c 10 --header "uid: 17002192300" --header "token: 45fb389a90702ac635a6d1c2446acee2" "http://10.20.0.56:8081/api/v3/auth/doOCRAuth POST" -t 10s
#header是Content-Type: application/json時,用命令-H “Content-Type: application/json”
五、Siege輸出結果說明
Transactions: 總共測試次數
Availability: 成功次數百分比
Elapsed time: 總共耗時多少秒
Data transferred: 總共數據傳輸
Response time: 等到響應耗時
Transaction rate: 平均每秒處理請求數
Throughput: 吞吐率
Concurrency: 最高併發
Successful transactions: 成功的請求數
Failed transactions: 失敗的請求數
運行結果存儲在:siege.log