因工作需要测试RESTful API性能,公司开发推荐了Apache Benchmark,经过简单的研究,学会了使用。但是新的需求是,我们需要多次跑这个命令,然后拿到全部结果。
于是萌生了写脚本测试的方法
在网上搜索半天,终于找到了思路。
- 使用 commands 模块,主要使用commands.getstatusoutput(cmd) 返回(status, output).执行shell命令, 返回两个元素的元组tuple(status, result),status为int类型,result为string类型。
如果使用Python3,则使用subprocess,因为commands在Python3中废弃了。
- ab的使用
所有ab命令的组成遵循此结构:
ab [options] [full path to web document]
Post命令需要使用-p参数, Put命令需要使用-u参数,同时需要设置-T。
例子:
ab -p test.json -T application/json -A [your_username]:[your_pwd] -c 1 -n 1 [your_url]
注意:AB目前不支持Delete