【问题标题】:Using Postman runner to call API n times for benchmark testing使用 Postman runner 调用 API n 次进行基准测试
【发布时间】:2019-10-13 10:12:35
【问题描述】:

我正在编写一个新的 API,并希望能够看到它在遇到 n 个请求时是如何公平的。

我尝试设置环境变量并使用 Postman 中的运行器工具无济于事。

最终目标是运行它 n 次,我将 [n] 的值传递到正文中,以便进行审计(该字段的值存储在数据库中)。

我已经设置了 2 个环境变量

company=Bulk API Test
requestcount=0

我的预请求脚本是

let requestCount = +postman.getEnvironmentVariable("requestcount");
if(!requestCount)
{
    requestCount = 0;
}

requestCount++;
postman.setEnvironmentVariable("requestcount", requestCount);

这应该每次都将环境变量 requestcount 更新为 +1。

我的测试脚本是

var currentCount = +postman.getEnvironmentVariable("requestcount");
if(currentCount < 5) // want it to run 5 times
{
    postman.setNextRequest("https://snipped");
}
else
{
    postman.setNextRequest(null);
}

当我通过运行程序运行它时,它比非运行程序执行花费的时间要长得多,结果是 API 只被命中了一次。

【问题讨论】:

    标签: postman postman-collection-runner postman-pre-request-script


    【解决方案1】:

    如果您的 API 调用始终相同,请尝试仅使用邮递员运行程序的迭代计数。只需输入那里,例如5. 你的收藏会重复5次。

    不能通过以下属性访问迭代:

    pm.info.iteration
    

    找出它是哪个迭代。

    如果您仍然需要增加变量,请确保它们被解析为整数。

    var currentCount =+ parseInt(postman.getEnvironmentVariable("requestcount"));
    

    说实话:此基准测试的最佳方法是使用负载测试工具,例如Loadrunner 而不是 Postman。

    【讨论】:

    • 是的,但我需要一个变量在每次运行/迭代之间更改(增量为 1)
    • @adrewb 我还添加了解析整数的提示。如果从 env 或 globals 加载它,则必须将值解析为整数。这是必要的,因为邮递员将所有内容作为字符串存储在 env 和全局变量中。
    猜你喜欢
    • 2018-05-18
    • 2019-03-07
    • 2019-12-18
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2018-09-12
    相关资源
    最近更新 更多