转:Loadrunner日积月累

上一篇 / 下一篇  2013-11-25 16:38:43 / 个人分类:loadrunner

把每天的收获都记录下来,免得上岁数的人记性差,忘的多。时常看一看,记在心里。

还是那句话:脚踏实地的走好每一步,想着未来的小小目标,努力前进,日积月累。

希望通过不屑的努力,能在未来的某一天,在我喜欢的领域上,取得不同于别人的成果,加油!

开始把.

 

一、通过web_set_user解决Windows登录验证问题。

The web_set_user function is a Service function that specifies a login string and password for a Web server or proxy server. It can be called more than once if several proxy servers require authentication. web_set_user overrides the run-time proxy authentication settings for user name and password.

When you log onto a server that requires user and password validation, VuGen records a web_set_user statement containing the login details. However, there are some more stringent, authentication methods for which VuGen is unable to insert web_set_user statements. See User Authentication for more detail. In such cases, you can add web_set_user into your script. manually.

When you run the script, LoadRunner automatically submits the user authorization along with every subsequent request to that server. At the end of the script, LoadRunner resets the authorization.

例子:web_set_user("mercury", "mercury", "mansfield:80");
如果要进行域验证的话,需要在用户名前面加上域名,如web_set_user("domain\\mercury", "mercury", "mansfield:80");

 

 

 

二、关于时间、日期格式的设置

Datetime Format Codes
%a day of week, using locale's abbreviated weekday names
%A day of week, using locale's full weekday names
%b month, using locale's abbreviated month names
%B month, using locale's full month names
%c date and time as %x %X
%d day of month (01-31)
%H hour (00-23)
%I hour (00-12)
%j number of day in year (001-366)
%m month number (01-12)
%M minute (00-59)
%p locale's equivalent of AM or PM, whichever is appropriate
%S seconds (00-59)
%U week number of year (01-52), Sunday is the first day of the week. Week number 01 is the first week with four or more January days in it.
%w day of week; Sunday is day 0
%W week number of year (01-52), Monday is the first day of the week. Week number 01 is the first week with four or more January days in it.
%x date, using locale's date format
%X time, using locale's time format
%y year within century (00-99)
%Y year, including century (for example, 1988)
%Z time zone abbreviation
%% to include the "%" character in your output string

例子:
lr_save_datetime("today is %Y-%B-%d", DATE_NOW , "today");
lr_output_message(lr_eval_string("{today}"));
英文操作系统中的执行结果:today is 2007-April-06
中文操作系统中的执行结果:today is 2007-四月-06

 

三、调用库函数,生成随机数

str=rand(); //生成任意随机数
str=rand() 0 //生成最大值为200的随机数
str=rand()0+200 //生成200-300之间的随机数
 
 
 
四、参数化事务名称

当脚本需要在多种场景下运行时,可以通过参数化事务名称来区分事务时间。下面的脚本将vusergroup作为事务名称:
int id, scid;
char* vuser_group;
char str[]="";
lr_whoami(&id, &vuser_group, &scid);
lr_message( "Group: %s, vuser id: %d, scenario id %d", vuser_group, id, scid);
strcat(str,vuser_group);
lr_start_transaction(str);
......
lr_end_transaction(str,LR_AUTO);
 
 

lr_whoami

  void lr_whoami (int *vuser_id, char **sgroup, int *scid);   返回值:返回当前运行的vuser ID、运行脚本的组名称以及场景ID   中文解释:lr_whoami函数获取关于vuser的信息   参数说明:   【int *vuser_id】需先注册,保存正在运行的vuser的ID,在虚拟用户生成器(Virtual User Generator)中回放返回-1   【char **sgroup】需先注册,保存正在运行的vuser的组名(脚本名),在虚拟用户生成器(Virtual User Generator)中回放返回None   【int *scid】需先注册,保存正在运行的Scenario的ID,在虚拟用户生成器(Virtual User Generator)中回放返回0   函数使用技巧:   1、函数的所有参数都为必须,其中vuser_id、scid两个整形的变量,如果不需要返回则直接用NULL替换即可   例:lr_whoami (&myid,&mygroup,NULL); //就用NULL把scid的变量替换掉了,也就不保存scid了   2、使用lr_whoami函数判断虚拟用户信息,增强脚本 Action()   {   int id,scid;//定义保存vuser信息的2个整形变量   char *group;//定义保存groupname   char *filename = "c:\\work\\log\\whoami.log";   long file_stream;   if ((file_stream = fopen(filename,"a+")) == NULL) //打开文件   {   lr_error_message("Cannot open %s", filename);   return -1;   }   lr_whoami (&id,&group,&scid);//获取变量   if (id > 10)   {   fprintf(file_stream,"vuser用户的信息:id=%d,group=%s,scid=%d\n",id,group,scid);   }   else   fprintf(file_stream,"id=%d,group=%s,scid=%d\n",id,group,scid);   fclose(file_stream);   return 0;   }   场景:运行15个用户跑这个脚本,log文件内容如下 id=3,group=lr_whoami,scid=0   id=1,group=lr_whoami,scid=0   id=2,group=lr_whoami,scid=0   id=4,group=lr_whoami,scid=0   id=6,group=lr_whoami,scid=0   id=5,group=lr_whoami,scid=0   id=7,group=lr_whoami,scid=0   id=8,group=lr_whoami,scid=0   id=9,group=lr_whoami,scid=0   id=10,group=lr_whoami,scid=0   vuser用户的信息:id=11,group=lr_whoami,scid=0   vuser用户的信息:id=13,group=lr_whoami,scid=0   vuser用户的信息:id=12,group=lr_whoami,scid=0   vuser用户的信息:id=14,group=lr_whoami,scid=0   vuser用户的信息:id=15,group=lr_whoami,scid=0
 
 
 
五、lr_continue_on_error

在脚本中的RTS中的“continue on error”用于脚本级别的设置,而通过本函数可以灵活的在脚本中控制错误处理,当出现错误时是否终止脚本运行,默认是出错时停止的。
其中的错误级别:
0:Terminate script. execution upon database access errors.(default)
1:Continue script. execution upon database access errors, but issue a warning.


脚本示例:
lr_continue_on_error(0);
lrd_stmt(Csr1, "select..."...);
lrd_exec(...);
lr_continue_on_error(1);
 
 
六、lr_get_attrib_string:获取命令行中的参数(检索脚本命令行中使用的字符串)

vuser_init() {

LPCSTR server;
LPCSTR user = "Tom";
LPCSTR password = "pwd";
LPCSTR connect[10];

server=lr_get_attrib_string("host");
if (server==NULL){
lr_error_message("Failed to login. Unknown host.\n");
return(0);
}


sprintf(connect,"%s, %s, %s", user, password, server);
lr_message("%s", connect);
return 0;
}

在脚本目录的out\output.txt中打印内容如下:
tomh, pwd, sun2 [MsgId: MMSG-17999]

运行的命令:
mdrv.exe -usr "D:\Program Files\MI\Mercury LoadRunner\scripts\test\TestScript\TestScript.usr" -out "D:\Program Files\MI\Mercury LoadRunner\scripts\test\TestScript\out" -host sun2 -loop 4 -time 1.5
 
LPCSTR  是以零结尾的字符串指针,相当于CHAR *。
 
 
七、常用函数
lr_end_sub_transaction
标记子事务的结束以便进行性能分析。
lr_end_transaction
标记事务的结束。
lr_end_transaction_instance
标记事务实例的结束以便进行性能分析。
lr_fail_trans_with_error

将打开事务的状态设置为 LR_FAIL 并发送错误消息。

lr_get_trans_instance_duration
获取事务实例的持续时间(由它的句柄指定)。
lr_get_trans_instance_wasted_time

获取事务实例浪费的时间(由它的句柄指定)。

lr_get_transaction_duration
获取事务的持续时间(按事务的名称)。
lr_get_transaction_think_time
获取事务的思考时间(按事务的名称)。
lr_get_transaction_wasted_time

获取事务浪费的时间(按事务的名称)。

lr_resume_transaction
继续收集事务数据以便进行性能分析。
lr_resume_transaction_instance
继续收集事务实例数据以便进行性能分析。
lr_set_transaction_instance_status

设置事务实例的状态。

lr_set_transaction_status

设置打开事务的状态。

lr_set_transaction_status_by_name

设置事务的状态。

lr_start_sub_transaction
标记子事务的开始。
lr_start_transaction
标记事务的开始。
lr_start_transaction_instance
启动嵌套事务(由它的父事务的句柄指定)。
lr_stop_transaction
停止事务数据的收集。
lr_stop_transaction_instance
停止事务(由它的句柄指定)数据的收集。
lr_wasted_time
消除所有打开事务浪费的时间。

命令行分析函数

lr_get_attrib_double
检索脚本命令行中使用的 double 类型变量。
lr_get_attrib_long
检索脚本命令行中使用的 long 类型变量。
lr_get_attrib_string
检索脚本命令行中使用的字符串。
信息性函数

lr_user_data_point
录制用户定义的数据示例。
lr_whoami
将有关 Vuser 的信息返回给 Vuser 脚本。不适用于应用程序管理。
lr_get_host_name
返回执行 Vuser 脚本的主机名。
lr_get_master_host_name
返回运行 LoadRunner Controller 或优化控制台的计算机的名称。不适用于应用程序管理。
字符串函数

lr_eval_string
用当前值替换参数。
lr_save_string
将以 NULL 结尾的字符串保存到参数中。
lr_save_var
将可变长度字符串保存到参数中。
lr_save_datetime
将当前日期和时间保存到参数中。
lr _advance_param
前进到下一个可用参数。
lr _decrypt
解密已编码的字符串。
lr_eval_string_ext
检索指向包含参数数据的缓冲区的指针。
lr_eval_string_ext_free
释放由lr_eval_string_ext分配的指针。
lr_save_searched_string

在缓冲区中搜索字符串实例,并相对于该字符串实例,将该缓冲区的一部分保存到参数中。

运行时函数

lr_load_dll
加载外部 DLL。
lr_peek_events
指示可以暂停 Vuser 脚本的位置。
lr_think_time
暂停脚本的执行,以模拟思考时间(实际用户在操作之间暂停以进行思考的时间)。
lr_continue_on_error
指定处理错误的方法。
lr_rendezvous
在 Vuser 脚本中设置集合点。不适用于应用程序管理。

 

lr_eval_string 将所有出现的参数替换为其当前值。
lr_save_string 将以 NULL 结尾的字符串保存到参数中。
lr_save_var 将可变长度字符串保存到参数中。

使用lr_save_string要将以 NULL 结尾的字符串保存到参数中,请使用 lr_save_string。要保存可变长度的字符串,请使用 lr_save_var 并指定要保存的字符串的长度。

 

 

 八、关于Rumtime Settings的默认设置

对于 LoadRunner,默认的运行时设置支持 VuGen 的调试环境和Controller 的负载测试环境。默认设置为:
思考时间:在 VuGen 中为“关闭”,而在 Controller 中为“按录制参数回放”。
日志:在 VuGen 中为“标准”,而在 Controller 中为“关闭”。
下载非 HTML 资源:在 VuGen 和 Controller 中均为“已启用”。

 

 

 

九、Runtime Settings中的日志设置

Enable Logging启用日志记录
该选项将在回放期间启用自动日志记录 - VuGen 将写入日志消息,您可以在执行日志中查看这些消息。该选项仅影响自动日志记录和通过 lr_log_message 出的日志消息。仍会发出使用 lr_message、lr_output_message 和lr_error_message 手动发送的消息。

如果在“常规运行时设置”文件夹中将“错误处理”设置为“出现错误时仍继续”,则错误消息仍将被发送到“输出”窗口。如果修改了脚本的“日志详细级别”, lr_message、lr_output_message 和lr_log_message 函数的行为将不会更改 — 这些函数将继续发送消息。

脚本示例:
Action()
{
lr_log_message("lr_log_message");
lr_message("lr_message");
lr_output_message("lr_output_message");
//lr_error_message("Unable to login to secure computing");
return 0;
}

不激活Enable Logging时的运行结果:
lr_message
Action.c(8): lr_output_message
Vuser Terminated.

如果激活Enable Logging并且设置send message only when error occur,则正常日志没有打印。
Just In Time Log mode.
Log messages will be sent only when an error occurs.
To change this behavior, look at the Log tab in Run Time Settings.

如果把lr_error_message语句前的注释去掉,则会将所有日志打印出来
Start auto log messages stack - Iteration 1.
Starting action Action.
lr_log_message
lr_message
Action.c(8): lr_output_message
Action.c(9): Error: Unable to login to secure computing
End auto log messages stack.

 

 

 

 

 

十、脚本中灵活设置日志打印级别

在脚本的Runtime Settings中的日志设置是针对整个脚本而言,可以通过lr_set_debug_message在脚本中进行灵活设置。
VuGen 日志事件(标准、参数替换等)的等级也称为消息类。有五个消息类:“简要”、“扩展”、“参数”、“结果数据”和“完全跟踪”。
可以使用 lr_set_debug_message 函数手动设置脚本内的消息类。如果您希望仅接收关于脚本的一小部分的调试信息,该设置非常有用。
例如,假设将日志运行时设置设为“标准日志”,并且需要获得脚本的特定部分的扩展日志。您可以使用 lr_set_debug_message函数在脚本中的所需位置设置扩展消息类。必须再次调用该函数以指定扩展模式的类型(“参数”、“结果数据”或“完全跟踪”)。通过调用 lr_set_debug_message 并指定“简要”模式,可返回到“标准日志”模式。

In this example, the lr_set_debug_message function enables the full trace option just before a call to lrd_fetch, which the user needs to debug because it has been giving unexpected results.

The second invocation of lr_set_debug_message resets the debug level to what it was formerly, by turning off (LR_SWITCH_OFF) the Extended message level.

Note that the two values for message level, LR_MSG_CLASS_EXTENDED_LOG and LR_MSG_CLASS_FULL_TRACE, have been logically ORed together.

lr_set_debug_message(LR_MSG_CLASS_EXTENDED_LOG | LR_MSG_CLASS_FULL_TRACE, LR_SWITCH_ON );
rc = lrd_fetch(Csr1, 1, 1, 0, PrintRow3);
if (rc>2000)
lr_debug_message(LR_MSG_CLASS_FULL_TRACE, "Fetch failed returned %d", rc);

lr_set_debug_message(LR_MSG_CLASS_EXTENDED_LOG | LR_MSG_CLASS_FULL_TRACE, LR_SWITCH_OFF );

 

 

十一、关于多线程的运行方式

Vuser 支持多线程环境。多线程环境的主要优势是每个负载生成器都能运行多个Vuser。只有线程安全协议才能按线程运行。(不适用于应用程序管理工具)

Controller 和优化控制台使用驱动程序(例如 mdrv.exe 或 r3vuser.exe)来运行Vuser。如果按进程运行每个 Vuser,则对于每个 Vuser 实例,都将反复启动(和加载)同一驱动程序并将其加载到内存中。将同一驱动程序加载到内存中会占用大量 RAM (随机访问内存)及其他系统资源。这就限制了可以在任一负载生成器上运行的 Vuser 数。或者,如果按线程运行每个 Vuser,Controller 为每 50 个 Vuser(默认情况下)仅启动驱动程序(如 mdrv.exe)的一个实例。该驱动进程 / 程序将启动几个Vuser,每个 Vuser 都按线程运行。这些线程 Vuser 将共享父驱动进程的内存段。这样做就不需要多次重新加载驱动程序 / 进程,节省了大量内存空间,从而可以在一个负载生成器上运行更多的 Vuser。

注意: 下列协议不是线程安全协议:Sybase-Ctlib、Sybase-Dblib、Informix、Tuxedo 和 PeopleSoft-Tuxedo。

 

 

十二、VUGen回放日志中各种不同颜色的文本,你知道代表什么意思吗

“回放日志”中使用了不同颜色的文本。
. 黑色:标准输出消息
. 红色:标准错误消息
. 绿色:用引号括起来的文字字符串(例如 URL)
. 蓝色:事务信息(开始、结束、状态和持续时间)
. 橙色:迭代的开始和结束。

如果双击以操作名开始的行,则光标将会跳到生成的脚本中的相应步骤上。

 

 

 

十三、从 UNIX 命令行运行 Vuser 脚本

使用 VuGen 开发基于 UNIX 的 Vuser 时,必须检查录制的脚本是否运行在UNIX 平台上。要验证脚本是否正确运行,请执行下列步骤:
1 从 VuGen 对录制的脚本进行测试,以检查该脚本是否在基于 Windows 的系统上正确运行。
2 将 Vuser 脚本文件复制到 UNIX 驱动器。
3 在 UNIX 计算机上通过使用 verify_generator 来检查 Vuser 的设置。
4 从 UNIX 命令行测试脚本,使用 run_db_vuser shell 脚本,在 Vuser 脚本目录中以独立模式运行要测试的脚本:run_db_vuser.sh script_name.usr

 


转载:http://blog.sina.com.cn/s/blog_68f2677f0100vbly.html


TAG:

 

评分:0

我来说两句

Open Toolbar