热爱测试,主要研究性能测试和自动化测试方面的技术,希望与同样对测试有热情的你一同进步成长

LoadRunner脚本编写(6)— 数据类型转换和字符串操作

上一篇 / 下一篇  2008-07-09 16:05:57 / 个人分类:性能测试

 

4j4V#Ul5Pe-z0

一,数据类型转换

DaW5n Ckp0

没有使用过C编程的LoadRunner脚本编写者会发现在数据类型转化方面比较困难。下面介绍这方面的知识。

5I"Wl[7C Er-x0

1. 相似函数的输出在不同的位置

:u#i,o~I/].|0

象很多C函数一样,使用atoi函数的结果即为返回值51Testing软件测试网-WQ-u b,?

intResult = atoi( charY );51Testing软件测试网)OC7z|Xc

而:itoa的返回结果为第二个参数。51Testing软件测试网R2R;fC3g Y

itoa( intX, charY, 10);

}Ms0ZkB1x0

  第一个参数是需要转换的数字,第二个参数是转换后存储的字符数组,需要注意的是数组必须定义为固定的长度,如:char chary[20]51Testing软件测试网7X"p(S-p ooG

数组的最大长度为3206432K),否则会出现“too many variables”编译错误。51Testing软件测试网@!kJ^a5Y

如果定义为变长的字符串如char *charY,则程序会出错。51Testing软件测试网W0] r,rR

  第三个参数不是数组的长度,而是数字的基数,10进制是最常用的,其他还有二进制,八进制,十六进制。

-n9X6N2C'H7B0

2. 有一些函数实现了同样的功能51Testing软件测试网O0dyB-I b nz x

itoa不是一个标准的ANSI C函数但是是Cstdlib.h中的一个函数。所以它不被包括在unix机器上的LibC中。我们可以使用标准的sprintf函数来代替:51Testing软件测试网.D7V#OY(n7[_s I!R

sprintfcharY,“%d”,intX);

%P?2~6{uIQ#P0

3. 是用%X来转换一个十六进制数

zNt h2[k0

int intNum

$`8{ t `g)m"]L0

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

ydP @4p%oRqv0

lr_output_message(“%d”,intNum); //打印65535 ,ffff的整数值51Testing软件测试网x:Y3B+_Z]

4. 从文本中提取数字的规则51Testing软件测试网Rx"a-T"K!J

如果第一个字符不是数字或者为空,atoi返回0,即“e24”会返回051Testing软件测试网p7t(i*~Ajy8K8Tuk

atoi转换一个非数字的字符会返回组成这个字符的数字,如“-3.2返回-3.0。“123XXX345”返回12351Testing软件测试网DUQ^j-u

5. LoadRunner脚本中的参数必须转换成C字符串。有两种方式来转化LR的参数为C语言的数字。

]%u8P8ri] H0

 i = atoi( lr_eval_string("{pX}") );51Testing软件测试网:Cf {*]7e ffE

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

E8A+Qa+]`4bF q0

6. 参数的算术运算51Testing软件测试网x^3`6D+G%o

LoadRunner没有提供对参数的算术运算的函数。所以LR的参数必须:51Testing软件测试网u vt+YN

1) 转换成C的整数51Testing软件测试网3rY5S(^H%z?

2) 使用C的函数来运算最后返回一个C的字符串51Testing软件测试网-s5ipxK7x+Aj

3) 把返回的字符串保存成参数

+d J;gq2^e8sm E0

char cBuf[10];

tTRQ_P-h0

int i;51Testing软件测试网$k!y#e3Or

// 1. Evaluate parameter into a C integer:51Testing软件测试网N Q4W+HD!BW

i = atoi( lr_eval_string("{pNum_in}") );51Testing软件测试网ZY4@:s t.p0C6w#b

// 2. Do the math and output the result to a C string:51Testing软件测试网8Y n|&?m

sprintf( cBuf, "%d", i+1);51Testing软件测试网F XAxNC]$n

// 3. Save the string as a parameter to be passed on:

+LpZSj*k9X-mS0

lr_save_string( cBuf, "pNum_out");51Testing软件测试网)V8Cn3?U;uq

//Print out the parameter value after incrementing it.

4[4W Y n Lg0

lr_message("**** Parameter from %s to %s",51Testing软件测试网 eRj"VsQ(\%E,]

    lr_eval_string("{pNum_in}"));51Testing软件测试网2aD?!}(L[7y cY n

    lr_eval_string("{pNum_out}"));51Testing软件测试网? n5j8^{s'S

zibeike注:除了对于数字类型的参数的运算之外,对于文本形式的参数的操作,可以参考我的另一篇文章的内容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html

`/L!e1G]-E0

二.字符串操作

DuK(WA g3d0

C语言中,字符串是固定长度的,因为他们本身由独立的字符组成的字符数组。数组是只读的。任何修改字符串长度的函数调用都会报错:

w[9x}&P5I$t&UE0

tXN`"gyu.M7`0Error: "C interpreter runtime error - memory violation error during replay.51Testing软件测试网#jo r.W[DW

LoadRunneras_web.h库中的字符串函数可以使用“prototyping”声明的方式读写内存:

z&a"Q(Yt&N(NH-m0I0
char *strtok(char *, char *); // tokenizer prototype
char *strstr(char *, char *); // substring prototype
char *strdup(char *); // String duplication prototype
float 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];
    
  // Create a parameter named pDate:
  lr_save_string("January 2, 2001", "pDate");
 
  // Put parameter into a string buffer:
  strcpy( aBuffer,lr_eval_string("{pDate}"));
 
  // Show the buffer for debugging:
  lr_output_message("%s\n",aBuffer);
 
  // get first word (to the first blank):
  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连接两个字符串

w9Z^(A7A5V3Y @0

strchr返回指向第一个要查找的字符出现的位置的指针51Testing软件测试网!x*B!};Li n9O"@9SS5Y

strcmp比较两个字符51Testing软件测试网-S ^;^)FFz](m @:z }

strcpy复制字符串到另一个

]ed L-u{W0

stricmp执行一个大小写敏感的比较

+y4{Y)B Oz0

其他还有strdupstrncatstrncpystrnicmpstrrchrstrsetstrspnstrstr等字符串操作的函数。

L/c"W G8OYY0

zibeike注:关于更多字符串操作的脚本编写,可以参考我的另一篇文章:51Testing软件测试网*B[9R_'A!Vz4z p

2w9[5[7W@ Z;H4F6z0http://www.51testing.com/?34866/action_viewspace_itemid_75428.html51Testing软件测试网"UH4w:jb9` f9k l

zibeike翻译自:http://www.wilsonmar.com/1lrscrīpt.htm51Testing软件测试网-p:Mk OPL


TAG: LoadRunner脚本编程 性能测试

jeffsui的个人空间--凭海临风的小筑 引用 删除 jeffsui   /   2013-08-22 16:32:47
3
比较狠的测试间 引用 删除 qiguojie   /   2008-07-30 11:31:50
收藏
引用 删除 achang21   /   2008-07-22 21:34:34
支持你,希望有更多的文章和大家共享^_^
 

评分:0

我来说两句

Open Toolbar