好好学习 天天向上!

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

上一篇 / 下一篇  2008-09-25 17:19:08 / 个人分类:要学习的

VUser_Init部分

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

操作系统的User ID

下面显示了使用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;
$tUeW;r:xQj$S)u&[J183368vuser_init()51Testing软件测试网w)T ^{dZ;E
{
    LPCSTR strUsertype; // Define *str.
    YFeW$t,~183368strUsertype =lr_get_attrib_string("usertype");
    51Testing软件测试网 ~9P5P:L'r;dg*[ F
    if (strUsertype==NULL){
      lr_output_message("### Run-time Settings Additional Attribute usertype not specified. Cannot continue.");51Testing软件测试网A[$@ s ]s5W
      lr_abort();
    }else{51Testing软件测试网'h G VL:M Fk;s
      lr_message("### Run-time Settings Additional Attribute usertype=\"%s\"", strUsertype );
      ` WUzT@UK183368if( strcmp( strUsertype,"advanced") == 0 ){ thinktime1=2; }51Testing软件测试网dU;tc;l
      else51Testing软件测试网0x3|6vOu)G9l*h]
      if( strcmp( strUsertype,"intermediate") == 0 ){ thinktime1=4; }
      j5ENt4Dr&P:pKvt6W^P183368else
      51Testing软件测试网 [ C'c'R.a*j rnv
      if( strcmp( strUsertype,"basic") == 0 ){ thinktime1=8; }
      cG,m9M7Z183368else{51Testing软件测试网:dV0O0F8S
        lr_error_message("### ERROR: Value not recognized. Aborting run." );51Testing软件测试网w/d2] VYt
        lr_abort();
      }
      }51Testing软件测试网rF3[!fH!b3I,`(V;k {
      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 的名称和行数51Testing软件测试网`{5^ ^%WO v
    lr_output_message("an output message");

    例子:

    • Actions.c (4): an output message

  • // 往输出日志和虚拟用户日志上发消息:
    snUD)r^183368
  • lr_message("*** a message"
      +"\r"+"A new line."
        );
  • Idea把");"放到另一行,这样可以容易的在命令上添加或者删除代码项。

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

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

    // 往输出日志上发送不带action名称和行数的信息51Testing软件测试网q![Le(Kh)n+I{ n
    lr_log_message("number\t"+ numvar +"\t");

     

    // 只给控制器上的虚拟用户状态区域发送信息(当在VuGen中运行时,只是简单的显示):51Testing软件测试网7~J0t%Q@G
    lr_vuser_status_message("a vuser status message");

     

    // 给LoadRunner控制器或者Tuning模块的控制台输出窗口显示一个红色高亮度显示的-17999 信息。51Testing软件测试网bH:g2],A:m
    lr_error_message("an error message");

     


    TAG: 要学习的

     

    评分:0

    我来说两句

    日历

    « 2024-02-27  
        123
    45678910
    11121314151617
    18192021222324
    2526272829  

    数据统计

    • 访问量: 20435
    • 日志数: 43
    • 建立时间: 2008-05-07
    • 更新时间: 2009-03-19

    RSS订阅

    Open Toolbar