Monday, March 12, 2012

How to Perform Mathematical Operations on Loadrunner Parameters

A simple way to perform calculations on parameters is as follows:

  • Convert the Loadrunner parameter to a C variable
  • Perform the calculation on the C variable
  • Push the C variable back to the parameter

The following shows how to do that in the case of addition and multiplication on a Loadrunner parameter.

Script

Action()
{
    // Assume you start with a parameter "{paramInt}"
    int i = 0;
    int result = 0;
    int num = 10; 
    lr_save_int(num, "paramInt"); 
    lr_log_message("Parameter value at start = %s", lr_eval_string( "{paramInt}" ));


    // Save the parameter to a C variable
    i = atoi( lr_eval_string( "{paramInt}" ));


    // Perform the calculation
    result = i + 10;
    
    // Save the calculation back to the parameter "{paramInt}"
    lr_save_int(result, "paramInt"); 
    lr_log_message("Parameter value after sample addition = %s", lr_eval_string( "{paramInt}" ));


    // Another example showing multiplication
    i = atoi( lr_eval_string( "{paramInt}" ));
    result = i * i;
    lr_save_int(result, "paramInt"); 
    lr_log_message("Parameter value after sample multiplication = %s", lr_eval_string( "{paramInt}" ));


    return 0;
}


Console Output

Running Vuser...
Starting iteration 1.
Starting action Action.
Parameter value at start = 10
Parameter value after sample addition = 20
Parameter value after sample multiplication = 400
Ending action Action.
Ending iteration 1.
Ending Vuser...

2 comments:

  1. This only works if you insert a prototyping function at the top of the action, before the action statement. E.g.,

    double atof( const char *string); // to use floats you must use a prototype function. ** This is critical do not move **
    // do not move this from this location.

    ReplyDelete
  2. see http://eyeontesting.com/questions/4661/atof-in-vugen-does-not-return-a-float.html

    ReplyDelete