我前面的推文我们认识了LR性能测试工具的原理及基本使用,今天给大家分享性能测试常见的脚本开发函数,后续文章都会系统分享干货,带大家从0到1学会性能测试!
01strtok
通过指定的分隔符来对字符串进行切片,函数语法:
char *strtok( char *string, const char *delimiters ); 参数说明: string:表示需要切片的字符串 delimiters:表示分隔符 加入购物车实例: 加入购物车成功后会返回一串JSON格式的数据 {"error":0,"message":"","content":"\u8d2d\u7269\u8f66\uff08 1 \uff09<\/a>","goods_id":"","one_step_buy":0,"confirm_type":"3"} 将error后面的零取出来,然后判断这个ERROR码是否为0,如果为零表示业务成功,否则表示业务失 败 Action() { char separators[] = ":"; char *token; web_reg_save_param("add_to_cart", "LB=charset=utf-8\r\n\r\n", "RB=", LAST); web_custom_request("flow.php", "URL=http://192.168.0.128:8091/ecshop3/flow.php?step=add_to_cart", "Method=POST", "Resource=0", "RecContentType=text/html", "Referer=http://192.168.0.128:8091/ecshop3/goods.php?id=72", "Snapshot=t54.inf", "Mode=HTTP", "Body=goods={\"quick\":1,\"spec\": [],\"goods_id\":72,\"number\":\"1\",\"parent\":0}", LAST); token = (char *)strtok(lr_eval_string("{add_to_cart}"), separators); token = (char *)strtok(NULL, separators); lr_save_var(token,1, 0, "error_num"); //if(atoi(lr_eval_string("{error_num}")) == 0) if(strcmp(lr_eval_string("{error_num}"),"0") == 0) { lr_output_message("PASS"); } else { lr_output_message("FAIL"); } return0; }02lr_save_var
表示在原始字符串中取一个子串出来,并保存到一个参数,函数语法:
int lr_save_var( const char *param_value, unsigned long const value_len, unsigned long const options, const char *param_name); 参数说明: param_value:表示原始的字符串 value_len:表示取的子串的长度 options:表示参数选择,一般设置为0 param_name:表示将取出来的子串保存到一个参数中 实例: Action() { #define MAX_NAME_LEN 4 // Create the parameter, InName lr_save_string("Fitzwilliam", "InName"); // Save the first four character of "InName" to "ShortName" lr_save_var( lr_eval_string("{InName}"), MAX_NAME_LEN, 0, "ShortName"); return 0; } Output: Action.c(8): Notify: Saving Parameter "InName = Fitzwilliam" Action.c(10): Notify: Saving Parameter "ShortName = Fitz03strcmp
表示比较两个字符串是否相等,函数语法:
int strcmp( const char *string1, const char *string2 ); 如果string1 > string2,那么返回值为1 如果string1 = string2,那么返回值为0 如果string1 < string2,那么返回值为-1 字符串比较的原理: 1.首先比较第一个字符,如果两个字符相等,就再比较第二个字符 2.如果第一个字符不相同就有大小之分,如何区分谁大谁小,是比较ASCII码来确定 3.如果字符相等就一直往下比较,只到比较不相同为止 4.如果比较到最后一个字符还是相同的,那么就比较字符串的结束符'\0'04fopen
表示打开一个文件,函数的语法:
long fopen( const char *filename, const char *access_mode ); 参数说明: filename:表示文件名 access_mode:表示打开方式,通常打开文件包含以下方式: r:表示读的方式式打 w:表示打开后可写文件 a:表示执行 也可以是r+、w+、a+,"+"表示这个文件必须是已存在的 if ((file_stream = fopen(filename, "r")) == NULL) { lr_error_message ("Cannot open %s", filename); return -1; }05fread
表示读文件,函数语法:
size_t fread( void *buffer, size_t size, size_t count, FILE *file_pointer ); 参数说明: buffer:表示用来存储数据流 size:表示每次读取的字符大小 count:表示读取的字符数 file_pointer:表示文件指针 int count,total; char buffer[1000]; char *filename = "C:\\test.txt"; long file_stream; count = 0; total = 0; if((file_stream=fopen(filename,"r")) == NULL) { lr_error_message("can not open file:%s",filename); } else { while(!feof(file_stream)) //判断是否到文件尾 { count = fread(buffer,sizeof(char),1000,file_stream); if (ferror(file_stream)) //表示对文件的操作是否存在错误 { lr_output_message ("Error reading file %s", filename); break; } total = total + count; } } lr_output_message ("Total number of bytes read = %d", total ); if (fclose(file_stream)) lr_error_message ("Error closing file %s", filename);06Function的使用
使用LoadRunner来调用Function的目的是为了让程序变的更简单。其步骤如下:
1)定义一个Function
数据类型 函数名(数据类型 形参1,...,数据类型 形参n) 如果该函数没有返回值,那么数据类型应该为void { 函数体; } intadd(int x,int y) { return x + y; } voidadd(int x,int y) { lr_output_message("%d",x + y); }2)在action中调用自定义的Function
函数名(实参1,....实参n); res = add(1,2);3)添加.h的头文件
先创建一个.h的头文件 将函数的内容写入到.h的头文件中 .h的头文件可以放在两个不同的位置 一是:将写好的.h头文件拷贝到loadrunner安装目录下的include目录下 二是:将写好的.h头文件拷贝到当前脚本所保存的目录下 因为LoadRunner是自动去检索这两个目录下的头文件 如果添加到loadrunner安装目录下的include目录下有一个好处,就是如果有多个脚本需要调 用这个文件,那么不用将这个文件分别拷贝到不同的脚本下面。4)在globals.h中添加我们写好的.h头文件
#include"头文件名" 如 #include"add.h"实例:将加入购物车的功能封装成一个Function
将下面的代码拷贝到一个goods_add_cart.h的头文件中 voidadd_to_cart(int goods_id) { char goods_id_index[32]; itoa(goods_id,goods_id_index, 10); lr_save_string(goods_id_index, "goods_id_request"); web_custom_request("flow.php", "URL=http://192.168.0.119:8091/ecshop3/flow.php?step=add_to_cart", "Method=POST", "Resource=0", "RecContentType=text/html", "Referer=http://192.168.0.119:8091/ecshop3/goods.php?id=60", "Snapshot=t68.inf", "Mode=HTTP", "Body=goods={\"quick\":1,\"spec\":[],\"goods_id\": {goods_id_request},\"number\":\"1\",\"parent\":0}", LAST); } 下面是action调用add_to_cart函数的过程,并对传入的数据进行参数化 Action() { add_to_cart(atoi(lr_eval_string("{goods_id}"))); return0; }