為了達到壓力測試的效果,需要申請一台線上機器,並且安裝壓力測試的工具siege。
安裝新版siege。資料說yum安裝的版本2.70對於post方式支持的不好,驗證后發現請求可以正常發過去,但是打開debug模式也拿不到返回值。
wget http://download.joedog.org/siege/siege-3.1.3.tar.gz
【如果下載地址不對,請登錄官網查看最新的版本。https://www.joedog.org/】
下載完后,解壓進入目錄。如下操作是在普通用戶下進行的
# sudo ./configure --prefix=/usr/local/siege --mandir=/usr/local/man
# sudo make
# sudo make install
如下的這些參數含義,自行查閱相關資料,不再贅述。
Usage: siege [options]
siege [options] URL
siege -g URL
Options:
-V, --version
-h, --help
-C, --config
-v, —verbose
-q, —quiet
-g, --get
-c, --concurrent=NUM
-i, --internet
-b, --benchmark
-t, --time=NUMm
-r, --reps=NUM
-f, --file=FILE
-R, --rc=FILE
-l, --log[=FILE]
-m, --mark="text"
-d, --delay=NUM
-H, --header="text"
-A, --user-agent=“text"
-T, --content-type="text"
我當時是需要對電話會議通行能力提供商的服務接口進行壓力測試,驗證其是否能夠提供穩定的服務。
表單提交的方式是post,提交的內容是xml格式的內容,廢了一些周折,最后寫出如下的命令。
siege命令運行的格式:
siege --debug -c10 -t10M -H'Authorization:ZmY4MDgwODEzZTljYjMxZTAxM2ViMTc2NjFiYjAxNTQ6MjAxNDA2MTcxNDMzMzY=' ' https://sandboxapp.***.com/ivr/createconf?sig=C0E95E8EEDB9A3C&maxmember=10 POST </tmp/createconf.xml ' >>/tmp/siege2.log
參數說明
-c10 :10個並發。
-t10M:壓力測試持續10分鍾,還可以按次數。
-H:http請求的header。
url:https://***。url中的POST </tmp/createconf.xml 是post提交請求的文件路徑。
最終的思路:用php腳本先生成siege運行的參數,然后運行
(1)生成url+sig ,header+sig 【sig是服務商提供的賬號應用id等信息和時間戳生成的一個校驗參數】
(2)生成post的body,寫入tmp/createconf.xml 中
(3)運行 siege 。並發數按照2的N次方遞增,一次運行時間固定10分鍾。
壓力測試的結果包括如下內容:
Transactions: 訪問次數
Availability: 成功次數
Elapsed time: 測試用時
Data transferred: 測試傳輸數據量
Response time: 平均響應時間
Transaction rate:每秒事務處理量
Throughput: 吞吐率
Concurrency: 並發用戶數
Successful transactions: 成功傳輸次數
Failed transactions: 失敗傳輸次數
Longest transaction: 最長響應時間
Shortest transaction: 最短響應時間
結果分析:
siege的結果參數,整理形成報告。
2017年10月23日
最近測試一個接口,服務端始終返回400,后來試了-T參數才成功,例如
sudo siege --debug -c10 -t10M -T -H'Content-type:application/json' 'http://10.0.1.77:8888/kafka/produce POST </tmp/senddata.json' >>/tmp/siege2.log
讀了下源碼,在main.c中由my.conttype接收了 T 參數 ,在 url.c 中如果不傳遞 T 會賦予默認值,在header里面指定的Content-type不生效。
public char * url_get_conttype(URL this) { if (this->conttype == NULL) { if (! empty(my.conttype)) { this->conttype = xstrdup(my.conttype); } else { this->conttype = xstrdup("application/x-www-form-urlencoded"); } } return this->conttype; }
by hyb
