Action() { //演示需要的一些变量,提前声明 char *name = "yezi_zh"; char *age = "30"; char *work = "engin"; char *cuntry = "中国"; char my_param[1000]; // 数组的大小根据自己拼接的字符串长度来定义,否则会出现内存错误 /**************************** * 需要的参数串为一下格式,这里只对汉字进行URL编码,英文字符不转为url编码, * name=yezi&age=30&work=engin&china=%e4%b8%ad%e5%9b%bd * *************************/ //汉字编码转换为url编码 EncodePlainToURL(cuntry,cuntry_urlencode); // 使用 srpintf 格式化需要传递的字符串 sprintf(my_param,"name=%s&age=%s&work=%s&cuntry=%s",name,age,work,cuntry_urlencode); //打印看看格式化的是否是需要的 lr_message(my_param); // 将已经存好变量的字符串转换成loadrunner使用的参数 lr_save_string(my_param,"loadrunner_param"); //调用参数,和普通参数使用没区别 web_url("function","url=http://ip:port/function?{loadrunner_param}",LAST); return 0; } //定义一个转换url字符的函数 void EncodePlainToURL(char *input_param, const char * output_param) { //将字符串编码转为UTF-8 lr_convert_string_encoding(input_param, LR_ENC_SYSTEM_LOCALE, LR_ENC_UTF8, "strUTF8"); //去掉C数组后面的null终止符 lr_save_string(lr_eval_string("{strUTF8}"), output_param); //将UTF-8字符串URL encode web_convert_param(output_param, "SourceEncoding=PLAIN", "TargetEncoding=URL", LAST); }
回放脚本
Virtual User Script started at : 2017/6/30 10:19:53 Starting action vuser_init. Web Turbo Replay of LoadRunner 12.0.0 for Windows 8; build 1362 (Feb 2 2014 23:20:03) [MsgId: MMSG-27143] Run mode: HTML [MsgId: MMSG-26993] Run-Time Settings file: "D:zyhVuGenUrlEncoding\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Maximum number of concurrent connections per server: 2 [MsgId: MMSG-26000] Starting action Action. Action.c(34): Notify: Saving Parameter "strUTF8 = 涓�浗x00". Action.c(36): Notify: Parameter Substitution: parameter "strUTF8" = "涓�浗x00" Action.c(36): Notify: Saving Parameter "cuntry_urlencode = 涓�浗". Action.c(38): web_convert_param started [MsgId: MMSG-26355] Action.c(38): Notify: Saving Parameter "cuntry_urlencode = %E4%B8%AD%E5%9B%BD". Action.c(38): web_convert_param was successful [MsgId: MMSG-26392] Action.c(16): Notify: Parameter Substitution: parameter "cuntry_urlencode" = "%E4%B8%AD%E5%9B%BD" name=yezi_zh&age=30&work=engin&cuntry=1.559239E-2714B80X1.138D80P-767D1.293305E-2315BBD Action.c(20): Notify: Saving Parameter "loadrunner_param = name=yezi_zh&age=30&work=engin&cuntry=%E4%B8%AD%E5%9B%BD". The param is name=yezi_zh&age=30&work=engin&cuntry=%E4%B8%AD%E5%9B%BD Action.c(26): Notify: Parameter Substitution: parameter "loadrunner_param" = "name=yezi_zh&age=30&work=engin&cuntry=%E4%B8%AD%E5%9B%BD" The param is name=yezi_zh&age=30&work=engin&cuntry=%E4%B8%AD%E5%9B%BD Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.
注:本案例需要一些c语言基础与loadrunner基础。
如果需要所有的字符串转化为url编码,需要调整代码的执行顺序。
Action() { //演示需要的一些变量,提前声明 char *name = "yezi_zh"; char *age = "30"; char *work = "engin"; char *cuntry = "中国"; char my_param[1000]; // 数组的大小根据自己拼接的字符串长度来定义,否则会出现内存错误 /**************************** * 需要的参数串为一下格式 *name%3dyezi_zh%26age%3d30%26work%3dengin%26cuntry%3d%e4%b8%ad%e5%9b%bd * *************************/ // 使用 srpintf 格式化需要传递的字符串 sprintf(my_param,"name=%s&age=%s&work=%s&cuntry=%s",name,age,work,cuntry); //打印看看格式化的是否是需要的 lr_message(my_param); //参数转换为url编码 EncodePlainToURL(my_param,"loadrunner_urlencode"); //调用参数,和普通参数使用没区别 web_url("function","url=http://ip:port/function?{loadrunner_urlencode}",LAST); lr_message("The param is %s",my_param); lr_message("The param is %s",lr_eval_string("{loadrunner_urlencode}")); return 0; }
回访脚本
Virtual User Script started at : 2017/6/30 10:34:29 Starting action vuser_init. Web Turbo Replay of LoadRunner 12.0.0 for Windows 8; build 1362 (Feb 2 2014 23:20:03) [MsgId: MMSG-27143] Run mode: HTML [MsgId: MMSG-26993] Run-Time Settings file: "D:zyhVuGenUrlEncoding\default.cfg" [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Maximum number of concurrent connections per server: 2 [MsgId: MMSG-26000] Starting action Action. name=yezi_zh&age=30&work=engin&cuntry=中国 Action.c(36): Notify: Saving Parameter "strUTF8 = name=yezi_zh&age=30&work=engin&cuntry=涓�浗x00". Action.c(38): Notify: Parameter Substitution: parameter "strUTF8" = "name=yezi_zh&age=30&work=engin&cuntry=涓�浗x00" Action.c(38): Notify: Saving Parameter "loadrunner_urlencode = name=yezi_zh&age=30&work=engin&cuntry=涓�浗". Action.c(40): web_convert_param started [MsgId: MMSG-26355] Action.c(40): Notify: Saving Parameter "loadrunner_urlencode = name%3Dyezi_zh%26age%3D30%26work%3Dengin%26cuntry%3D%E4%B8%AD%E5%9B%BD". Action.c(40): web_convert_param was successful [MsgId: MMSG-26392] The param is name=yezi_zh&age=30&work=engin&cuntry=中国 Action.c(28): Notify: Parameter Substitution: parameter "loadrunner_urlencode" = "name%3Dyezi_zh%26age%3D30%26work%3Dengin%26cuntry%3D%E4%B8%AD%E5%9B%BD" The param is name%3Dyezi_zh%26age%3D30%26work%3Dengin%26cuntry%3D%E4%B8%AD%E5%9B%BD Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.
代码只说明原理,具体使用根据数据来源来替换。
文中的变量在实际使用场景中多是关联获取的动态参数,结合参数的使用来使用本代码.
本文没写结合实际场景的参数使用,具有一定项目经验的学习者可以提升工具的使用熟练度。
建议:对于Loadrunner的使用,多使用F1查看帮助文档;
规定:在loadrunner中使用 c语言的变量,一定要在函数一开始定义,否则会出错(和内存有关的)。数组的定义需要根据实际情况来定义,c语言对内存的控制很严格,养成严谨的编码习惯很重要。