1. 程式人生 > >loadrunner 接口腳本編寫

loadrunner 接口腳本編寫

return inf token source login headers targe charset user

  前段時間接觸了一下loadrunner的接口性能測試,然後嘗試了一下手動編寫腳本,畢竟錄制這種東西,不是每次都能通的,而且錄制下來的腳本,通常是有很多其他雜七雜八的請求夾雜在中間,沒有達到真正的壓測接口的目的;

  loadrunner默認的腳本是C語言的,但其實它支持目前所有主流的語言,如:Java User、VB User、VB script User 、Javascript User等。

  下面是嘗試過的幾種post接口的請求方式:

Action()  
{  

    //參數化;
    lr_save_string("0b5628f2326657017903hhg", "requestId
"); lr_save_string("Android", "platform"); lr_save_string("a8ika09", "deviceId"); lr_save_string("150173375600", "timestamp"); lr_save_string("official", "memberChannel"); lr_save_string("v1.0.0", "version"); //保存下面接口返回的參數; web_reg_save_param ("S_respond","LB=","RB=",LAST);
//獲取appKey並保存為參數變量 web_reg_save_param ("appKey","LB=appKey","RB=}",LAST); web_reg_save_param("Connection","LB=Connection","RB=X",LAST); //獲取headers參數 web_save_header(REQUEST, "RequestHeader");// REQUEST為內置變量,保存請求的頭信息,需在發送URL請求前註冊使用,將請求頭信息存入字符串RequestHeader中 web_save_header(RESPONSE, "ResponseHeader
");// 將RESPONSE保存響應的頭信息在字符串ResponseHeader中 web_add_header("Content-Type","application/json; charset=UTF-8"); lr_start_transaction("GetAPPKey"); web_custom_request("app-keys", "URL=http://14.119.110.171/cbclient/app-keys", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Resource=0", "Referer=", "Mode=HTML",//HTML或者HTTP都可以 //"EncType=application/x-www-form-urlencoded", //編碼類型 //"EncType=application/json", //指定響應頭的Content-Type,這裏是JSON //"RecContentType=application/json", //指定請求頭的Content-Type,這裏是JSON //"Snapshot=t10.inf", "Body={" "\"requestId\":\"{requestId}\"," "\"platform\":\"{platform}\"," "\"deviceId\":\"{deviceId}\"," "\"timestamp\":\"{timestamp}\"," "\"memberChannel\":\"{memberChannel}\"," "\"version\":\"{version}\"" "}", //"Body=&requestId=aimaii1212u19283nfna&platform=Android&deviceId=a8ika09109&timestamp=8291377319798173&memberChannel=official&version=v1.0",此傳參方法不work。 LAST); lr_end_transaction("GetAPPKey", LR_AUTO); //web_find ("Text=\"ret\":true",LAST); //對響應報文進行轉碼 lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond"); //打印轉碼後的響應報文 lr_output_message ( "all respond is %s",lr_eval_string ("{respond}")); lr_output_message ( "HEANDER IS %s",lr_eval_string ("{RequestHeader}")); lr_output_message ( "HEANDER IS %s",lr_eval_string ("{ResponseHeader}")); lr_output_message ( "\n\nConnection IS %s",lr_eval_string ("{Connection}")); lr_start_transaction("login"); web_reg_save_param ("S_respond","LB=","RB=",LAST); web_add_header("Content-Type","application/json; charset=UTF-8"); lr_save_string("0b5628f2326657017903190016", "requestId"); web_custom_request("login", "URL=http://14.119.110.171/cbclient/user/login/mobile", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Resource=0", "Referer=", "Mode=HTML",//HTML或者HTTP都可以 //"EncType=application/x-www-form-urlencoded", //編碼類型 //"EncType=application/json", //指定響應頭的Content-Type,這裏是JSON //"RecContentType=application/json", //指定請求頭的Content-Type,這裏是JSON //"Snapshot=t10.inf", "Body={" "\"requestId\":\"{requestId}\"," "\"platform\":\"{platform}\"," "\"deviceId\":\"{deviceId}\"," "\"timestamp\":\"{timestamp}\"," "\"memberChannel\":\"{memberChannel}\"," "\"version\":\"{version}\"," "\"appKey\":\"{appKey}\"," "\"sign\":\"1234\"," "\"data\":{\"mobile\":\"{mobile}\",\"password\":\"123456\"}" "}", LAST); lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond"); //打印轉碼後的響應報文 lr_output_message ( "%s",lr_eval_string ("{respond}")); lr_log_message("\n\requestId is :%s",lr_eval_string("{requestId}")); lr_output_message ( "appKey is %s",lr_eval_string ("{appKey}")); lr_end_transaction("login", LR_AUTO); /* lr_start_transaction("query"); web_reg_save_param ("S_respond","LB=","RB=",LAST); web_add_header("Content-Type","application/json; charset=UTF-8"); web_custom_request("query", "URL=http://14.119.110.171/cbclient/", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Resource=0", "Referer=", "Mode=HTML",//HTML或者HTTP都可以 //"EncType=application/x-www-form-urlencoded", //編碼類型 //"EncType=application/json", //指定響應頭的Content-Type,這裏是JSON //"RecContentType=application/json", //指定請求頭的Content-Type,這裏是JSON //"Snapshot=t10.inf", "Body={" "\"requestId\":\"{requestId}\"," "\"platform\":\"{platform}\"," "\"deviceId\":\"{deviceId}\"," "\"timestamp\":\"{timestamp}\"," "\"memberChannel\":\"{memberChannel}\"," "\"version\":\"{version}\"," "\"appKey\":\"{appKey}\"," "\"sign\":\"1234\"," "\"accessToken\":\"{accessToken}\" "}", LAST); lr_convert_string_encoding ( lr_eval_string ("{S_respond}"),LR_ENC_UTF8 ,NULL,"respond"); //打印轉碼後的響應報文 lr_output_message ( "%s",lr_eval_string ("{respond}")); lr_log_message("\n\nusername is :%s",lr_eval_string("{requestId}")); lr_end_transaction("query", LR_AUTO); */ return 0; }

loadrunner 接口腳本編寫