Loadrunner中socket协议中的三个关联函数
這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文件中取值。這個一定要搞清楚。關于這個函數,這里再次提供下最原始的說明:
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. user:實例: char *ParamBuf = "(size=\x05)"? lrs_save_param_ex("socket0", "user", ParamBuf, 0, strlen(ParamBuf),"ascii", "size_param");? static實例: lrs_save_param_ex("socket0", "user", "buf1", 0, 23,"ascii", "param");? received實例: 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?:標識符描述
總結
以上是生活随笔為你收集整理的Loadrunner中socket协议中的三个关联函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Server 2008怎
- 下一篇: Uploadify——学习(1):在St