Loadrunner Function lr_save_var to convert C variable into loadrunner parameter

Loadrunner
lr_save_var:

This loadrunner function saves the defined bytes of a content from a one variable to the other.

Syntax:

lr_save_var(source parameter, value length(bytes), option, destination parameter)

C Program:

In our below C program we are going to see how the file content stored in a c variable is converted to a loadrunner parameter. (comments highlighted in RED matters)

//#include <stdio.h>
#define SEEK_SET 0 /* beginning of file. */
#define SEEK_CUR 1 /* current position. */
#define SEEK_END 2   /* end of file */


Action()
{
 long infile; // file pointer
 char *buffer; // buffer to read file contents into
 char *filename = "test.txt"; // file to read
 int fileLen; // file size
 int bytesRead; // bytes read from file
 //
 // open the file
 infile = fopen(filename, "rb");
 if (!infile) {
    lr_error_message("Unable to open file %s", filename);
    return;
 }


 // get the file length
 fseek(infile, 0, SEEK_END);
 fileLen=ftell(infile);
 fseek(infile, 0, SEEK_SET);
 lr_log_message("File length is: %9d bytes.", fileLen);


 // Allocate memory for buffer to read file
 buffer=(char *)malloc(fileLen+1);
 if (!buffer) {
    lr_error_message("Could not malloc %10d bytes", fileLen+1);
    fclose(infile);
    return;
 }


 // Read file contents into buffer & saves the file length in bytes to bytesRead 
 bytesRead = fread(buffer, 1, fileLen, infile);
 if (bytesRead != fileLen)
 {
    lr_error_message("File length is %10d bytes but only read %10d bytes", fileLen, bytesRead);
 }
 else
 {
    lr_log_message("Successfully read %9d bytes from file: ", bytesRead);
 }
 fclose(infile);


 // Save the C variable 'buffer' to a loadrunner parameter 'fileDataParameter'
 lr_save_var( buffer, bytesRead, 0, "fileDataParameter");
 free(buffer);
 lr_log_message("File contents: %s", lr_eval_string("{fileDataParameter}"));
}


In the above code

Source parameter -- File contents are stored in the c variable buffer.
Value length -- File length in bytes is saved in the c variable bytesRead.
Option -- By default set option to 0.
Destination Parameter -- File contents to be copied to the Loadrunner Parameter fileDataParameter.

While not done loop in Loadrunner


Loadrunner
Below is the scenario in which a request will be executed based on the while not done loop.

Code:

//While not done loop
while(!flag)
{

//Text check

web_reg_find("Search=Body", "SaveCount=PendingCounter", "Text=Pending", LAST);


//Request to be executed

web_url("IZ7_N2M81BG0K87C60AQE01L2O00M1=CZ6_N2M81BG0K87C60AQE01L2O0062=MEjavax.servlet.include.path_info!QCP_rlvid.jsp=_rvip!QCPBBDMyReportsView.jsp=_rap!ReportListBean.refresh=com.ibm.faces.portlet.mode!view==",
"URL=https://XXXXXX/wps/myportal/BBD/Home/bbdRptUserActivity/!ut/p/z1/{R6_1}/dz/d5/{L2_1}/p0/IZ7_N2M81BG0K87C60AQE01L2O00M1=CZ6_N2M81BG0K87C60AQE01L2O0062=MEjavax.servlet.include.path_info!QCP_rlvid.jsp=_rvip!QCPBBDMyReportsView.jsp=_rap!ReportListBean.refresh=com.ibm.faces.portlet.mode!view==/",
"Resource=0",
"RecContentType=text/html",
"Referer=https://XXXXXX/wps/myportal/BBD/Home/bbdRptUserActivity/!ut/p/z1/{R3_1}/",
"Snapshot=t121.inf",
"Mode=HTTP",
LAST);


//IF Block - condition

if(atoi(lr_eval_string("{PendingCounter}")) > 0)
{
flag = 0;

}
else
flag = 1;
}

Code Explained:

1. In the web_reg_find we are searching for the text 'Pending' and saving the text count in the variable 'PendingCounter'.

2. We are extracting the value from 'PendingCounter' using lr_eval_string and converting the string into integer using atoi function and passing the value as input to the IF Block.

3. If the text check is positive, IF Block will have value greater than 0 and the statement flag = 0 will be executed.
So the while condition becomes while(!0) == while(1) which is true and the request will be executed.

4.  If the text check is negative, IF Block will have value less than 0 and the statement flag = 1 will be executed.
So the while condition becomes while(!1) == while(0) which is false and the request will not be executed.