用户访问某个网站或软件,一般不会不停地做个各种操作,例如一次查询,用户需要时间查看查询的结果是否是自己想要的。例如一次订单提交,用户需要时间核对自己填写的信息是否正确等。

  也是说用户在做某些操作时,是会有停留时间的,我把这个时间叫思考时间。但利用代码去执行的时候是没有时间的,当然,脚本运行本身是需要时间的,但比起人的思考时间要小很多。这也是我们为什么要用软件来代替人的某些工作。

  但有时候,我们在做性能测试时,为了更真实的模拟用户的操作,需要给代码加入思考时间。来看看在loadrunner是如何设置思考时间的。

  打开loadrunner 的Virtual User Generator

  菜单栏:Vuser ---Run-Time settings...,切换到Thark Time选项

  好吧!为了更好的理解这个界面上设置,现在我们通过一个脚本来分析。

Action()
{

     double trans_time;  //定义变量

    web_url("www.baidu.com",
        "URL=http://www.baidu.com/",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTML",
        LAST);


    web_url("i-1.0.0.png",
        "URL=http://s1.bdstatic.com/r/www/img/i-1.0.0.png",
        "Resource=1",
        "RecContentType=image/png",
        "Referer=http://www.baidu.com/",
        "Snapshot=t2.inf",
        LAST);

    web_url("favicon.ico",
        "URL=http://www.baidu.com/favicon.ico",
        "Resource=1",
        "RecContentType=image/x-icon",
        "Referer=",
        "Snapshot=t3.inf",
        LAST);

    web_url("su",
        "URL=http://suggestion.baidu.com/su?wd=&cb=window.bdsug.sugPreRequest&sid=1466&t=1362316450913",
        "Resource=1",
        "RecContentType=baiduapp/json",
        "Referer=http://www.baidu.com/",
        "Snapshot=t4.inf",
        LAST);


    lr_start_transaction("注册");  //添加事务
    lr_think_time(20);    //设置思考时间  

    web_url("v.gif",
        "URL=http://nsclick.baidu.com/v.gif?pid=201&pj=www&rsv_sid=1466&fm=behs&tab=tj_reg&un=&path=http%3A%2F%2Fwww.baidu.com%2F&t=1362316485456",
        "Resource=1",
        "Referer=http://www.baidu.com/",
        LAST);


    web_url("favicon.ico_2",
        "URL=https://passport.baidu.com/favicon.ico",
        "Resource=1",
        "RecContentType=application/octet-stream",
        "Referer=",
        "Snapshot=t5.inf",
        LAST);

    web_link("注册",
        "Text=注册",
        "Snapshot=t6.inf",
        LAST);

    web_custom_request("urs.asmx",
        "URL=https://urs.microsoft.com/urs.asmx?MSURS-Client-Key=WI0pAZHPdb%2b3UDOD0AtzxA%3d%3d&MSURS-Patented-Lock=S1IpDfNCCC4%3d",
        "Method=POST",
        "Resource=0",
        "RecContentType=text/xml",
        "Referer=",
        "Snapshot=t7.inf",
        "Mode=HTML",
        "EncType=text/xml; charset=utf-8",
        "Body=<RepLookup v="3"><G>ED8654D5-B9F0-4DD9-B3E8-F8F560086FDF</G><O>F03F2D77-79E1-4DEC-BBF8-81A5C0790160</O><D>9.0.8110.0</D><C>9.00.8112.16421</C><OS>6.1.7601.1.0</OS><I>9.0.8112.16421</I><L>zh-CN</L><R><Rq><URL>aHR0cHM6Ly9wYXNzcG9ydC5iYWlkdS5jb20vdjIvP3JlZyZyZWdUeXBlPTEmdHBsPW1uJnU9aHR0cCUzQSUyRiUyRnd3dy5iYWlkdS5jb20lMkY=</URL><O>PRE</O><T>TOP</T><HIP>220.181.111.48</HIP></Rq></R></RepLookup>",
        EXTRARES,
        "Url=https://passport.baidu.com/img/topbarnav_bg.png", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        "Url=https://passport.baidu.com/passApi/js/reg_6e270622.js", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        "Url=https://passport.baidu.com/passApi/img/small_blank_9dbbfbb1.gif", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        LAST);

    web_url("api",
        "URL=https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1362316491971&class=reg&callback=bd__cbs__c93h6w",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F",
        "Snapshot=t8.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=/img/v.gif?type=1&regType=mail", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        LAST);

    web_url("api_2",
        "URL=https://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1362316491978&class=regPhone&callback=bd__cbs__xs2rv5",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F",
        "Snapshot=t9.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=/cgi-bin/genimage?001362316472015690A3686612594D3D2C14ABF30D0B432CF4462DF3A4B5A8E5E7B7EE094DCAB428311C96E23B35A3CB73A67533699084E25A27CF6C9745497B5C5435611E0593A615CC284B27D70CA5FFB5F771E5D7DC9EE266F6FD61A2D88FEC7B7BA20EFF5DE2CD301E011E634D5063BDDCD35A47C1EB7B5EF365B347DD5800C612B744FAEF845540239F1036CC800902957BD17F36F6547B71C198C154162F8F71077B935E6B", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        "Url=/img/bd-split.gif", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        "Url=/img/reg_tip_header_t.gif", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        "Url=/img/reg_tip_bg_bottom.gif", "Referer=https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F", ENDITEM,
        LAST);


    trans_time=lr_get_transaction_wasted_time("注册");  //获得消耗时间

    if (trans_time) {
        lr_output_message("The duration up to the submit is %f seconds",trans_time);  //打印输出消耗实时间
    }else{
        lr_output_message("the duration cannot be determined. ");
    }

    lr_end_transaction("注册",LR_AUTO);  //事务结束

    return 0;
}