LoadRunner 事物

添加事物

Action()
{
    lr_start_transaction("openindex");
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    lr_end_transaction("openindex", LR_AUTO);
    return 0;
}
运行结果:
Action.c(4): 通知: Transaction "openindex" started. Action.c(6): web_url("WebTours") 已成功,312 个正文字节,164 个标头字节 [MsgId: MMSG-26386] Action.c(15): 通知: Transaction "openindex" ended with "Pass" status (Duration: 0.4420 Wasted Time: 0.3987).

lr_end_transaction的4中状态

LR_PASS LR_FAIL 自定义事物成功和失败。也可以用 lr_set_transaction_status_by_name(LR_FAIL,"openindex"); 函数自定义事物状态

Action()
{
    int status ;
    lr_start_transaction("openindex");
    //web_url 成功返回0 失败返回1
    status =web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours1/", //不存在的网址
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    if (status==0) {
        lr_end_transaction("openindex", LR_PASS);
    }else{   
        lr_end_transaction("openindex", LR_FAIL);
    }    
    return 0;
}
运行结果:
网址不存在 Action.c(
6): 通知: Transaction "openindex" ended with "Fail" status (Duration: 0.4105 Wasted Time: 0.3801). 如果网址存在,请求成功: Action.c(15): 通知: Transaction "openindex" ended with "Pass" status (Duration: 0.3951 Wasted Time: 0.3687).

LR_AUTO 

Action()
{
    lr_start_transaction("openindex");
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/1", //不存在的网址
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    lr_end_transaction("openindex", LR_AUTO);
    return 0;
}
运行结果:
Action.c(4): 通知: Transaction "openindex" ended with "Fail" status (Duration: 0.4072 Wasted Time: 0.3774).
Action()
{
    lr_start_transaction("openindex");
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", //网址正确
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    lr_set_transaction_status_by_name(LR_FAIL,"openindex"); //手动设置事务失败
    lr_end_transaction("openindex", LR_AUTO);
    return 0;
}
结果:
Action.c(14): 通知: Transaction "openindex" ended with "Fail" status (Duration: 0.4284 Wasted Time: 0.3811).

LR_STOP 

事物返回时间的详细解释

Action.c(14): 通知: Transaction "openindex" ended with "Fail" status (Duration: 0.4284 Wasted Time: 0.3811).

Duration(持续时间):事物开始一共的时间。包括Wasted Time、代码浪费时间。在Analysis获取的是 Duration-Wasted Time时间。

Wasted Time(浪费的时间):LR为了支撑,工具内部的时间。事物的统计。用lr_start_timer()  lr_end_timer()函数获取。

Action()
{
    lr_start_transaction("openindex");
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", //网址正确
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    //lr_think_time(3);   
    lr_output_message("Duration:%lf;Wasted Time=%lf",lr_get_transaction_duration("openindex"),lr_get_transaction_wasted_time("openindex"));
    lr_end_transaction("openindex", LR_AUTO);
    return 0;
}
运行结果:
Action.c(13): Duration:0.389529;Wasted Time=0.361978
去掉lr_think_time注释

  Action.c(13): Duration:3.384649;Wasted Time=0.352630   //Duration多了3秒
  Action.c(14): 通知: Transaction "openindex" ended with "Pass" status (Duration: 3.3882 Think Time: 2.9994 Wasted Time: 0.3526).

 程序消耗时间:

Action()
{
    merc_timer_handle_t timer;
    char save[1000];
    double wasteTime;
    int i;
    lr_start_transaction("openindex");
    web_url("WebTours", 
        "URL=http://127.0.0.1:1080/WebTours/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t3.inf", 
        "Mode=HTTP", 
        LAST);
    timer=lr_start_timer();
    for(i=0;i<1000;i++){
        sprintf(save,"this is the way we wasteTime in a script %d",i);
    }
    wasteTime=lr_end_timer(timer);
//lr_wasted_time(wasteTime*1000); //把浪费的时间加到WastedTime.因为wasteTime是秒。lr_wasted_time接收的是毫秒。所以好*1000 lr_output_message(
"Duration:%lf;Wasted Time=%lf",lr_get_transaction_duration("openindex"),lr_get_transaction_wasted_time("openindex")); lr_end_transaction("openindex", LR_AUTO); return 0; } 运行结果: Action.c(22): 通知: Transaction "openindex" ended with "Pass" status (Duration: 3.0407 Wasted Time: 0.3701). 持续时间包括了程序时间。
去掉 lr_wasted_time注释。运行结果
Action.c(23): 通知: Transaction "openindex" ended with "Pass" status (Duration: 3.7046 Wasted Time: 3.6650).
可以看到for循环运行的时间,被加到了Wasted Time

需要注意的

1)事务中不要插入日志函数

2)不要插入集合点函数

3)尽量不要插入思考时间

 

 

 

posted @ 2017-06-27 15:35  米蓝  阅读(304)  评论(0编辑  收藏  举报