loadrunner字符串处理函数

在Loadrunner中提供了C对字符串出了的相关功能函数,如下:

lr_save_datetime

把当前日期和时间保存到一个参数中

lr_save_int

把一个整数保存为参数

lr_save_searched_string

保存一个字符数组相对于字符串出现的部分。

lr_save_string

把一个字符串保存到参数中

lr_save_var

把字符串的一部分内容保存为参数

lr_eval_string

返回参数的实际内容或返回一个包含参数的字符串的实际内容

     下面我们将对这些函数作一一说明,并举例实践。

void lr_save_datetime(const char *format, int offset, const char *name);

参数解释: 

format

所检索的日期/时间信息的格式

offset

当前日期和时间的偏移量, 采用形式有: DATE_NOW, TIME_NOW, ONE_DAY, ONE_HOUR, ONE_MIN. 例如: TIME_NOW + ONE_HOUR

name

保存的参数名称,用来存储日期/时间信息。

例子:

 

Action()
{
    lr_save_datetime("Tomorrow is %B %d %Y", DATE_NOW + ONE_DAY, "next");

    lr_output_message(lr_eval_string("{next}"));

    return 0;
}

 

运行结果为:

Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(5): Tomorrow is 七月 06 2011
Ending action Action.
Ending iteration 1.
Ending Vuser...

int lr_save_int ( int value, const char *param_name);

参数解释: 

value

将要保存到参数得整数值

param_name

保存的参数名称

例子:

 

Action()
{
    int num;
    num = 5;
    lr_save_int(num * 2, "param1");
    lr_output_message(lr_eval_string("{param1}"));
    return 0;
}

 

运行结果为:

Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(6): 10
Ending action Action.
Ending iteration 1.
Ending Vuser...

int lr_save_searched_string (const char *buffer, long buf_size, unsigned int occurrence, const char *search_string, intoffset, unsigned int string_len, const char *parm_name );

参数解释:

buffer

The STRING or CARRAY buffer, part of whose contents you want to save.

buf_size

The buffer size.

occurrence

The occurrence number of the search_string (0-based count). For example, if the search_string occurs three times, and you want the second occurrence, set occurrence to 1.

search_string

The string to search for in the buffer.

offset

The number of characters to skip after the end of the search string occurrence.

string_len

The number of characters to save.

parm_name

Parameter name to be used in subsequent lr statements to refer to the saved information. Name is enclosed in double-quotes.

例子:

 

Action()
{
    char cBuff[] = "abc Emma Woodhouse abc Elizabeth Bennet abc William Price";
    lr_save_searched_string(cBuff, strlen(cBuff),
                            2, "abc", // Search for third occurrence of "abc"
                            1,        // Skip the space after "abc"
                            4,           // Put the next four characters...
                            "Fannys_brother"); // ... in parameter Fannys_brother.
    lr_output_message("%s",lr_eval_string("Fannys_brother={Fannys_brother}"));

    return 0;
}

 

运行结果为:

Running Vuser...
Starting iteration 1.
Starting action Action.
Action.c(9): Fannys_brother=Will
Ending action Action.
Ending iteration 1.
Ending Vuser...

int lr_save_string (const char *param_value, const char *param_name);

int lr_save_var (const char *param_value, unsigned long const value_len, unsigned long const options, const char *param_name);

char *lr_eval_string (const char *instring );

     以上三个函数我们经常会用在一起。例如:

例子:

 

Action()

{
    lr_save_string("testaaaab", "InName");
    lr_output_message("%d",strlen("{InName}"));
    lr_output_message("%d",strlen("{InName}")-1);
    lr_save_var( lr_eval_string("{InName}"),
                 strlen("{InName}")-1, 0, "ShortName");
    lr_output_message("%s",lr_eval_string("ShortName={ShortName}"));
    lr_output_message("ShortName=%s",lr_eval_string("{ShortName}"));

    return 0;

}

 

Starting iteration 1.
Starting action Action.
Action.c(5): 8
Action.c(6): 7
Action.c(9): ShortName=testaaa
Action.c(10): ShortName=testaaa
Ending action Action.
Ending iteration 1.

      *****注:在loadrunner中,字符串处理函数用的比较多,其用法也比较简单。多加以练习操作,在项目实践中可以很快的做到得心应手。*****

专注于自动化、性能研究,博客为原创,转载请注明文章来源于:http://www.cnblogs.com/Automation_software/ 只求在IT界有一个清闲的世界让我静心的去专研,不求功名利禄,只为心中的那份成就感及自我成长、自我实现的快感。

 

posted @ 2016-02-19 11:05  老夫子_22  阅读(210)  评论(0)    收藏  举报