LoadRunner中常用的字符串操作函数有:

               strcpy(destination_string, source_string);

              strcat(string_that_gets_appended, string_that_is_appended);51Testing软件测试网:J3~c:c[(wR%A2l

              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 equal51Testing软件测试网q%USEK

对各函数的定义:

            strcpy( ):拷贝一个字符串到另一个字符串中.

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

            strcmp( ):比较两个字符串,如果相等返回0。

            atoi():转换一个ASCII字符串为一个整型。

            itoa():根据给定的进制,转换一个整型数据为ASCII字符串51Testing软件测试网D&VI2KD|

下面的例子使用了上面这些函数:     

Actions()

{

       char MyString1[] = "";

       char MyString2[] = "";

       char MyString3[] = "Mercury2";

       char Cstring[] = "";

       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 = ;

       itoa(Cint,Cstring,);

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

       return ;

}
LoadRunner字符串比较的常见错误

最近在论坛上看到有人提问LoadRunner如何对两个字符串进行比较,其脚本中两个字符串进行比较结果总是不一样的。我把问题整理了一下以便注意这个容易被忽略的错误。
脚本如下:
...
lr_save_string( "Hello World!","string1" );
lr_save_string( "Hello World!","string2" );
result = strcmp("string1","string2");
if ( result == 0 )
{
lr_output_message("the result is 0.");
}
else
{
lr_output_message("the result is not 0.");
}
大家可以看出脚本那里错了吗?
问题错在result = strcmp("string1","string2");这个上,这样变成了对字符串"string1"和"string2"的比较,而不是对变量的值进行比较,因此比较结果肯定是不一样的。 正确的写法有两种:
result = strcmp(&string1,&string2);
result = strcmp(lr_eval_string("{string1}"),lr_eval_string("{string2}"));
 Loadrunner的字符串处理函数 

 1)strcat编辑本段回目录

  char *strcat ( char *to, const char *from );
  功能:链接两个字符串。   例子:
  这个例子是用strcat链接字符串:Cheers_Lee和 @hotmail.com   脚本如下:
char test[1024], *a = "@hotmail.com";
strcpy(test, "Cheers_Lee");
strcat(test, a);
lr_output_message("We can see %s",test);   运行后在executon log中看到如下语句:
Starting action Action.
Action.c(16): We can see Cheers_Lee@hotmail.com   2)strchr编辑本段回目录   char *strchr ( const char *string, int c );
  功能:返回字符串中指定字符后面的字符串。   例子:
  这个例子是返回第一个出现e字符以后所有的字符,和最后一次出现e字符以后所有的字符。   脚本如下:
char *string = "Cheers is a tester";
char *first_e, *last_e;
first_e = (char *)strchr(string, 'e');
lr_output_message("We can see the first occurrence of e: %s",first_e);
last_e = (char *)strrchr(string, 'e');
lr_output_message("We can see the last occurrence of e: %s", last_e);   运行后在executon log中看到如下语句: Starting action Action.
Action.c(12): We can see the first occurrence of e: eers is a tester
Action.c(14): We can see the last occurrence of e: er    3)Strcmp&stricmp编辑本段回目录   int strcmp ( const char *string1, const char *string2 );大小写敏感。
  int stricmp ( const char *string1, const char *string2 );大小写不敏感。
  功能:比较字符串。   例子:
  按是否区分大小写对比两个字符串,并打印出它们的大小关系。   脚本如下:
int result;
char tmp[20];
char string1[] = "We can see the string:Cheers";
char string2[] = "We can see the string:cheers";
result = strcmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "大于" );
else if( result < 0 )
strcpy( tmp, "小于" );
else
strcpy( tmp, "等于" );
lr_output_message( "strcmp: String 1 %s string 2", tmp );
result = stricmp( string1, string2 );
if( result > 0 )
strcpy( tmp, "大于" );
else if( result < 0 )
strcpy( tmp, "小于" );
else
strcpy( tmp, "等于" );
lr_output_message( "stricmp: String 1 %s string 2", tmp );   运行后在executon log中看到如下语句: Starting action Action.
Action.c(22): strcmp: String 1 小于 string 2
Action.c(33): stricmp: String 1 等于 string 2 4)strcpy编辑本段回目录   char *strcpy ( char *dest, const char *source );
  功能:复制一个字符串到另一个字符串中。   例子:
  复制一个字符串到字符数组中,并打印出来。   脚本如下:
char test[1024];
strcpy(test, "what can we see?");
lr_output_message("%s", test);   运行后在executon log中看到如下语句: Starting action Action.
Action.c(10): what can we see?    5)Strdup& strlwr编辑本段回目录   char *strdup ( const char *string );
  功能:复制一个字符串。
  char *strlwr ( char *string );
  功能:转换成小写字母。   例子:
  在这个例子中,Vuser的组名被转换为小写字母。但是lr_whoami把组名作为静态buffer返回。这样的buffer不能被操作。如果有操作需要,就复制这个静态buffer。   脚本如下:
int id;
char *groupname_static, *groupname;
lr_whoami(&id, &groupname_static, NULL);
lr_output_message("groupname=%s", groupname_static);
groupname = (char *)strdup(groupname_static);
groupname = (char *)strlwr(groupname);
lr_output_message("lower case groupname=%s", groupname);
free(groupname);   上述脚本用vugen保存为:CHANGE
  在controller中运行(设置为总是发送消息)
  运行后在log中看到如下语句:
Starting action Action. [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change [MsgId: MMSG-17999]   6)Strlen编辑本段回目录   size_t strlen ( const char *string );
  功能:返回字符串长度(bytes).   例子:
  这个例子很简单,就是得到一个字符串中的字符的个数。然后打印出来。   脚本如下:
Starting action Action. [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change [MsgId: MMSG-17999]   运行后在log中看到如下语句: Action.c(13): The sentence has 18 letters 7)Strncat编辑本段回目录   char *strncat ( char *to_string, const char *from_string, size_t n );
  功能:把一个字符串连接到另一个字符串后面。   例子:
  在这里,我随便写了两个字符串,用此函数把他们连接起来,并打印出来。   脚本如下:
char str1[]="Cheers is ";
char str2[]="a tester.";
lr_output_message("What can we see?");
lr_output_message("The str1 is %s.",str1);
strncat(str1,str2,20);
lr_output_message("The str1 is %s.",str1);   运行后在log中看到如下语句: Action.c(9): What can we see?
Action.c(10): The str1 is Cheers is .
Action.c(13): The str1 is Cheers is a tester..   注:我们可以看到,没有连接前的str1是:Cheers is,连接后的字符串是:Zee is a tester。也可以看看strcat函数。   8)strncmp编辑本段回目录   int strncmp ( const char *string1, const char *string2, size_t n );
  功能:对比两个字符串的前n位。   例子:
  对比两个字符串,并把对比结果打印出来。这里我和上面的strcmp一起写。   脚本如下:
char result;
char str1[]="Cheers is a tester.";
char str2[]="Cheers is a tester.";
char str3[]="Cheers is a tester?";
result = strcmp(str1,str2);
if(result > 0)
lr_output_message("str1 is greater than str2.");
else if(result < 0)
lr_output_message("str1 is less than str2.");
else
lr_output_message("str1 is equal to str2.");
result = strncmp( str1, str3 , 30);
if(result > 0)
lr_output_message("str1 is greater than str3.");
else if(result < 0)
lr_output_message("str1 is less than str3.");
else
lr_output_message("str1 is equal to str3.");   运行后在log中看到如下语句:
Starting iteration 1.
Starting action Action.
Action.c(18): str1 is equal to str2.
Action.c(28): str1 is less than str3.
loadrunner比较有用的字符串函数

  strcat的串连两个字串。

  strchr返回指向第一次出现的字符串中的字符。

  STRCMP比较两个字符串来确定的字母顺序。

  STRCPY一个字符串复制到另一个地方。

  strdup重复一个字符串。

  stricmp执行区分大小写的比较两个字符串。

  strlen的返回一个字符串的长度。

  strlwr将字符串转换为小写。

  strncat函数串连?从一个字符串到另一个字符。

  STRNCMP比较两个字符串的前n个字符。

  strncpy一个字符串的前n个字符复制到另一个地方。

  strnicmp执行区分大小写的比较n个字符串。

  strrchr查找的字符串中的字符的最后一次出现。

  strset一个特定的字符填充一个字符串。

  strspn返回一个指定的字符串中包含的字符串中的前导字符的长度。

  strstr返回一个字符串第一次出现在另一个

把字符看成ASII的值 , 和数字比较大小一般,
if( strcmp(A,B) > 0 )  串A > 串B
if( strcmp(A,B) == 0 )   相同的串
if(strcmp(A,B) < 0 )  串A < 串B

int strcmp(char *str1, char *str2);

比较字符串str1和str2是否相同。如果相同则返回0;
如果不同,在不同的字符处如果str1的字符大于str2的字符,则返回1,否则返回-1
比如:
char a[]="abcd";
char *b="abcd";
char *d="abcde";
int d=strcmp(a,b);    //那么d的值是0
d=strcmp(b,d);   //d的值是-1 因为 '\0' 比'e' 小
d=strcmp(d,b);   //d的值是1,因为 'e' 比'\0'大

  if (strcmp(lr_eval_string("{username}"),"0")==0)
       lr_log_message("登录失败!");//或使用lr_output_message     else    lr_log_message("登录成功!");
注:username是判断参数列表取出的值
 
 
if(strcmp(lr_eval_string("{P_Value}"),",") != 0 && strcmp(lr_eval_string("{P_Value}"),".") != 0)

{

break;

}

LR中常用的C函数

        分类:             性能测试              2006-11-19 17:45     3786人阅读     评论(6)     收藏     举报    
注意:
1,将字符串中的数字转换为浮点数的函数,如果不事先声明,则转换有问题。
1       strcat
 
char *strcat ( char *to, const char *from );
 
功能:链接两个字符串。
 
例子:
 
这个例子是用strcat链接字符串:zee和slo@hotmail.co
 
脚本如下:
 
     char test[1024], *a = "slo@hotmail.com";
 
     strcpy(test, "zee");
 
     strcat(test, a);
 
     lr_output_message("We can see %s",test);
 
 
运行后在executon log中看到如下语句:
 
Starting action Action.
Action.c(16): We can see zeeslo@hotmail.com
 
2       strchr
char *strchr ( const char *string, int c );
 
功能:返回字符串中指定字符后面的字符串。
 
例子:
 
这个例子是返回第一个出现e字符以后所有的字符,和最后一次出现e字符以后所有的字符。
 
脚本如下:
     char *string = "Zee is a tester";
     char *first_e, *last_e;
 
     first_e = (char *)strchr(string, 'e');
     lr_output_message("We can see the first occurrence of e: %s",first_e);
     last_e = (char *)strrchr(string, 'e');
     lr_output_message("We can see the last occurrence of e: %s", last_e);
 
运行后在executon log中看到如下语句:
Starting action Action.
Action.c(12): We can see the first occurrence of e: ee is a tester
Action.c(14): We can see the last occurrence of e: er
3       Strcmp&stricmp
int strcmp ( constchar *string1, const char *string2 );大小写敏感。
int stricmp ( const char *string1, const char *string2 );大小写不敏感。
 
功能:比较字符串。
 
例子:
 
按是否区分大小写对比两个字符串,并打印出它们的大小关系。
 
脚本如下:
     int result;
     char tmp[20];
     char string1[] = "We can see the string:ZEE";
     char string2[] = "We can see the string:zee";
 
     result = strcmp( string1, string2 ); /*区分大小写,比较字符串 */
 
     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );
 
     lr_output_message( "strcmp: String 1 %s string 2", tmp );
 
     result = stricmp( string1, string2 ); /* 不区分大小写,比较字符串 */
 
     if( result > 0 )
          strcpy( tmp, "大于" );
     else if( result < 0 )
          strcpy( tmp, "小于" );
     else
          strcpy( tmp, "等于" );
 
     lr_output_message( "stricmp: String 1 %s string 2", tmp );    
 
运行后在executon log中看到如下语句:
Starting action Action.
Action.c(22): strcmp: String 1 小于 string 2
Action.c(33): stricmp: String 1 等于 string 2
  
4       strcpy
char *strcpy ( char *dest, const char *source );
 
功能:复制一个字符串到另一个字符串中。
 
例子:
 
复制一个字符串到字符数组中,并打印出来。
 
脚本如下:
 
     char test[1024];
 
     strcpy(test, "what can we see?       ");
 
     lr_output_message("%s", test);
 
运行后在executon log中看到如下语句:
Starting action Action.
Action.c(10): what can we see?      
5       Strdup& strlwr
char *strdup ( const char *string );
 
复制一个字符串。
 
char *strlwr ( char *string );
 
转换成小写字母。
 
例子:
 
在这个例子中,Vuser的组名被转换为小写字母。但是lr_whoami把组名作为静态buffer返回。这样的buffer不能被操作。如果有操作需要,就复制这个静态buffer。
 
脚本如下:
 
     int id;
     char *groupname_static, *groupname;
 
     /* 从VuGen中得到组名 */
     lr_whoami(&id, &groupname_static, NULL);
     lr_output_message("groupname=%s", groupname_static);
 
     /*复制这个静态组名以便我们可以操作它 */
     groupname = (char *)strdup(groupname_static);
     groupname = (char *)strlwr(groupname);
     lr_output_message("lower case groupname=%s", groupname);
 
     free(groupname);
上述脚本用vugen保存为:CHANGE
 
在controller中运行(设置为总是发送消息)
运行后在log中看到如下语句:
Starting action Action.    [MsgId: MMSG-15919]
Action.c(11): groupname=CHANGE     [MsgId: MMSG-17999]
Action.c(16): lower case groupname=change      [MsgId: MMSG-17999]
6       Strlen
size_t strlen ( constchar *string );
 
功能:返回字符串长度(bytes).
 
例子:
 
这个例子很简单,就是得到一个字符串中的字符的个数。然后打印出来。
 
脚本如下:
 
     char *str = "Zee is a tester";
     unsigned int len;
 
     len = strlen(str);
 
     lr_output_message("The sentence has %d letters",len);
 
运行后在log中看到如下语句:
Action.c(13): The sentence has 15 letters
7       Strncat
char *strncat ( char *to_string, const char *from_string, size_t n );
 
把一个字符串连接到另一个字符串后面。
 
例子:
 
在这里,我随便写了两个字符串,用此函数把他们连接起来,并打印出来。
 
脚本如下:
 
 
   char str1[]="Zee is ";
    char str2[]="a tester.";
    lr_output_message("What can we see?");
    lr_output_message("The str1 is %s.",str1);
 
    strncat(str1,str2,20);
lr_output_message("The str1 is %s.",str1);
 
 
运行后在log中看到如下语句:
Action.c(9): What can we see?
Action.c(10): The str1 is Zee is .
Action.c(13): The str1 is Zee is a tester..
注:我们可以看到,没有连接前的str1是:Zee is,连接后的字符串是:Zee is a tester。也可以看看strcat函数。
8       strncmp
int strncmp ( constchar *string1, const char *string2, size_t n );
 
对比两个字符串的前n位。
 
例子:
 
对比两个字符串,并把对比结果打印出来。这里我和上面的strcmp一起写。
 
脚本如下:
 
    char result;
    char str1[]="Zee is a tester.";
    char str2[]="Zee is a tester.";
    char str3[]="zee is a tester?";
   
    result = strcmp(str1,str2);
 
    if(result > 0)
         lr_output_message("str1 is greater than str2.");
    else if(result < 0)
         lr_output_message("str1 is less than str2.");
    else
         lr_output_message("str1 is equal to str2.");
  
    result = strncmp( str1, str3 , 30);
 
 
    if(result > 0)
         lr_output_message("str1 is greater than str3.");
    else if(result < 0)
         lr_output_message("str1 is less than str3.");
    else
         lr_output_message("str1 is equal to str3.");
 
运行后在log中看到如下语句:
Starting iteration 1.
Starting action Action.
Action.c(18): str1 is equal to str2.
Action.c(28): str1 is less than str3.
字符串判断用strcmp
请一下判断lr_eval_string("{test}")是否为空怎么写呢?
一样的,你定义一个空值字符串,然后用取到的值进行比较。 
char * mystr=" ";
char *str="";
int result;
result=strcmp(mystr,str);
lr_output_message("result is %d",result);

Loadrunner中C脚本设计知识积累(不断更新)

(2009-01-18 19:28:49)

前边已经发了不少关于Loadrunner中脚本编写的文章,现在发一个帖子,作为Loadrunner中C脚本编写一些该注意问题的帖子,作为对脚本编写知识的进一步巩固。

1.全局变量和局部变量

vuser_init() {

//Allocates a block of memory.   char * p = (char * )malloc(1000 * sizeof(char));  return ; }

------------------------------------------

Action() {  return ; }

------------------------------------------

vuser_end() {

//Frees a block of memory.

free(p);    return ; }

如上脚本所示:

最初的思路是在 vuser_init中定义指针p,并为其malloc函数申请1000个字节的内存空间,为了避免内存泄露,用户试图在vuser_end中释放p指向的内存空间,但free(p),由于变量p是在vuser_init中定义的,其作用域仅局限于vuser_init,离开了vuser_init,在vuser_end编译是会报错“p变量未被定义”。

这样我们的问题就产生了,那如何在vuser_init()、Action()、vuser_end()中使用全局变量呢?

这里我们就要用到全局变量,它是在函数外部定义的变量。它不属于哪一个函数,它属于一个源程序文件,其作用域是整个源程序。

在Loadrunner的HTTP协议录制的web系统时,会生成一个globals.h文件,在这里定义的变量相当于;Loadrunner脚本的全局变量,可以在vuser_init()、Action()、vuser_end()中被使用。

在globals.h文件中,添加全局变量的方法如下:

#ifndef _GLOBALS_H #define _GLOBALS_H

//-------------------------------------------------------------------- // Include Files #include "lrun.h" #include "web_api.h" #include "lrw_custom_body.h"

//-------------------------------------------------------------------- // Global Variables  包含全局变量

char * p;

#endif // _GLOBALS_H

注意:红色部分为添加的全局变量的位置;

然后,执行脚本,脚本就可以通过了,呵呵!

  2.lr_whoami() 使用的一点说明;

说明:lr_whoami()在VU中运行返回的值是:-1

举例:

Action() { int id, scid; char *vuser_group;

lr_whoami(&id, &vuser_group, &scid); lr_message( "Group: %s, vuser id: %d, scenario id %d",

vuser_group, id, scid);

return 0; }

执行脚本结果如下:

Virtual User Script started Starting action vuser_init. Web Turbo Replay of LoadRunner 9.0.0 for WINXP; WebReplay82 build 5727   [MsgId: MMSG-27143] Run-Time Settings file: "C:\Documents and Settings\Administrator\Local Settings\Temp\noname3\\default.cfg"   [MsgId: MMSG-27141] Ending action vuser_init. Running Vuser... Starting iteration 1. Starting action Action. Group: None, vuser id: -1, scenario id 0 Ending action Action. Ending iteration 1. Ending Vuser... Starting action vuser_end. Ending action vuser_end. Vuser Terminated.

这里需要说明的是:lr_whoami()在VU中运行返回的值是:-1(见结果绿色部分显示),只有在conctroller中多用户并发的时候才会输出正确的值。

 3.多个action()逻辑排序。

比如说,我们的一个脚本中有多个action,我们想改变执行顺序,我们可以这样来操作。

在VU菜单栏,Vuser  -  Run-Time Settings...- Run Logic中,通过 move up 、move down 改变action()执行的顺序。

 4.Loadrunner中检查点判断执行那些操作;

 web_reg_find("Text=ABC", "SaveCount=abc_count", LAST);

web_url("Step", "URL=...", LAST);

if (strcmp(lr_eval_string("{abc_count}"), "0") == 0)

Action A

else

Action B

  5.利用数组做冒泡排序法例子

Action() { int a[]={,,,,}; int i; int j; int temp;

for (i=;i<;i++) for (j=i+1;j<;j++)  if (a[i]>a[j]) {   temp=a[i];   a[i]=a[j];   a[j]=temp;   }  for (i=;i<;i++) {   lr_message("%d",a[i]);  }  return ; }

脚本:
vuser_init()
{

int result;
char string1;
char string2;
     lr_save_string( "We can see the string:zee","string1" );
     lr_save_string( "We can see the string:zee","string2" );

lr_output_message("the string1 is %s.",lr_eval_string("{string1}"));
lr_output_message("the string2 is %s.",lr_eval_string("{string2}"));

result = strcmp(lr_eval_string("{string1}"),lr_eval_string("{string1}"));  
   if ( result == 0 )
           {
        lr_output_message("the result is 0.");
        }
   else
   {
     lr_output_message("the result is not 0.");
   }

return 0;
}

结果:

Starting action vuser_init.
Web Turbo Replay of LoadRunner 8.1.0 for WINXP; Web build 4788          [MsgId: MMSG-27143]
Run-Time Settings file: "C:\Documents and Settings\Zee\Local Settings\Temp\noname26\\default.cfg"          [MsgId: MMSG-27141]
vuser_init.c(10): the string1 is We can see the string:zee.
vuser_init.c(11): the string2 is We can see the string:zee.
vuser_init.c(16): the result is 0.
Ending action vuser_init.
Running Vuser...

 

最新文章

  1. iOS开发Swift篇—(五)元组类型
  2. char与TCHAR相互转化
  3. Striiv Myland 攻略
  4. IAR修改工程名称Tab键设置模板建立
  5. 运输层协议----UDP
  6. 【COGS 56】质数取石子
  7. redis maxmemory设置
  8. 在cnblog中使用syntax方法
  9. Oracle之分组函数嵌套以及表连接
  10. 点评阿里JAVA手册之MySQL数据库 (建表规约、索引规约、SQL语句、ORM映射)
  11. 14、ABPZero系列教程之拼多多卖家工具 新建微信公众号模块
  12. JAVA面向对象-----main方法详解
  13. linux内核中的排序接口--sort函数
  14. [LeetCode] Find And Replace in String 在字符串中查找和替换
  15. 使用ThreadPoolExecutor进行多线程编程
  16. day51 JS基础
  17. git中的忽略配置文件中没有忽略该文件,却提交不到服务器上。
  18. tcp的粘包和拆包示例以及使用LengthFieldFrameDecoder来解决的方法
  19. Codeforces 1103 E. Radix sum
  20. 读取2007以上版本的excel(xslx格式)

热门文章

  1. python学习(十三)进程和线程
  2. python自学笔记(二)
  3. 通过ida dump Uinity3D的加密dll
  4. apk文件签名绕过
  5. Python基础之面向对象(初级篇)
  6. sweetAlert2
  7. C11线程管理:原子变量&amp;单调函数
  8. 【BZOJ】1468: Tree(POJ1741) 点分治
  9. 2017ACM暑期多校联合训练 - Team 6 1011 HDU 6106 Classes (容斥公式)
  10. lintcode 40. 用栈实现队列