您的位置:首页 > 编程语言 > C语言/C++

测试回顾版-第三讲:Loadrunner脚本 VS C语言

2009-02-23 23:57 344 查看
就目前的了解。Loadrunner的脚本语言其实和C没什么区别。他内部的好多机制都是C实现的。

不过是一种“类C”。

(至于说什么javavuser等我不熟悉,所以我的理解就是类C,也只是让大家熟悉“类C”编程)

所以我从几个方面分析

1:定义常量变量和C一样

2:LR函数的参数使用与C有点不一样,在LR中,C的变量和LR的参数是不一样的。
任何C的变量都不能被LR的函数直接调用。
应该用lr_eval_string来取值。

3:什么循环语句,选择语句都和C一样

4:一些函数的定义和C不一样。虽然名字一样,参数有不同

5:输入输出也有些不同。

所以重点要突破的地方就是理清参数和变量直接的关系。和多熟悉一LR些函数,其他就是C语言的知识了。

lr它有自己管理的变量,也叫参数,这种参数只能通过reg或者lr_save_方式定义,或者通过文件定义.

1:定义常量和变量

所有变量在C必须声明,才可以使用

浮点型

字符型

%d是用来表示一个整型值

%f浮点型。(不常见)。

同样,%ç对应字符值。

在LoadRunner中lr_output_message相当于C中的printf的。输出将被印在执行日志中Vugen窗口。

inta=5;


floatx=3.14;

charc=‘A’;


lr_output_message(“Thevaluesare:a=%d,x=%f,c=%c”,a,x,c);


1.分支构造
a.if

b.switch

2.循环构造
a.for

b.while

c.do

逻辑运算

Symbol
Meaning
<
LESSTHAN
<=
LESSTHANOREQUALTO
>
GREATERTHAN
>=
GREATERTHANOREQUALTO
==
EQUALTO
&&
AND
||
OR
!
NOT
·break-退出形式循环或开关。Thisappliesonlytofor/whileloops.这仅适用于为/while循环。TobreakoutofaLoadRunneriteration,useexit(-1);打破了LoadRunner的迭代,使用exit(-1);

continue--skip1iterationofloop.继续-跳到1迭代循环。Thisappliesonlytofor/while.这仅适用于为/时。TocontinuetothenextiterationinLoadRunneriterations,usereturn0.要继续到下一个迭代的LoadRunner的重复,请使用返回0。

//Globalvariablecount

intcount=0;


//Globalvariablecount

intcount=0;


Actions()

{

//Incrementcount

//

count++;


//Ifcountisequalto10,exitthescript

//

if(count==10)

exit(-1);


//Ifcountiseven,skipthecurrentiteration

//andcontinuewiththenextiteration

//

if(count%2==0)

return0;


lr_output_message(">>>>>>>>>>>>>>>>>>>>>>%d",count);


return0;


字符串处理

strcpyisusedtocopyonestringintoanother.

strcatisusedtoaddonestringtotheendofanother.
strcmpcomparestwostringsandreturns0ifbothstringsareidentical.
atoiconvertsanASCIIstringtoaninteger.
itoaconvertsanintegertoanasciistringforagivenbase.Syntax:

随机数

Actions()

{

inti;


//srand(time(NULL));


for(i=0;i<10;i++)

lr_output_message(">>>>>>>>>>RandomNumber:%d",rand());


}


LoadRunner运行原理浅析

Actions()

{

charMyString1[20]="";

charMyString2[20]="";

charMyString3[20]="Mercury2";

charCstring[10]="12345";

intCint;



//MyString1isempty

//

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


//copy"Mercury1"intoMyString1

//

strcpy(MyString1,"Mercury1");


//NowMyString1contains"Mercury1"

//

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



//CopyMyString3intoMyString2

//

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

strcpy(MyString2,MyString3);

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



//CatenateMyString2toMyString1

//

strcat(MyString1,MyString2);

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


//CstringisconvertedtointegerCint

//

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

Cint=atoi(Cstring);

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


//Cintisconvertedtostring

Cint=100;

itoa(Cint,Cstring,10);

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


return0;

}



多维数组
文件处理

LoadRunnerdoesnotsupporttheFILEdatatype.Sowecandeclarethefileasanintegertype.

intMyFile;

intMyFile;
MyFile=(int)fopen(“C://temp//loans.txt”,”w”);//InLoadRunner

intMyFile;
intLoanNumber;
MyFile=fopen(“C://temp//loans.txt”,”r”);
fscanf(MyFile,”%d”,&LoanNumber);

intMyFile;
charName[]=“JohnDoe”;
MyFile=fopen(“C://temp//loans.txt”,”w”);//notethat“w”isusedtowrite
fscanf(MyFile,”%s”,Name);//notethatweareprintingastringhere

函数

引用文件

#include"as_web.h".as_web.hi

MiscellaneousFunctions



Actions()

{

system("delc://temp//temp.txt");

return0;

}


Actions()
{
intMyFile;
charName[]="JohnDoe";

MyFile=fopen("C://temp//names.txt","w");
//notethat"w"isusedtowrite

fprintf(MyFile,"%s",Name);
//notethatweareprintingastringhere

fclose(MyFile);
return0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: