loadrunner编写脚本
VUser_Init部分
这里是Vuser_init部分的一些例子:
下面显示了使用advapi32.dll的GetUserNameA函数获得的操作系统的用户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变量(如int或char)对其他部分的脚本是不可见的。所以使用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;
vuser_init(){
LPCSTR strUsertype; // Define *str.
str Usertype =lr_get_attrib_string("usertype");
if (strUsertype==NULL){
lr_output_message("### Run-time Settings Additional Attribute usertype not specified. Cannot continue.");
lr_abort();
}
else{
lr_message("### Run-time Settings Additional Attribute usertype=\"%s\"", strUsertype );
if( strcmp( strUsertype,"advanced") == 0 )
{ thinktime1=2; }
else
if
( strcmp( strUsertype,"intermediate") == 0 ){ thinktime1=4; }
else
}
if( strcmp( strUsertype,"basic") == 0 )
{ thinktime1=8; }
else
{
lr_error_message("### ERROR: Value not recognized. Aborting run." );
lr_abort();
}
}
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的名称和行数
lr_output_message("an output message");
例子:
·Actions.c (4): an output message
·//往输出日志和虚拟用户日志上发消息:
· lr_message("*** a message"
+"\r"+"A new line."
);
把");"放到另一行,这样可以容易的在命令上添加或者删除代码项。
在UNIX/Linux机器上,使用"\n"来添加一个换行。
在Windows机器上,使用"\r"来添加一个换行。
//往输出日志上发送不带action名称和行数的信息
lr_log_message("number\t"+ numvar +"\t");
//只给控制器上的虚拟用户状态区域发送信息(当在VuGen中运行时,只是简单的显示):lr_vuser_status_message("a vuser status message");
//给LoadRunner控制器或者Tuning模块的控制台输出窗口显示一个红色高亮度显示的-17999信息。lr_error_message("an error message");
TAG: