Showing posts with label Report generation. Show all posts
Showing posts with label Report generation. Show all posts

Saturday, November 13, 2010

Report generation code in Load Runner 9.5

Recently was working on a LoadRunner script that needed to generate and download a report and the requirement was that it should not take more than 5 minutes to generate the report.

Manually testing the report generation, I noticed that on completion a "Report Generated" text wa displayed. Therefore, I wrote following LR Code to wait for report to generate and raise an error message if it takes more than 5 minutes to generate the report.

ReportGeneration()
{
int ReportGenerationTimeout = 300; // 5 minutes report gen timeout
long StartTime;
...
time(&StartTime); //save the time into StartTime
do
{ //execute this code while Report Generated Text is not found
lr_think_time (5);

web_reg_find("Text=Report Generated",
"Search=Body",
"SaveCount=ReportGeneratedCount",
LAST);

web_submit_data("GenerateReport",
"Action={URL}/xxx/xxx.xx",
"Method=POST",
"RecContentType=text/html",
"Referer={URL}/xxx/xxxcc.xx",
"Snapshot=t5.inf",
"Mode=HTML",
ITEMDATA,
"Name=ContinueButton", "Value=Continue", ENDITEM,
LAST);

if ( (time(NULL) - StartTime) > ReportGenerationTimeout) //check if the report generation time is more than 5 minutes
{
lr_error_message("Report took more than 5 minutes to generate. The user id is ", lr_eval_string("{UserName}"));
Logout(); //execute logout function
lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_AUTO); //exit the current iteration
and start next one
}

} while (atoi(lr_eval_string("{ReportGeneratedCount}")) == 0);
...
return 0;
}

Description:
- The ReportGeneration function defines two variables, ReportGenerationTimeout and StartTime.
-ReportGenerationTimeout is the report generation maximum time
-StartTime saves the time before the code that generates the report is executed.

- {URL} and {UserName} are LR parameters.

-The while loop checks for the text ="Report Generated" from the server response. It continues executing the while loop until it either gets the text or 5 minutes timeout limit is reached.

-If it finds the text then it exits the loop and continues executing rest of the code.
-If the report is not generated within 5 minutes (IF condition) then log an error message with username for whom it did not generate the report, execute Logout function and exit the current iteration.

-time(NULL) function return the current time. For more on time function refer to
http://www.cplusplus.com/reference/clibrary/ctime/time/

Note:
-if you use lr_abort() function rather then lr_exit(...), you will end the execution. This will execute Vuser_end function. Depending on how you have set up your scenario you may want to stop the execution of script(lr_abort - this will stop virtual users and your load level will drop, that is what you want) or continue to next iteration(lr_exit) when the report timeout is met.

-You could also use lr_log_message() instead of lr_error_message(). This will avoid overloading the network. However it will depend how often you are sending the message. For more information refer to LR help on these functions.