【備忘】GENERAL C PROGRAMMING EXAMPLES(2)Data Types

1. Program Format2. Data Types

 

In this section, we will discuss the C program format and how it relates to LoadRunner.

 

C Program Format

Every programming language has a set of rules, called syntax of the language, that govern how legal programs are constructed. A program in C can be made up of many components, sometimes spread over several files.

 

We will take a look at a very simple but popular C program:

 

#include<stdio.h>

 

main()

{

/* This program prints

   “Hello World!”       */

 

printf(“Hello World!”);

}

 

 

The first line, #include, is a signal that some information should be added to the program from an external file, either a system file as in this case (<stdio.h> is a standard input and output library), or a user defined file.

 

The body of the program is contained in main(), encapsulated by { }.

 

Comments in C are surrounded by /* and */. The C compiler ignores all text within the comments.

 

The only statement in the program, printf (“Hello World!”) instructs the program to print the “Hello World!” to the standard output. Now, the function printf is defined in stdio.h, and that is the reason we included stdio.h above the main.

User-Defined Functions in C

We are violating every rule in the art of C-instruction by introducing user-defined functions so early in the game. But it is very important to understand user-defined functions to introduce how LoadRunner scripts tie into C programming. At this point, we will introduce user-defined functions at a conceptual level, and get into the details in a later section.

 

In the Hello World program, we used the function printf, which prints the given message to the standard output. The printf function is a standard built-in function in C. So the compiler understands the action requested by the use of that function. But it is often necessary in programming to create your own functions to accomplish certain tasks. These functions are called user-defined functions.

 

For example, lets say you have to print the following text a number of times in your program:

 

$$$$$$$$$$$

Hello World!

$$$$$$$$$$$

 

This can be accomplished by creating a function that will print the above text and calling the function a number of times as required.

 

This is what your program will look like:

 

#include<stdio.h>

 

my_function()

{

/* Text generating code */

printf(“$$$$$$$$$$$/n”);

printf(“Hello World!/n”);

printf(“$$$$$$$$$$$/n/n”);

}

 

 

main()

{

my_function(); /* call 1 */

 

printf(“This program demonstrates user-defined functions./n/n”);

 

my_function(); /* call 2 */

}

 

In the above program, we defined a function named my_function, which was called twice in the main (call 1 and call 2). The following will be the output of the above program. You can see that every time the function my_function was called, the 3-line text output was generated.

 

$$$$$$$$$$$

Hello World!

$$$$$$$$$$$

 

This program demonstrates user-defined functions

 

$$$$$$$$$$$

Hello World!

$$$$$$$$$$$

 

By creating a function to generate the text output, we avoided the need to write the text-generating code over and over in the main program. We can simply call the function as many times as we need.

 

The above is a trivial example of a user-defined function with a single purpose. Multi-purpose and reusable functions can be created by use of parameters. This will be discussed in a later section.

 

Now that we have an idea of what a C function is, let us look at how LoadRunner scripts relate to C.

 


LoadRunner Scripts and C

 

In Vugen, you may have noticed that a LoadRunner script consists of init, actions and end sections. In LoadRunner 6.0 and above, you can have multiple actions sections as shown in figure 1.1.

 

 

 

Figure 1.1: Vugen

 

Lets look at the above script from a C programming point of view. When you record a script into different sections of Vugen, each section will be recorded like a regular C function. In the above figure, vuser_init, Action1, Action2 and vuser_end will be our 4 functions. If the above script is set to run 3 iterations, this is how an equivalent C program would look like:

 

main()

{

/* calling init section */

vuser_init();

 

/* calling actions section iteration 1 */

Action1();

Action2();

 

/* calling actions section iteration 2 */

Action1();

Action2();

 

/* calling actions section iteration 3 */

Action1();

Action2();

 

vuser_end();

}

 

where each function is defined in the Vugen script. There is no main in LoadRunner. It is automatically taken care of behind the scenes. Different sections of the scripts are the only visible parts of the program.

 

What goes on behind the scenes is a lot more ugly, but the above is a simplistic representation of the idea. The vuser will execute all actions corresponding to that section when a function is called. It is important to understand that each section in the script is treated like a user-defined function.

 

Example:

In the above script, lets say each section prints a message identifying itself. The following will be the output in the execution log of Vugen:

 

Virtual User Script started

vuser_init.c(3):You are in vuser_init

Running Vuser...

Starting iteration 1.

Action1.c(3):You are in Actions1

Action2.c(3):You are in Actions2

Ending iteration 1.

Starting iteration 2.

Action1.c(3):You are in Actions1

Action2.c(3):You are in Actions2

Ending iteration 2.

Starting iteration 3.

Action1.c(3):You are in Actions1

Action2.c(3):You are in Actions2

Ending iteration 3.

Ending Vuser...

vuser_end.c(3):You are in vuser_end

Vuser Terminated.

 

In a real script, each of these functions might be exercising the application under test (AUT), rather than just printing pretty little messages.

 

The idea behind this chapter was just to familiarize you with the LoadRunner program structure and compare it to C. We will focus on different elements of C in the upcoming sections.

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章