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


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

     

       今天接着翻译http://www.wilsonmar.com/1lrscrīpt.htm上面关于LR脚本编写部分.

    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;
    vuser_init()
    {
        LPCSTR strUsertype; // Define *str.

        strUsertype =
    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 );
    控制信息的显示:
    在运行时,当脚本的事务失败后继续,你怎么知道哪个用户失败了?
    Idea
    在每个失败的事务之后,发出一个能够唯一确定该用户的信息。
    Loadrunner提供了一些函数来在运行时显示信息:
    • // 往输出日志上发送消息,这个消息前边会带有action 的名称和行数
      lr_output_message("an output message");

      例子:

    • Actions.c (4): an output message
    • // 往输出日志和虚拟用户日志上发消息:
    • lr_message("*** a message"
        +" "+"A new line."
          );

    Idea把");"放到另一行,这样可以容易的在命令上添加或者删除代码项。

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

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

    // 往输出日志上发送不带action名称和行数的信息
    lr_log_message("number "+ numvar +" ");

     

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

     

    // 给LoadRunner控制器或者Tuning模块的控制台输出窗口显示一个红色高亮度显示的-17999 信息。
    lr_error_message("an error message");

     

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

     

     

     

     转自:http://www.51testing.com/html/66/34866-70093.html
  • 相关阅读:
    Linux笔记(九)
    Linux笔记(八)
    Linux笔记(七)
    Linux笔记(五)
    Linux笔记(六)
    Linux笔记(四)
    Linux笔记(三)
    hdu 1009 qsort运用
    dfs模板 二部图的最大匹配
    拉格朗日函数c++
  • 原文地址:https://www.cnblogs.com/abcd19880817/p/7201551.html
Copyright © 2020-2023  润新知