loadrunner学习系列---脚本编写(2)
上一篇 / 下一篇 2007-12-12 20:58:53 / 个人分类:性能测试
51Testing软件测试网!w a4{reXr
今天接着翻译http://www.wilsonmar.com/1lrscrīpt.htm上面关于LR脚本编写部分.51Testing软件测试网VmjEs&s!q
.u_4Q-f/?TQ%xy o0VUser_Init部分
-\nh g"u _xZv051Testing软件测试网Gr(NW-Gxq#x#C这里是Vuser_init部分的一些例子:51Testing软件测试网%l0q%~/W#w)gh
51Testing软件测试网+[$@:NG9sF*F操作系统的User ID51Testing软件测试网$]4V?4g4E `l#MJS
51Testing软件测试网 Ok CK5M下面显示了使用advapi32.dll的GetUserNameA函数获得的操作系统的用户ID
6DX ennNstH0char 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;
'~je&S { i j0vuser_init()
d?p(X3p"h.g0{LPCSTR strUsertype; // Define *str.51Testing软件测试网4\5cY/_alfE
strUsertype =lr_get_attrib_string("usertype");51Testing软件测试网&H]UqG,V1fvDB
if (strUsertype==NULL){lr_output_message("### Run-time Settings Additional Attribute usertype not specified. Cannot continue.");
}else{51Testing软件测试网a7f6W)q6C3C(ks Z3C.MA
;YF2d'BzJ.xZ I0lr_abort();lr_message("### Run-time Settings Additional Attribute usertype=\"%s\"", strUsertype );51Testing软件测试网4jg'c.y3W(z
if( strcmp( strUsertype,"advanced") == 0 ){ thinktime1=2; }51Testing软件测试网9U:_B YI'a
else51Testing软件测试网k+zT#P0CK
if( strcmp( strUsertype,"intermediate") == 0 ){ thinktime1=4; }