热爱测试,主要研究性能测试和自动化测试方面的技术,希望与同样对测试有热情的你一同进步成长

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

上一篇 / 下一篇  2007-12-12 20:58:53 / 个人分类:性能测试

51Testing软件测试网!wa4{reXr

   今天接着翻译http://www.wilsonmar.com/1lrscrīpt.htm上面关于LR脚本编写部分.51Testing软件测试网V m jEs&s!q

.u_4Q-f/?TQ%xy o0VUser_Init部分

-\nhg"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 C K5M

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

6DXen nNstH0
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;
'~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,V1fv DB
    if (strUsertype==NULL){
      lr_output_message("### Run-time Settings Additional Attribute usertype not specified. Cannot continue.");
      ;YF2d'BzJ.xZ I0lr_abort();
    }else{51Testing软件测试网a7f6W)q6C3C(ks Z3C.MA
      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; }
      yN&X gw%yeQ0^*@0else
      6u:^u M4V*`0if( strcmp( strUsertype,"basic") == 0 ){ thinktime1=8; }
      !N+eY A XJ0else{
      *w EBD%koE0
        lr_error_message("### ERROR: Value not recognized. Aborting run." );
        Vk:MS"S5yQ0lr_abort();
      }
      }51Testing软件测试网@I#iOP?V
      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 );
    控制信息的显示:
    在运行时,当脚本的事务失败后继续,你怎么知道哪个用户失败了?
    Idea在每个失败的事务之后,发出一个能够唯一确定该用户的信息。
    Loadrunner提供了一些函数来在运行时显示信息:
  • // 往输出日志上发送消息,这个消息前边会带有action 的名称和行数
    !j(c!{:J'vTP0lr_output_message("an output message");

    d-Zu3u\n051Testing软件测试网 Mz%B[(T-g6V

    n;KZ:^G0例子:

    r3\F BmRC0
    • Actions.c (4): an output message

    b,kEwOY;Z0

    (]8d`ih7^ b051Testing软件测试网tQ1cg0p6W J

    %o/Y/k:LqV&~Oc0
  • // 往输出日志和虚拟用户日志上发消息:51Testing软件测试网n:Q0u Qt*I
  • lr_message("*** a message"
      +"\r"+"A new line."
        );
  • Idea把");"放到另一行,这样可以容易的在命令上添加或者删除代码项。51Testing软件测试网m1Y:Rb XF Yx

    UNIX/Linux机器上,使用 "\n"来添加一个换行。51Testing软件测试网a6w3?^-RHD f%Q

    Windows机器上,使用"\r"来添加一个换行。51Testing软件测试网7ZYc`:b#`1g

    // 往输出日志上发送不带action名称和行数的信息51Testing软件测试网}9Z7jZA7fD l1T
    lr_log_message("number\t"+ numvar +"\t");

    ;v&g.|d/l%K0t;@n0

     51Testing软件测试网(EVHh KHVB

    // 只给控制器上的虚拟用户状态区域发送信息(当在VuGen中运行时,只是简单的显示):
    \)}0@k9f*F,YE8N0lr_vuser_status_message("a vuser status message");

    $wV%u1E \6e0

     

    S4YS7{NQ9P#K0

    // 给LoadRunner控制器或者Tuning模块的控制台输出窗口显示一个红色高亮度显示的-17999 信息。51Testing软件测试网IH*f9d%S p3D
    lr_error_message("an error message");
    51Testing软件测试网T&bO M!Q2g(eU\

     51Testing软件测试网%g;W0K0r?r;| Y3V4a

    Idea使用lr_error_message将会使日志信息堆栈在每个新的action开始时被自动清空。如果选择了"当错误发生时才发送消息", 这些信息仍然被创建在"日志信息堆栈"里, 但是被压缩了(没有显示),直到监测到一个错误。

    R;p&K;e1r}0

     51Testing软件测试网P]po/To

     

    U Ly!F4o0

      51Testing软件测试网sa7p xkd

       
       
       
       
       

      TAG: 性能测试

       

      评分:0

      我来说两句

      Open Toolbar