PostMan脚本的响应时间

qni6mghb  于 2022-11-07  发布在  Postman
关注(0)|答案(5)|浏览(487)

是否有一种简单的方法可以捕获PostMan脚本中请求和响应的响应时间?
我尝试通过创建一个Collection然后在Tests中执行以下操作,编写以下脚本来捕获值并将其放入环境变量中:

postman.setEnvironmentVariable("MyURLRequest", (responseTime));

这为我提供了环境变量中的值,但我每次都需要专门去那里检查时间。
我尝试过的另一种方法是在集合上使用'Runner',并从导出结果中的“totalRequestTime”值中获取响应时间。但由于我运行了大约50个请求,因此很难为每个单独的请求捕获相同的响应时间。
有没有人尝试过这种方法,并能够获得一种更好的方法来捕获和导出每个发出的请求的响应时间值?

osh3o9ms

osh3o9ms1#

您可以使用内置的pm.response.responseTime函数来获取时间。
将其添加到测试中的console.log(pm.response.responseTime)中,以进行测试或将其分配给env/全局变量。
你甚至可以这样做(虽然有点过度杀戮):

如果您不想在每个测试中使用它,可以将它添加到集合中。在最新版本中,按集合上的“编辑”按钮以查看选项。

wrrgggsh

wrrgggsh2#

pm.test("Response time is less than 200ms", () => {
 pm.expect(pm.response.responseTime).to.be.below(200);
});

上面的scriipt是测试响应时间的,要打印响应时间可以这样做

pm.test("Response time is less than 200ms which is "+pm.response.responseTime, () => {
 pm.expect(pm.response.responseTime).to.be.below(200);
});

输出量:

ugmeyewa

ugmeyewa3#

我可以用下面的代码在纽曼上实现这一点:

"event": [
    {
    "listen": "test",
    "script": {
        "id": "aaaaaaaa-1111-4444-aaaa-000000011111",
        "exec": [                   
                  "var rtm = pm.response.responseTime",
                  "var rts = rtm/1000",

                  "pm.test(\"Check response time and Status\", function () {",
                  "pm.response.to.have.status(200);",

                  "console.log(\"Response time in mili seconds\" , rtm);",
                  "console.log(\"Response time in seconds\" , rts);",
                   "});"    
                ]
            }
     }
]
wpcxdonn

wpcxdonn4#

我在集合Tests中添加了以下脚本,根据您希望运行的迭代次数更新desiredIterationsCount,这也可以存储为全局变量

// we are running 10 iterations so this will reset counters for the nex run
var desiredIterationsCount = 10

var responseTime = pm.response.responseTime
var totalResponseTime = (typeof pm.collectionVariables.get("totalResponseTime") === 'undefined') ? 0 : pm.collectionVariables.get("totalResponseTime");
var iterationsCount = (typeof pm.collectionVariables.get("iterationsCount") === 'undefined') ? 0 : pm.collectionVariables.get("iterationsCount");
var maxResponseTime = (typeof pm.collectionVariables.get("maxResponseTime") === 'undefined') ? null : pm.collectionVariables.get("maxResponseTime");
var minResponseTime = (typeof pm.collectionVariables.get("minResponseTime") === 'undefined') ? null : pm.collectionVariables.get("minResponseTime");

iterationsCount++
if(iterationsCount > desiredIterationsCount) {
    iterationsCount = 1
    totalResponseTime = 0
    maxResponseTime = null
    minResponseTime = null
}

if(maxResponseTime == null || responseTime > maxResponseTime) {
    maxResponseTime = responseTime
    pm.collectionVariables.set("maxResponseTime", maxResponseTime)
}

if(minResponseTime == null || responseTime < minResponseTime) {
    minResponseTime = responseTime
    pm.collectionVariables.set("minResponseTime", minResponseTime)
}

totalResponseTime = totalResponseTime + responseTime
pm.collectionVariables.set("totalResponseTime", totalResponseTime)
pm.collectionVariables.set("iterationsCount", iterationsCount)

if(iterationsCount === desiredIterationsCount) {
    console.log("iterationsCount: ", iterationsCount)
    console.log("avgResponseTime: ", totalResponseTime/iterationsCount)
    console.log("maxResponseTime: ", maxResponseTime)
    console.log("minResponseTime: ", minResponseTime)
}
guicsvcw

guicsvcw5#

var time=pm.response.responseTime
pm.test('Set Filter up to 3 month and check the Response time in millisecond(1 Sec =1000 millisecond):-> ' +time ,()=>
pm.expect(pm.response.code).to.eql(200));

以上代码将为您提供以毫秒为单位响应时间

相关问题