Tuesday, October 23, 2012

Loadrunner Asynchronous Results Polling

Some user action trigger asynchronous server-side operations.  The user can then be forced to check for results periodically by polling the server.   The following script gives an example of how to do this as follows:

  • Define variables to specify polling interval and polling timeout.
  • Each polling interval, make the call to the server and check the results.
  • If the server has returned the finished results, end the polling loop and pass the transaction.
  • Otherwise, wait for the next polling interval and repeat.
  • If the timeout is hit, end the polling loop and fail the transaction.

Script

Action()
{
int done = 0;  
int pollIntervalSec = 1;
int timeoutSec = 3;
int secWaited = 0;
char *response, *expectedResponse; 

expectedResponse = "abc";

        lr_start_transaction("wait_for_data");

        // poll the site until expected response is received or timeout is reached
while (done==0)
{
            // capture response information
    web_reg_save_param("responseParam", 
"LB=Control: ", 
"RB=max", 
LAST ); 

            // hit the page to be polled
    web_url("Test", 
"URL=http://www.google.com", 
"Resource=0", 
"RecContentType=text/html", 
"Snapshot=t1.inf",
LAST);

    // Save respone to string
   response = lr_eval_string(lr_eval_string("{responseParam}"));

   // check string contents to see if done
   if(strcmp(response,expectedResponse)==0)
{
     done=1; // set flag to indicate success
 lr_message("Found expected response=%s", response);
     lr_end_transaction("wait_for_data", LR_PASS);
   }

    // increment time waited
   secWaited = secWaited + pollIntervalSec;

    // check for timeout
   if (secWaited > timeoutSec)
   {
    done = 1;
    lr_end_transaction("wait_for_data", LR_FAIL);
    lr_error_message("Wait for data timed out, took longer than %d seconds", timeoutSec);
   }
   lr_think_time(pollIntervalSec);


return 0;
}


Console Output


Starting action Action.
Action.c(11): Notify: Transaction "wait_for_data" started.
Action.c(15): Registering web_reg_save_param was successful   [MsgId: MMSG-26390]
Action.c(20): Found resource "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" in HTML "http://www.google.com"   [MsgId: MMSG-26659]
Action.c(20): Warning -26376: Requested parameter "responseParam" found in 2 different pages/frames. Use web_reg_save_param with more restrictive LB/RB/RelFrameId   [MsgId: MWAR-26376]
Action.c(20): Warning -26373: Parameter "responseParam" saved from a resource (URL="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png")   [MsgId: MWAR-26373]
Action.c(20): web_url("Test") highest severity level was "warning", 12282 body bytes, 1071 header bytes   [MsgId: MMSG-26388]
Action.c(48): lr_think_time: 1.00 seconds.
Action.c(15): Registering web_reg_save_param was successful   [MsgId: MMSG-26390]
Action.c(20): Resource "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" is in the cache already and will not be downloaded again   [MsgId: MMSG-26655]
Action.c(20): web_url("Test") was successful, 4952 body bytes, 400 header bytes   [MsgId: MMSG-26386]
Action.c(48): lr_think_time: 1.00 seconds.
Action.c(15): Registering web_reg_save_param was successful   [MsgId: MMSG-26390]
Action.c(20): Resource "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" is in the cache already and will not be downloaded again   [MsgId: MMSG-26655]
Action.c(20): web_url("Test") was successful, 4952 body bytes, 224 header bytes   [MsgId: MMSG-26386]
Action.c(48): lr_think_time: 1.00 seconds.
Action.c(15): Registering web_reg_save_param was successful   [MsgId: MMSG-26390]
Action.c(20): Resource "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" is in the cache already and will not be downloaded again   [MsgId: MMSG-26655]
Action.c(20): web_url("Test") was successful, 4952 body bytes, 224 header bytes   [MsgId: MMSG-26386]
Action.c(45): Notify: Transaction "wait_for_data" ended with "Fail" status (Duration: 4.1903 Think Time: 3.0017 Wasted Time: 0.6545).
Action.c(46): Error: Wait for data timed out, took longer than 3 seconds
Action.c(48): lr_think_time: 1.00 seconds.
Ending action Action.

No comments:

Post a Comment