就目前的了解。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. 分支构造
- a. if
- b. switch
- 2. 循环构造
- a. for
- b. while
- 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());
}
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;
}
Actions()
{
system("del c://temp//temp.txt");
return 0;
}