Monday, 28 September 2015

In-Script simulation of a load-balancer with LoadRunner

When testing enterprise grade web-based solutions there is often a company-wide LoadBalancer present that is a black-box from the testers point of view. Bypassing the LoadBalancer during testing is usually very important to be able to determine one servers limits or capacity.
There are however situations where the LB’s need to be simulated in a controlled way, and having the load share known and controlled during testing. To achieve this there is a simple trick that can be used in LoadRunner scripts.
The easiest way is to create a new action and then place this action in the correct place in the Run-Logic tree. Depending on the script it can be inserted as the last VUSER_INIT action or the 1st action in the VUSER_RUN section.
We now add a new parameter named VUserID to the parameter list, selecting the VUSER ID parameter type.
The code in the new action simply changes the target DOMAIN based on the VUserID parameter. Using the VuserID is good since we know it’s an integer number and always unique for the test.
Below is an example code where even and odd VuserID’s are used to determine the domain name:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Manual Load-Balancing
// This code enables execution of traffic to two separate web-servers, distributed 50/50
// by sending Even and Odd VUserID's to separate Web-Servers</code>
// Get the VUserID (Does not matter what the ID is
VUserID = atoi( lr_eval_string("{VUserID}") );
//if (VUserID & 0x0001==0) // use AND operator to filter out LSB bit
if(VUserID%2 == 0)
{
  lr_save_string( "http://www.server1.com", "BaseURL" ); // Even VUserID's
} else
{
  lr_save_string( "http://www.server2.com", "BaseURL" ); // Odd VUserID's
}
And here’s an example on how to actually use the BaseURL parameter:
1
2
3
4
5
6
7
8
9
10
11
lr_start_transaction("Homepage");
web_url("Home",
    "URL={BaseURL}/",   // BaseURL is determined by code above!
    "TargetFrame=",
    "Resource=0",
    "RecContentType=text/html",
    "Mode=HTML",
    LAST);
lr_end_transaction("Homepage", LR_AUTO);