因工作需要測試RESTful API性能,公司開發(fā)推薦了Apache Benchmark,經過簡單的研究,學會了使用。但是新的需求是,我們需要多次跑這個命令,然后拿到全部結果。
于是萌生了寫腳本測試的方法
在網上搜索半天,終于找到了思路。
- 使用 commands 模塊,主要使用commands.getstatusoutput(cmd) 返回(status, output).執(zhí)行shell命令, 返回兩個元素的元組tuple(status, result),status為int類型,result為string類型。
如果使用Python3,則使用subprocess,因為commands在Python3中廢棄了。
- ab的使用
所有ab命令的組成遵循此結構:
ab [options] [full path to web document]
Post命令需要使用-p參數(shù), Put命令需要使用-u參數(shù),同時需要設置-T。
例子:
ab -p test.json -T application/json -A [your_username]:[your_pwd] -c 1 -n 1 [your_url]
注意:AB目前不支持Delete