51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1810|回复: 0
打印 上一主题 下一主题

[原创] loadrunner中socket协议中的关联函数

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-7-18 11:23:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
首先先看下3个函数的原始定义:
也可以通过我的CSDN博客进行阅读:http://blog.csdn.net/cafardhaibin/article/details/6607827
------------------------------------------------------------------------------------------------------------
首先先看下3个函数的原始定义:
        lrs_save_searched_string:Searches for an occurrence of strings in a static or received buffer and saves a portion of the buffer, relative to the string occurrence, to a parameter.
        lrs_save_param:Saves data from a static or received buffer to a parameter.   
        lrs_save_param_ex :Saves data from a static, received, or user buffer to a parameter.The lrs_save_searched_string function saves a portion of a buffer to a parameter. This function is used for correlating or linking statements within a script. This function extends the functionality of lrs_save_param.



        原文:概念就不解释了,主要看下3个函数的区别。请注意除了:lrs_save_param_ex保存user buffer以外,其他两个都保存的是静态或者received buffer数据。这就是说当使用其他两个时是取不到每次变化的值的。获取的都是我们data.ws文件中的数据。

                 纠正:因为自己理解肤浅,在原文中进行了如上错误的描述。如果前面有已经阅读了的读者,再次致以歉意。这3个函数其实都可以动态获取运行中收到的数据包中的数据,只要跟在要获取的收取数据包脚本后面即可。其中:lrs_save_searched_string和lrs_save_param如果buf_desc指明buf名称,则从我们录制的data.ws中获取数据,所以每次这个值都是固定值,不会改变的;比如订单提交的确认信息等;而如果:lrs_save_searched_string和lrs_save_param参数buf_desc设置为:NULL,则从脚本中每次返回的数据包中获取数据,这个数据也随着每次脚本的动态运行而变化,比如某个不断递增的ID号等。而:lrs_save_param_ex 的buf_desc有3个参数,其中static指的是从我们在脚本中定义的一个静态变量中取值,而received指的是从收到的数据包取值;而user指从data.ws文件中取值。这个一定要搞清楚。关于这个函数,这里再次提供下最原始的说明:      
view plain
type   
  
The type of buffer data to save into the parameter. Possible values:   
  
user        - save the user data,  
static        - save the static buffer that was originally recorded,  
received        - save the buffer(s) received by the last call to lrs_receive.   
  
buff   
  
Depends on the type of buffer data specified in the type parameter.   
For user               the pointer to the user buffer,  
For static       -               the descriptor of the buffer in the data file,  
For received       the buff parameter is ignored.   
view plain
<strong>user:实例:</strong>  
view plain
char *ParamBuf = "(size=\x05)"   
lrs_save_param_ex("socket0", "user", ParamBuf, 0, strlen(ParamBuf),"ascii", "size_param");   
view plain
<strong>static实例:</strong>  
view plain
<span style="color: rgb(0, 102, 0); "></span><pre name="code" class="cpp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">lrs_save_param_ex("socket0", "user", "buf1", 0, 23,"ascii", "param");   
view plain
<strong>received实例:</strong>  
view plain
<span style="color: rgb(0, 102, 0); font-family: monospace; white-space: pre; "></span><pre name="code" class="cpp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">lrs_save_param_ex("socket0", NULL, "buf1", 0, 23,"ascii", "param");   

        ---------------------------------------------------------------------------------------------------
   
        //实例1:
        lrs_save_searched_string("socke0","buf1","wtbh","LB/BIN=|","RB/BIN=|",2,0,-1);

        //输出结果: "wtbh = 10000013" 。原始数据为:"0|普通指令下达成功|10000031|"。那就是在以左边界和右边界第2次出现的地方,
        //获取左右边界里面的所有的值

        
        //实例2:
        lrs_save_searched_string("socke0","buf1","wtbh","LB/BIN=|","RB/BIN=|",1,0,-1);

        //输出结果:"wtbh = 普通指令下达成功"原始数据为:"0|普通指令下达成功|10000031|"。那就是在以左边界和右边界第1次出现的地方,
        //获取左右边界里面的所有的值

        
        //实例3:
        lrs_save_searched_string("socke0","buf1","wtbh","LB/BIN=|","RB/BIN=|",1,4,-1);

        //输出结果:"wtbh = 指令下达成功"原始数据为:"0|普通指令下达成功|10000031|"。那就是在以左边界和右边界第1次出现的地方,
        //获取左边界为起点的第4个字符后面的数据。

        
        //实例4:
        lrs_save_param("socket0","buf1","wtbh", 34, 8);
        
        //从数据包中第34个字符开始连续取8个字符。
        
        //实例5:
            
        char * ReceivedBuffer;
        
        int iLen = 0;
        
        int iFor = 2;
        
        lrs_get_last_received_buffer("socket0", &ReceivedBuffer, &iLen);
        
        lrs_save_param_ex("socket0", "user", ReceivedBuffer, 0, 43, "ascii", "test_para");
        
        lrs_free_buffer(ReceivedBuffer);

        strZhwth = lr_eval_string("<test_para>");
        
        strZhwth = (char *)strtok(strZhwth,"|");
        
        while(iFor > 0)
        {
            iFor = iFor - 1;
            strZhwth = (char *)strtok(NULL,"|");//获取下一个token
        }
        //通过上面的循环获取了第2个“|”后面的字符,并且保存到strZhwth中
        lrs_save_param_ex("socket0", "user", strZhwth, 0, 8, "ascii", "Zhwth");
        //在上面获取字符中从第1位开始连续获取8个字符。
        //函数实例6:
        函数:strtok
        说明:Returns a token from a string delimited by specified characters.
        定义:char *strtok ( char *string, const char *delimiters );
        参数:string: The string to scan.  delimiters: The string consisting of the character or characters that delimit tokens in the first string.
        实例:
       extern char * strtok(char * string, const char * delimiters ); // Explicit declaration
       char path[] = "c:\\mercury\\lrun\\bin\\wlrun.exe";
       char separators[] = ".\\";
       char * token;
       token = (char *)strtok(path, separators); // Get the first token
       if (!token) {
              lr_output_message ("No tokens found in string!");
              return( -1 );
       }
       while (token != NULL ) { // While valid tokens are returned
              lr_output_message ("%s", token );
              token = (char *)strtok(NULL, separators); // Get the next token
       }
        Output:
        Action.c(18): c:
        Action.c(18): mercury
        Action.c(18): lrun
        Action.c(18): bin
        Action.c(18): wlrun
        Action.c(18): exe
        //函数实例7:
        函数:strstr
        说明:Returns the first occurrence of one string in another.
        定义:char *strstr ( const char *string1, const char *string2 );
        参数:string1:The string that is searched.  string2:The string that is searched for in the first string.  
        实例1:
        lrs_save_param_ex("socket0", "user", ReceivedBuffer_cjhb, 0, iLenCj, "ascii", "cjhb");
        lrs_free_buffer(ReceivedBuffer_cjhb);
        if(strstr(lr_eval_string("<cjhb>"), "已成交") != NULL)
        return 0;
        实例2:
        After strstr returns the address, position, the code then calculates the word's place in str by subtracting the address of the start of the string from position. This is the offset of the word "dog", in bytes.

       int offset;
       char * position;
       char * str = "The quick brown dog jumps over the lazy fox";
       char * search_str = "dog";
       position = (char *)strstr(str, search_str);

       // strstr has returned the address. Now calculate * the offset from the beginning of str
       offset = (int)(position - str + 1);
       lr_output_message ("The string \"%s\" was found at position %d", search_str, offset);
        Output:
        Action.c(14): The string "dog" was found at position 17

        //函数实例8:
        函数:strcmp
        说明:Compares string1 and string2 to determine the alphabetic order.
        定义:int  ( const char *string1, const char *string2 );
        参数:string1  The first string that is compared.  string2  The second string that is compared.strcmp compares string1 and string2 to determine the alphabetic order.   
        实例1:
        The following example compares two strings, string1 and string2, which are identical except for the word "quick" which is lowercase in string1 and uppercase in string2. strcmp, which is case-sensitive, returns an unequal comparison.
       int result;
       char tmp[20];
       char string1[] = "The quick brown dog jumps over the lazy fox";
       char string2[] = "The QUICK brown dog jumps over the lazy fox";
       result = strcmp( string1, string2); // Case-sensitive comparison
       if(result > 0)
              strcpy(tmp, "greater than");
       else if(result < 0)
              strcpy(tmp, "less than");
       else
              strcpy(tmp, "equal to");
       lr_output_message ("strcmp: String 1 is %s string 2", tmp);
       result = stricmp(string1, string2 ); // Case-insensitive comparison
       if( result > 0 )
              strcpy( tmp, "greater than" );
       else if( result < 0 )
              strcpy( tmp, "less than" );
       else
              strcpy( tmp, "equal to" );
       lr_output_message( "stricmp: String 1 is %s string 2", tmp );
        Output:
        Action.c(17): strcmp: String 1 is greater than string 2
        Action.c(28): stricmp: String 1 is equal to string 2

        单词:descriptor identifying :标识符描述
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-6 00:39 , Processed in 0.065899 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表