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

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

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

 51Testing软件测试网 G6oZ%sH!W$u

一,数据类型转换

1F@3RUD$V0

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

l*S,aJ.R%k?W0

1. 相似函数的输出在不同的位置51Testing软件测试网 b/w.^ Z'P.@U8l

象很多C函数一样,使用atoi函数的结果即为返回值

gT7xtT+`/y+P0

intResult = atoi( charY );

O-c}-x*L0

而:itoa的返回结果为第二个参数。51Testing软件测试网0~ R D;h+c-A

itoa( intX, charY, 10);51Testing软件测试网 afEg _b

  第一个参数是需要转换的数字,第二个参数是转换后存储的字符数组,需要注意的是数组必须定义为固定的长度,如:char chary[20]

,~zV LZ)cq&{'U0

数组的最大长度为3206432K),否则会出现“too many variables”编译错误。51Testing软件测试网l7J U7v8y7m+onzANy

如果定义为变长的字符串如char *charY,则程序会出错。

!i}(ud| b0

  第三个参数不是数组的长度,而是数字的基数,10进制是最常用的,其他还有二进制,八进制,十六进制。51Testing软件测试网w7{ i~*ZQ(Jv/p

2. 有一些函数实现了同样的功能

GCQTg M5K0

itoa不是一个标准的ANSI C函数但是是Cstdlib.h中的一个函数。所以它不被包括在unix机器上的LibC中。我们可以使用标准的sprintf函数来代替:51Testing软件测试网 BA+a Yv

sprintfcharY,“%d”,intX);

G pPN'v ML0

3. 是用%X来转换一个十六进制数51Testing软件测试网 j3dfBA Jcu*b

int intNum51Testing软件测试网4a~:RX8Cxz[

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

x"B Yfh.E?0

lr_output_message(“%d”,intNum); //打印65535 ,ffff的整数值51Testing软件测试网 M:C.?.Ly,vT4gQ0f

4. 从文本中提取数字的规则

yUK+h7_0

如果第一个字符不是数字或者为空,atoi返回0,即“e24”会返回0

8B-w Q?#d!x p@0

atoi转换一个非数字的字符会返回组成这个字符的数字,如“-3.2返回-3.0。“123XXX345”返回123

q mt#@ X6nz;L`K0

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

%|+b8E2`r8|;iX0

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

(] O#C9M?/P7q;iJ^0

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

Vp9|X/d_!_0

6. 参数的算术运算51Testing软件测试网3N$k!fu e2r ZxM

LoadRunner没有提供对参数的算术运算的函数。所以LR的参数必须:51Testing软件测试网&~%n&qR4zM

1) 转换成C的整数

'eY0o ~.Z0

2) 使用C的函数来运算最后返回一个C的字符串

D7c:L-@9E)w3I)F0

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

KlNS)v`3K.G ?0

char cBuf[10];

$fi4G)j?URY |0

int i;51Testing软件测试网K[+iu Ms#s3HNp6u

// 1. Evaluate parameter into a C integer:51Testing软件测试网U:t,h"},]1uK+U

i = atoi( lr_eval_string("{pNum_in}") );51Testing软件测试网"V!G'b+lN6s3D

// 2. Do the math and output the result to a C string:

|.@.T8Q[NC4{wG0

sprintf( cBuf, "%d", i+1);51Testing软件测试网KMBt6l0H;Q'[

// 3. Save the string as a parameter to be passed on:51Testing软件测试网.A FNKR+j:R

lr_save_string( cBuf, "pNum_out");51Testing软件测试网#` q,I$Aa.xf

//Print out the parameter value after incrementing it.51Testing软件测试网0Xqg!nLE

lr_message("**** Parameter from %s to %s",51Testing软件测试网)Mh}kw$OP-xZm

    lr_eval_string("{pNum_in}"));51Testing软件测试网-xXX[*}/^WYA

    lr_eval_string("{pNum_out}"));

;xu^6x(x:VUO0

zibeike注:除了对于数字类型的参数的运算之外,对于文本形式的参数的操作,可以参考我的另一篇文章的内容:http://www.51testing.com/?34866/action_viewspace_itemid_75592.html51Testing软件测试网5BG'lT^HI

二.字符串操作

:N5z0|.Yc0

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

[g/Bbg?0

Eak'} m!O J j(}0Error: "C interpreter runtime error - memory violation error during replay.51Testing软件测试网@h w^#Sb_7w

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

El)Z4F'iOE1G0
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连接两个字符串51Testing软件测试网F6Gr3\^

strchr返回指向第一个要查找的字符出现的位置的指针51Testing软件测试网MVw7]i L#k$D

strcmp比较两个字符51Testing软件测试网!M$j,Nm`2N

strcpy复制字符串到另一个

Qgro%CY&o1Qf0

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

B#PplI8kS"zV0

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

8K4IrnTz0

zibeike注:关于更多字符串操作的脚本编写,可以参考我的另一篇文章:

,Lb6t;eK*Sfv3]0

l u L*U!cT-~p%~"hz0http://www.51testing.com/?34866/action_viewspace_itemid_75428.html

4C^8N4b L'Nv0

zibeike翻译自:http://www.wilsonmar.com/1lrscrīpt.htm51Testing软件测试网3Q@/S:h`$Ql


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