【问题标题】:Response times from PostMan scriptsPostMan 脚本的响应时间
【发布时间】:2018-05-21 03:35:33
【问题描述】:

是否有一种简单的方法可以在 PostMan 脚本中捕获请求和响应的响应时间?

我试图通过创建一个集合然后在测试中执行以下操作,编写以下脚本来捕获并将值放入环境变量中:

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

这为我提供了环境变量中的值,但我每次都需要专门去那里查看时间。

我尝试过的另一种方法是在集合上使用“Runner”并从导出结果中的“totalRequestTime”值获取响应时间。但是当我运行大约 50 个请求时,很难为每个单独的请求捕获相同的请求。

之前有没有人尝试过这个并且能够获得更好的方法来捕获和导出每个发出的请求的响应时间值?

【问题讨论】:

  • 不确定这是否对您有帮助,但如果您想记录执行 Postman 集合的性能统计信息,我们有一个名为 k6 的性能测试/负载测试工具,它报告了很多性能统计数据,并且有一个Postman-to-k6 转换器可用。

标签: javascript performance performance-testing postman


【解决方案1】:
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);
});

输出:

【讨论】:

    【解决方案2】:

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

    "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);",
                       "});"    
                    ]
                }
         }
    ]
    

    【讨论】:

      【解决方案3】:
      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));
      

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

      【讨论】:

        【解决方案4】:

        您可以使用内置的pm.response.responseTime 函数来获取时间。

        在测试中将其添加到console.log(pm.response.responseTime),以进行尝试或将其分配给环境/全局变量。

        你甚至可以(虽然有点过头了)这样做:

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

        【讨论】:

          猜你喜欢
          • 2020-05-06
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          • 2021-02-28
          • 2018-09-10
          • 2021-09-15
          • 1970-01-01
          • 2017-05-09
          相关资源
          最近更新 更多