日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Loadrunner腳本編程(4)-數(shù)據(jù)類型操作和字符串操作

 山泉水清 2010-08-06

一,數(shù)據(jù)類型轉(zhuǎn)換

沒有使用過C編程的LoadRunner腳本編寫者會發(fā)現(xiàn)在數(shù)據(jù)類型轉(zhuǎn)化方面比較困難。下面介紹這方面的知識。

1.  相似函數(shù)的輸出在不同的位置

象很多C函數(shù)一樣,使用atoi函數(shù)的結(jié)果即為返回值

如intResult = atoi( charY );

而:itoa的返回結(jié)果為第二個參數(shù)。

itoa( intX, charY, 10);

   第一個參數(shù)是需要轉(zhuǎn)換的數(shù)字,第二個參數(shù)是轉(zhuǎn)換后存儲的字符數(shù)組,需要注意的是數(shù)組必須定義為固定的長度,如:char chary[20];

數(shù)組的最大長度為32064(32K),否則會出現(xiàn)“too many variables”編譯錯誤。

如果定義為變長的字符串如char *charY,則程序會出錯。

   第三個參數(shù)不是數(shù)組的長度,而是數(shù)字的基數(shù),10進制是最常用的,其他還有二進制,八進制,十六進制。

2.  有一些函數(shù)實現(xiàn)了同樣的功能

itoa不是一個標準的ANSI C函數(shù)但是是C的stdlib.h中的一個函數(shù)。所以它不被包括在unix機器上的LibC中。我們可以使用標準的sprintf函數(shù)來代替:

sprintf(charY,“%d”,intX);

sprintf

Writes formatted output to a string.

3.  是用%X來轉(zhuǎn)換一個十六進制數(shù)

int intNum;

sscanf(“ffff”,“%X”,&Num);

lr_output_message(“%d”,intNum);  // 打印65535 ,ffff的整數(shù)值

sscanf

Reads formatted input from a string.

4.  從文本中提取數(shù)字的規(guī)則

如果第一個字符不是數(shù)字或者為空,atoi返回0,即“e24”會返回0

atoi 轉(zhuǎn)換一個非數(shù)字的字符會返回組成這個字符的數(shù)字,如“-3.2”返回-3.0。“123XXX345”返回123。

Converts a string to an integer value.

atoi reads the initial portion of the string only, by stopping at the first non-numerical character.

 

5.  LoadRunner腳本中的參數(shù)必須轉(zhuǎn)換成C字符串。有兩種方式來轉(zhuǎn)化LR的參數(shù)為C語言的數(shù)字。

  i = atoi( lr_eval_string("{pX}") );

sprintf( intX, "%d", lr_eval_string("{pX}") );

lr_eval_string

Returns the string argument after evaluating embedded parameters.

The lr_eval_string function returns the input string after evaluating any embedded parameters. If string argument contains only a parameter, the function returns the current value of the parameter.

Embedded parameters must be in brackets.

6.  參數(shù)的算術(shù)運算

LoadRunner沒有提供對參數(shù)的算術(shù)運算的函數(shù)。所以LR的參數(shù)必須:

 

1)  轉(zhuǎn)換成C的整數(shù)

2)  使用C的函數(shù)來運算最后返回一個C的字符串

3)  把返回的字符串保存成參數(shù)

view plaincopy to clipboardprint?
char cBuf[10];   
 
int i;   
 
// 1. 轉(zhuǎn)換成C的整數(shù):  
 
i = atoi( lr_eval_string("{pNum_in}") );  
 
// 2. 使用C的函數(shù)來運算最后返回一個C的字符串:  
 
sprintf( cBuf, "%d", i+1);   
 
// 3.把返回的字符串保存成參數(shù):  
 
lr_save_string( cBuf, "pNum_out");   
 
//Print out the parameter value after incrementing it.  
 
lr_message("**** Parameter from %s to %s",  
 
     lr_eval_string("{pNum_in}"));   
 
     lr_eval_string("{pNum_out}"));  
char cBuf[10];

int i;

// 1. 轉(zhuǎn)換成C的整數(shù):

i = atoi( lr_eval_string("{pNum_in}") );

// 2. 使用C的函數(shù)來運算最后返回一個C的字符串:

sprintf( cBuf, "%d", i+1);

// 3.把返回的字符串保存成參數(shù):

lr_save_string( cBuf, "pNum_out");

//Print out the parameter value after incrementing it.

lr_message("**** Parameter from %s to %s",

     lr_eval_string("{pNum_in}"));

     lr_eval_string("{pNum_out}"));
 

zibeike注:除了對于數(shù)字類型的參數(shù)的運算之外,對于文本形式的參數(shù)的操作,可以參考我的另一篇文章的內(nèi)容:http://www./?34866/action_viewspace_itemid_75592.html

二.字符串操作

在C語言中,字符串是固定長度的,因為他們本身由獨立的字符組成的字符數(shù)組。數(shù)組是只讀的。任何修改字符串長度的函數(shù)調(diào)用都會報錯:

Error: "C interpreter runtime error - memory violation error during replay.

在LoadRunner的as_web.h庫中的字符串函數(shù)可以使用“prototyping”聲明的方式讀寫內(nèi)存:

char *strtok(char *, char *); // tokenizer prototypechar *strstr(char *, char *); // substring prototypechar *strdup(char *); // String duplication prototypefloat atof(); // alpha to return float datatype#include "as_web.h"char *strtok(char *, char *); // prototype function call. ActionX(){   char aBuffer[256]; // input string to be parsed.    char *cToken; // individual token from strtok.   char cSeparator[] = " "; // blank separator.   int i; // incrementer   char val[3][20]; // output array of strings.   char modified_val[20];        // 創(chuàng)建一個參數(shù)pDate:   lr_save_string("January 2, 2001", "pDate"); // 把參數(shù)放到字符串緩沖Put parameter into a string buffer:   //strcpy:Copies one string to another. //lr_eval_string:Returns the string argument after evaluating embedded parameters.    strcpy( aBuffer,lr_eval_string("{pDate}"));    //在調(diào)試中顯示這個buffer Show the buffer for debugging:  //lr_output_message:Sends a message to the log file and Output window   lr_output_message("%s\n",aBuffer);    // get first word (to the first blank):  //strtok:Returns a token from a string delimited by specified characters.     cToken = strtok( aBuffer,cSeparator);    i = 1;    if(!token) { // first token was not found:           lr_output_message("No tokens found in string!");           return( -1 );   } else {           while( cToken != NULL) { // tokens are not NULL:                   lr_output_message("Token=%s", cToken);                    // Stuff in another array:                   strcpy( val[i], cToken );                     // Get next token:                   cToken = strtok( NULL, cSeparator);                    i++; // increment            }           lr_output_message("Val #1 is: %s", val[1]);           lr_output_message("Val #2 is: %s", val[2]);           lr_output_message("Val #2 is: %s", val[3]);            strncpy( modified_val, val[2], 1 );           modified_val[2] = '\0';           while (modified_val[2] != NULL) {                   lr_output_message("===>%s", modified_val);                   modified_val[2] = strtok(NULL, " ");           }   }   return 0;} 

strcat 連接兩個字符串

strchr 返回指向第一個要查找的字符出現(xiàn)的位置的指針

strcmp 比較兩個字符

strcpy 復制字符串到另一個

stricmp 執(zhí)行一個大小寫敏感的比較

其他還有strdup,strncat,strncpy,strnicmp,strrchr,strset,strspn,strstr等字符串操作的函數(shù)。

zibeike注:關于更多字符串操作的腳本編寫,可以參考我的另一篇文章:

http://www./?34866/action_viewspace_itemid_75428.html

zibeike翻譯自:http://www./1lrscrīpt.htm

LoadRunner中常用的字符串操作函數(shù)有:

               strcpy(destination_string, source_string);

               strcat(string_that_gets_appended, string_that_is_appended);

               atoi(string_to_convert_to_int); //returns the integer value

               itoa(integer_to_conver_to_string, destination_string, base); // base is 10

               strcmp(string1, string2); // returns 0 if both strings are equal

對各函數(shù)的定義:

             strcpy( ):拷貝一個字符串到另一個字符串中.

             strcat( ):添加一個字符串到另一個字符串的末尾。

            strcmp( ):比較兩個字符串,如果相等返回0。

            atoi():轉(zhuǎn)換一個ASCII字符串為一個整型。

            itoa():根據(jù)給定的進制,轉(zhuǎn)換一個整型數(shù)據(jù)為ASCII字符串

下面的例子使用了上面這些函數(shù):

view plaincopy to clipboardprint?
               
 
Actions()  
 
{  
 
        char MyString1[20] = "";  
 
        char MyString2[20] = "";  
 
        char MyString3[20] = "Mercury2";  
 
        char Cstring[10] = "12345";  
 
        int Cint;  
 
   
 
   
 
        // MyString1 is empty   
 
        //  
 
        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);  
 
   
 
        // copy "Mercury1" into MyString1  
 
        //  
 
        strcpy(MyString1,"Mercury1");  
 
   
 
        // Now MyString1 contains "Mercury1"  
 
        //  
 
        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);  
 
   
 
   
 
        // Copy MyString3 into MyString2  
 
        //  
 
        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);  
 
        strcpy(MyString2,MyString3);  
 
        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);  
 
   
 
   
 
        // Catenate MyString2 to MyString1  
 
        //  
 
        strcat(MyString1,MyString2);  
 
        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);  
 
   
 
        // Cstring is converted to integer Cint  
 
        //  
 
        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);  
 
        Cint = atoi(Cstring);  
 
        lr_output_message(">>>>>>>>>> Cint = %d",Cint);  
 
   
 
        // Cint is converted to string  
 
        Cint = 100;  
 
        itoa(Cint,Cstring,10);  
 
        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);  
 
   
 
        return 0;  
 

            

Actions()

{

        char MyString1[20] = "";

        char MyString2[20] = "";

        char MyString3[20] = "Mercury2";

        char Cstring[10] = "12345";

        int Cint;

 

 

        // MyString1 is empty

        //

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

        // copy "Mercury1" into MyString1

        //

        strcpy(MyString1,"Mercury1");

 

        // Now MyString1 contains "Mercury1"

        //

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

 

        // Copy MyString3 into MyString2

        //

        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);

        strcpy(MyString2,MyString3);

        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);

 

 

        // Catenate MyString2 to MyString1

        //

        strcat(MyString1,MyString2);

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

        // Cstring is converted to integer Cint

        //

        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);

        Cint = atoi(Cstring);

        lr_output_message(">>>>>>>>>> Cint = %d",Cint);

 

        // Cint is converted to string

        Cint = 100;

        itoa(Cint,Cstring,10);

        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);

 

        return 0;

}


//To send an error message to the LoadRunner output window or Application Management agent log, use the lr_error_message function. It is not recommended that you send a message to the output window or agent log in the middle of a transaction, as it will lengthen the execution time. To send a message to the Vuser execution log or Application Management Web site, but not to the Output window, use lr_log_message.

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多