postman压力测试

序:

       今天公司的门锁设备可能需要压力测试,提供的接口API接口需要压力测试。

一、postman准备

       其实也没有什么好准备的,唯一就是有些变量不能写死了,需要随机一个,然后再请求。

postman压力测试

{{}}包裹的都是要随机的参数,前面headers的设置今天就不讲了,要知道的可以看之前的文章。

然后这块因为随机东阿u是有规则的,所以还是一样直接在Pre-requestScript这个Tab里写

postman.clearGlobalVariable("permissionBegin");
postman.clearGlobalVariable("permissionEnd");
postman.clearGlobalVariable("Authorization");
postman.clearGlobalVariable("timestamp");


var heads = request.headers;
//console.log(heads);

var clientId = heads.clientid;

//console.log("---clientId:"+clientId);
var secret = "xxxxx";

var ts = Math.round(new Date().getTime());
//console.log("---ts:"+ts)
postman.setGlobalVariable("timestamp",ts);

var signParm = xxxxxxxxxxxxxxxxxxx;  //要加密的串,按照大家自己公司的加密规则算
var sign = CryptoJS.SHA1(signParm).toString();
//console.log("------------Authorization:"+sign);

postman.setGlobalVariable("Authorization",sign);

var permissionBegin = ts;
var permissionEnd = ts + 1000*60*60*24;
console.log("------------permissionBegin:"+permissionBegin);
console.log("------------permissionEnd:"+permissionEnd);

var userNo = Math.floor(Math.random()*99) + 1;
if(userNo < 10){
    userNo = "0" + userNo;
}
var passwordNo = "0" + (Math.floor(Math.random()*8) + 1);

var password = "";
for(var i = 0;i < 6;i++){
    var tmpPwd = Math.floor(Math.random()*10);
    password += tmpPwd;
}
console.log("userNo:"+userNo+"--passwordNo:"+passwordNo+"--password:"+password);

postman.setGlobalVariable("userNo",userNo);
postman.setGlobalVariable("passwordNo",passwordNo);
postman.setGlobalVariable("password",password);
postman.setGlobalVariable("permissionBegin",permissionBegin);
postman.setGlobalVariable("permissionEnd",permissionEnd);

二、断言(Tests这个Tab)

这里所谓的断言,其实就是用来判断接口是否请求成功的。我们这个code返回为20000就表示请求成功。所以断言如下:

pm.test('测试接口请求是否成功',function(){
    var resStr = pm.response.text();
    console.log(resStr);
    var resJson = JSON.parse(resStr);
    var reCode = resJson.CODE;
    console.log(reCode);
    pm.expect(reCode).to.include("20000");
});

要知道更多的断言写法可以看官方文档。

特别说明:

如果不写断言,那接口请求可能就会报错,如图:

postman压力测试

三、压力测试方法

postman压力测试

特别说明:

压测的接口必须放在collection里,或者选择一个文件夹,里面放collection导出的文件

Environment输入一个名字

Iterations压测次数(接口请求次数)

Delay间隔时间

下面2个默认就好,然后点击StartRun

四、效果

postman压力测试

成功失败次数都可以看到,这个失败次数就是基于你写的断言判断的。有人说那随机的参数我看不到啊,ok,可以告诉你此种情况下postman的console是依然可以打印你console.log的日志的。你可以从这里看到你的参数,甚至可以复制出来分析。

ok,到这里就分享完成了,唯一要说的是,我这里打印了时间戳,发现就算我把间隔时间设置为0,也做不到多线程方式的压力测试,所以如果对并发有要求的话,可以多找几个人一起跑postman,或者用jmater。也许postman也有我还没发现,那就下次用到了再告诉大家吧。