Thursday, December 15, 2011

Loadrunner Technique for Handling Early Script Exits

The following shows a technique for handling the following type of scenario within a single script:
  • Percent users complete step 1: 100
  • Percent users complete step 2: 60
  • Percent users complete step 3: 10
  • etc.
In this case, all users begin the script, some of them continue on to the next part, some of them continue on to the third step, etc.  It is convenient to have this scenario handled within a single script.  Load can then be varied merely by varying the vuser count.
The user behavior controlling when they exit is handled by creating a parameter table with columns as follows
  • stepTwoPercentComplete (set to 60, e.g.)
  • stepThreePercentComplete (set to 10, e.g.)
  • etc.
The script will read the parameterized percentages, then generate a random number each iteration to determine how many steps to complete before exiting.

This technique allows a fairly complex scenario to be handled by a single simple script.

Sample Script
#include "as_web.h"
#include "lrw_custom_body.h"


char url[] = "URL=http://server001";


Action()
{   
int stepOnePercentComplete = 100; // 100% do step 1
int stepTwoPercentComplete = atoi(lr_eval_string("{stepTwoPercentComplete}")); // get param, e.g., 60% complete step 2
int stepThreePercentComplete = atoi(lr_eval_string("{stepThreePercentComplete}")); // get param, e.g., 10% complete step 3
   
  
    // Get a random percent to use in deciding whether and when to exit early in the script
    int randomPercent = (rand() % 100)+1;  // Get a random percent


lr_log_message( "--- Random Percent = %d", randomPercent );
    lr_log_message( "--- Percent users completing step 1 = %d", stepOnePercentComplete );
    lr_log_message( "--- Percent users completing step 2 = %d", stepTwoPercentComplete );
    lr_log_message( "--- Percent users completing step 3 = %d", stepThreePercentComplete );

   /****************************** STEP 1 *************************************/
   web_reg_save_param( "transactionResponse",
                       "LB=<title ID=titletext>",
                       "RB=</title>",
                       "Search=Body",
                       "NOTFOUND=Warning",
                        LAST );


   lr_start_transaction("step1");


   web_custom_request( "step 1",
                       url,
                       "Method=GET",
                       "Body=",
                       LAST );


   if( 0 != strstr( lr_eval_string( "{transactionResponse}" ), "Error" ) )  {
  lr_end_transaction( "step1", LR_FAIL );
  lr_error_message( "--- ERROR (exception found in response): %s", lr_eval_string( "{transactionResponse}" ) );
  lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_FAIL);
  }
   else {
  lr_end_transaction("step1", LR_PASS);
  lr_log_message( "--- Verify Response = %s", lr_eval_string( "{transactionResponse}" ));
   }
   /****************************** END STEP 1 *************************************/



   /****************************** STEP 2 *************************************/
    // DECIDE WHETHER USER EXITS BEFORE STEP 2
   if (randomPercent > stepTwoPercentComplete) {
  lr_log_message( "--- Exiting after step 1 because randomPercent > stepTwoPercentComplete (%d > %d)", randomPercent, stepTwoPercentComplete);
       lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_PASS);;
   }
   lr_log_message( "--- Continuing to step 2 because stepTwoPercentComplete >= randomPercent (%d >= %d)", stepTwoPercentComplete, randomPercent);


   web_reg_save_param( "transactionResponse",
                       "LB=<title ID=titletext>",
                       "RB=</title>",
                       "Search=Body",
                       "NOTFOUND=Warning",
                        LAST );


   lr_start_transaction("step2");


   web_custom_request( "step 2",
                       url,
                       "Method=GET",
                       "Body=",
                       LAST );


   if( 0 != strstr( lr_eval_string( "{transactionResponse}" ), "Error" ) )  {
  lr_end_transaction( "step2", LR_FAIL );
  lr_error_message( "--- ERROR (exception found in response): %s", lr_eval_string( "{transactionResponse}" ) );
  lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_FAIL);
  }
   else {
  lr_end_transaction("step2", LR_PASS);
  lr_log_message( "--- Verify Response = %s", lr_eval_string( "{transactionResponse}" ));
   }
   /****************************** END STEP 2 *************************************/




   /****************************** STEP 3 *************************************/


   // DECIDE WHETHER USER EXITS BEFORE STEP 3
   if (randomPercent > stepThreePercentComplete) {
  lr_log_message( "--- Exiting after step 2 because randomPercent > stepThreePercentComplete (%d > %d)", randomPercent, stepThreePercentComplete);
  lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_PASS);;
   }
   lr_log_message( "--- Continuing to step 3 because stepThreePercentComplete >= randomPercent (%d >= %d)", stepThreePercentComplete, randomPercent);


   web_reg_save_param( "transactionResponse",
                       "LB=<title ID=titletext>",
                       "RB=</title>",
                       "Search=Body",
                       "NOTFOUND=Warning",
                        LAST );


   lr_start_transaction("step3");


   web_custom_request( "step 3",
                       url,
                       "Method=GET",
                       "Body=",
                       LAST );


   if( 0 != strstr( lr_eval_string( "{transactionResponse}" ), "Error" ) )  {
  lr_end_transaction( "step3", LR_FAIL );
  lr_error_message( "--- ERROR (exception found in response): %s", lr_eval_string( "{transactionResponse}" ) );
  lr_exit(LR_EXIT_ITERATION_AND_CONTINUE,LR_FAIL);
  }
   else {
  lr_end_transaction("step3", LR_PASS);
  lr_log_message( "--- Verify Response = %s", lr_eval_string( "{transactionResponse}" ));
   }
   /****************************** END STEP 3 *************************************/
}



No comments:

Post a Comment