`
liyiye
  • 浏览: 416008 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

loadrunner学习系列---脚本编写(2)

阅读更多

今天接着翻译http://www.wilsonmar.com/1lrscrīpt.htm上面关于LR脚本编写部分.

VUser_Init部分

这里是Vuser_init部分的一些例子:

操作系统User ID

下面显示了使用advapi32.dllGetUserNameA函数获得的操作系统的用户ID

        char    sUserID[1024]; // Maximum possible UserID length.

        long    lUserIDSize = sizeof(sUserID)-1;

        int     rc;

 

        rc=lr_load_dll("advapi32.dll");

        if( rc != 0 ){

               lr_error_message("lr_load_dll of advapi32.dll failed. Aborted for rc=%d",rc);

               lr_abort();

        }else{

               GetUserNameA(sUserID, &lUserIDSize);

               lr_message("UserID='%s'", sUserID);

        }

所有的变量声明需要一块放到最上方。在vuser_init 部分创建的本地C变量(如 intchar)对其他部分的脚本是不可见的。所以使用lr_save_string函数来创建对所有脚本可用的全局参数。例子:

  

char *itoa ( int value, char *str, int radix );

vuser_init(){

        int x = 10;

        char buffer[10];

        lr_save_string( itoa( x, buffer, 10) , "pX" );

        lr_message ( "int x = %s", lr_eval_string("{pX}" ));

return 0;

}

 

 

运行时设置的附加属性(Additional Attribute

  8.0版本引进了一个非常有价值的特性:在运行时设置中指定属性,这个属性可以对不同的虚拟用户组设置不同的值。

 

 

 下面的代码是从运行时设置的附加属性中读取名为“usertype”的参数。然后使用参数值来对应的设置全局的"thinktime1"变量。

  

int thinktime1=0; 51Testing软件测试网Y[1]D9d
wN$z[1]P+M

vuser_init() 51Testing
软件测试网%YuP(i(j F^"O1d `L
{


 

 

LPCSTR strUsertype; // Define *str. 51Testing软件测试网8o[1]@(FQ0L;IC
strUsertype = lr_get_attrib_string("usertype"); 51Testing
软件测试网n^(m#{N1mjb
if (strUsertype==NULL){

lr_output_message("### Run-time Settings Additional Attribute usertype not specified. Cannot continue.");
a

G0A0s3b C"^:SeH(O$p0o0lr_abort();


 

 

}else{51Testing软件测试网T0{9D:|
~

lr_message("### Run-time Settings Additional Attribute usertype=\"%s\"", strUsertype );51Testing软件测试网6~ e%{ c?'x @ C
if( strcmp( strUsertype,"advanced") == 0 ){ thinktime1=2; }
r4?r#F#IB
M|!Ln0
else

To[1]m$_ KtQY0
if( strcmp( strUsertype,"intermediate") == 0 ){ thinktime1=4; }51Testing
软件测试网Cv;bz V*M$[
h[1]B

else
 }^"q$f|)w0if( strcmp( strUsertype,"basic") == 0 ){ thinktime1=8; }51Testing
软件测试网'r$pb%Q
e,?0dx.Y
S

else{
%m4T
w0j3b&Gh/x,m0


 


 


 


 

 

lr_error_message("### ERROR: Value not recognized. Aborting run." );

Js"V_*L0lr_abort();

}

}51Testing软件测试网Tt,p^WN[1]H
return 0;

}

 

Time Structure Fix(不知道怎么翻译,呵呵,时间结构的解决?)

根据知识库34195的文章,默认当前时间戳的毫秒部分不被更新,除非ftime使用的时间结构被重新定义:

 

   

typedef long time_t;

struct _timeb {

   time_t time;

   unsigned short millitm;

   short timezone;

   short dstflag;

};

struct _timeb t;

_tzset(); \\ 使用ftime设置变量

_ftime( &t );

lr_message( "Plus milliseconds: %u", t.millitm );

控制信息的显示:

在运行时,当脚本的事务失败后继续,你怎么知道哪个用户失败了?

  在每个失败的事务之后,发出一个能够唯一确定该用户的信息。

Loadrunner提供了一些函数来在运行时显示信息:

  

·  // 往输出日志上发送消息,这个消息前边会带有action 的名称和行数51Testing软件测试网!O(u­K `­hA(? s
  lr_output_message("an output message");


 

 

   例子:

·  Actions.c (4): an output message

·  // 往输出日志和虚拟用户日志上发消息:
|;}

e9E:G0

·    lr_message("*** a message"

+"\r"+"A new line."

);

  ");"放到另一行,这样可以容易的在命令上添加或者删除代码项。

   UNIX/Linux机器上,使用 "\n"来添加一个换行。

   Windows 机器上,使用"\r"来添加一个换行。

// 往输出日志上发送不带action名称和行数的信息
O3X-Np,O4ZZ0
lr_log_message("number\t"+ numvar +"\t");

 

// 只给控制器上的虚拟用户状态区域发送信息(当在VuGen中运行时,只是简单的显示): 51Testing软件测试网"o­C:d
L9^

lr_vuser_status_message("a vuser status message");

 

// LoadRunner控制器或者Tuning模块的控制台输出窗口显示一个红色高亮度显示的-17999 信息。51Testing软件测试网*[

p
^C
mA*j

lr_error_message("an error message");

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics