loadrunner 接口脚本编写

Posted JHBlogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了loadrunner 接口脚本编写相关的知识,希望对你有一定的参考价值。

  前段时间接触了一下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 接口脚本编写的主要内容,如果未能解决你的问题,请参考以下文章

jmeter用java代码怎样编写接口测试源码

jmeter用java代码怎样编写接口测试源码

Loadrunner录制脚本与编写脚本的区别

Loadrunner录制脚本与编写脚本的区别

LoadRunner接口脚本编写过程中遇到的问题及分享

Loadrunner:脚本编写通用模板(Http协议类型)