You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让LoadRunner中web_sync与轮询ResponseCB()不被计为浪费时间?

如何将LoadRunner中web_sync轮询时间计入事务时长而非浪费时间?

场景说明

针对异步web_url,LoadRunner新增了web_reg_async_attributes函数。我在Poll_0_ResponseCB中等待aHttpStatusCode == 200,因为该请求是XHR请求,服务器会异步传输所有数据,只有GET请求接收完所有数据后,事务才算完成。

现有代码

web_reg_async_attributes("ID=Poll_0", 
    "Pattern=Poll", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "PollIntervalMs=500", 
    "RequestCB=Poll_0_RequestCB", 
    "ResponseCB=Poll_0_ResponseCB", 
    LAST);
web_url("GetUnreadNotifications_2", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "Resource=0", 
    "RecContentType=application/json", 
    "Referer=https://[URL]/", 
    "Snapshot=t14.inf", 
    "Mode=HTML", 
    LAST);
web_sync("ParamCreated=stopAsync", "RetryIntervalMs=500", "RetryTimeoutMs=120000", LAST);
web_stop_async("ID=Poll_0",LAST);

当前问题

LoadRunner将上述轮询机制的时间计为浪费时间,但实际上这段等待是为了确保数据接收完成,需要将其纳入实际事务时长。当前运行结果示例:

ended with a "Pass" status (Duration: 33,9532 Wasted Time: 33,3178).

注:该API确实较慢...

解决方案

1. 手动用事务包裹完整流程

通过lr_start_transaction和lr_end_transaction直接包裹包含web_sync的整个关键流程,覆盖LoadRunner的自动计时逻辑,让工具将这段时间全部计入事务时长:

lr_start_transaction("GetUnreadNotifications_Transaction");

web_reg_async_attributes("ID=Poll_0", 
    "Pattern=Poll", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "PollIntervalMs=500", 
    "RequestCB=Poll_0_RequestCB", 
    "ResponseCB=Poll_0_ResponseCB", 
    LAST);
web_url("GetUnreadNotifications_2", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "Resource=0", 
    "RecContentType=application/json", 
    "Referer=https://[URL]/", 
    "Snapshot=t14.inf", 
    "Mode=HTML", 
    LAST);
web_sync("ParamCreated=stopAsync", "RetryIntervalMs=500", "RetryTimeoutMs=120000", LAST);
web_stop_async("ID=Poll_0",LAST);

lr_end_transaction("GetUnreadNotifications_Transaction", LR_AUTO);

2. 调整浪费时间识别配置

在LoadRunner的脚本设置中,找到「浪费时间」相关配置项,将web_sync操作的等待时间排除在浪费时间统计范围之外。不过这种方式是全局设置,可能会影响其他脚本的统计逻辑,适合所有web_sync等待都需计入事务的场景。

3. 自定义计时并关联事务

通过lr_start_timer和lr_end_timer获取这段流程的精确耗时,手动设置到事务中,实现更精细的时长控制:

double timer_start = lr_start_timer();

// 执行异步请求、同步等待、停止异步的完整代码块
web_reg_async_attributes("ID=Poll_0", 
    "Pattern=Poll", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "PollIntervalMs=500", 
    "RequestCB=Poll_0_RequestCB", 
    "ResponseCB=Poll_0_ResponseCB", 
    LAST);
web_url("GetUnreadNotifications_2", 
    "URL=https://[URL]/api2/notifications/GetUnreadNotifications", 
    "Resource=0", 
    "RecContentType=application/json", 
    "Referer=https://[URL]/", 
    "Snapshot=t14.inf", 
    "Mode=HTML", 
    LAST);
web_sync("ParamCreated=stopAsync", "RetryIntervalMs=500", "RetryTimeoutMs=120000", LAST);
web_stop_async("ID=Poll_0",LAST);

double total_duration = lr_end_timer();
lr_set_transaction_duration("GetUnreadNotifications_Transaction", total_duration);
lr_end_transaction("GetUnreadNotifications_Transaction", LR_AUTO);

内容的提问来源于stack exchange,提问作者kenneth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 03:41:31