测试回顾版-第三讲:Loadrunner脚本 VS C语言

本文介绍了LoadRunner的脚本语言与C语言的相似之处,强调了它们在变量和函数使用上的差异。在LoadRunner中,必须通过特定方式定义和调用参数。文章讨论了两者的共同点,如循环和选择语句,并提到了LoadRunner特有的lr_output_message函数。此外,还涵盖了逻辑运算、字符串处理、随机数生成以及文件处理等主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

就目前的了解。Loadrunner的脚本语言其实和C没什么区别。他内部的好多机制都是C实现的。

不过是一种“类C”。

(至于说什么java vuser等我不熟悉,所以我的理解就是类C,也只是让大家熟悉“类C”编程)

 

 所以我从几个方面分析

 

1:定义常量变量和C一样

2:LR函数的参数使用与C有点不一样, 在LR中,C的变量和LR的参数是不一样的。
任何C的变量都不能被LR的函数直接调用。
应该用lr_eval_string来取值。

3:什么循环语句,选择语句都和C一样

 

 

4:一些函数的定义和C不一样。虽然名字一样,参数有不同

5: 输入输出也有些不同。

 

所以重点要突破的地方就是理清参数和变量直接的关系。和多熟悉一LR些函数,其他就是C语言的知识了。

 

 

lr它有自己管理的变量,也叫参数,这种参数只能通过reg或者lr_save_方式定义,或者通过文件定义. 

 

1:定义常量和变量

 

 所有变量在C必须声明,才可以使用

 

浮点型

字符型

 

 

%d是用来表示一个整型值

%f浮点型。(不常见)。

同样, %ç对应字符值。

 

在LoadRunner中lr_output_message相当于C中的printf的。 输出将被印在执行日志中Vugen窗口。

int a = 5;

float x = 3.14;

char c = ‘A’;

 

lr_output_message(“The values are: a = %d, x = %f, c = %c”,a,x,c);

 

  1. 1.      分支构造
    1. a.      if
    2. b.      switch
  2. 2.      循环构造
    1. a.      for
    2. b.      while
    3. c.       do 


逻辑运算

Symbol

Meaning

LESS THAN

<=

LESS THAN OR EQUAL TO

> 

GREATER THAN

>=

GREATER THAN OR EQUAL TO

==

EQUAL TO

&&

AND

||

OR

!

NOT

 

  ·     break -退出形式循环或开关。 This applies only to for/while loops.这仅适用于为/ while循环。 To break out of a LoadRunner iteration, use exit(-1);打破了LoadRunner的迭代,使用exit(-1);

        continue -- skip 1 iteration of loop.继续-跳到1迭代循环。 This applies only to for/while.这仅适用于为/时。 To continue to the next iteration in LoadRunner iterations, use return 0.要继续到下一个迭代的LoadRunner的重复,请使用返回0 。

 

// Global variable count

int count=0;

 

// Global variable count

int count=0;

 

Actions()

{

        // Increment count

        //

        count++;

 

        // If count is equal to 10, exit the script

        //

        if(count == 10)

        exit(-1);

 

        // If count is even, skip the current iteration

        // and continue with the next iteration

        //     

        if(count%2 == 0)

        return 0;

 

        lr_output_message(">>>>>>>>>>>>>>>>>>>>>> %d",count);

 

        return 0;

 

 

字符串处理

 

strcpy is used to copy one string into another.

strcat is used to add one string to the end of another.

strcmp compares two strings and returns 0 if both strings are identical.

atoi converts an ASCII string to an integer.

itoa converts an integer to an ascii string for a given base. Syntax:

 

随机数

Actions()

{

        int i;

 

        // srand(time(NULL));

 

        for(i=0;i<10;i++)

        lr_output_message(">>>>>>>>>> Random Number: %d",rand());

 

}

 

 

 

LoadRunner运行原理浅析

 

Actions()

{

        char MyString1[20] = "";

        char MyString2[20] = "";

        char MyString3[20] = "Mercury2";

        char Cstring[10] = "12345";

        int Cint;

 

 

        // MyString1 is empty

        //

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

        // copy "Mercury1" into MyString1

        //

        strcpy(MyString1,"Mercury1");

 

        // Now MyString1 contains "Mercury1"

        //

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

 

        // Copy MyString3 into MyString2

        //

        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);

        strcpy(MyString2,MyString3);

        lr_output_message(">>>>>>>>>> MyString2 = %s",MyString2);

 

 

        // Catenate MyString2 to MyString1

        //

        strcat(MyString1,MyString2);

        lr_output_message(">>>>>>>>>> MyString1 = %s",MyString1);

 

        // Cstring is converted to integer Cint

        //

        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);

        Cint = atoi(Cstring);

        lr_output_message(">>>>>>>>>> Cint = %d",Cint);

 

        // Cint is converted to string

        Cint = 100;

        itoa(Cint,Cstring,10);

        lr_output_message(">>>>>>>>>> Cstring = %s",Cstring);

 

        return 0;

}

 

 

多维数组

文件处理

 

LoadRunner does not support the FILE data type. So we can declare the file as an integer type.

 

               int MyFile;

 

               int MyFile;

MyFile =(int) fopen(“C://temp//loans.txt”,”w”); // In LoadRunner

 

               int MyFile;

               int LoanNumber;

               MyFile = fopen(“C://temp//loans.txt”,”r”);

               fscanf(MyFile,”%d”, &LoanNumber);

 

int MyFile;

               char Name[] = “John Doe”;

               MyFile = fopen(“C://temp//loans.txt”,”w”); // note that “w” is used to write

               fscanf(MyFile,”%s”, Name); // note that we are printing a string here

 

 

 

 

 

函数

 

引用文件

 

#include "as_web.h". as_web.h i

Miscellaneous Functions

Actions()

{

        system("del c://temp//temp.txt");

        return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值