Sunday, October 27, 2013

Using random functions for performance testing



    When doing performance testing, we may need to use random functions to emulate client behavior.  This blog use the ep scripting (can be seen as pesudo code here) to show some interesting applications of random functions.

    The simplest example is the function that returns the random number between min and max.
 randNumber(0,100);  
 
    Just with this simple function, we can do some fascinating client emulations. For example, suppose we want to emulate lots of sensor clients who will send temperature info periodically to a server.  Each client will start with a random temperature between 50 and 90 degrees and adjust randomly by 2 degrees, after reporting temperature, it will sleep for 10 seconds.
function VUSER() {
   temp = randNumber(50, 90);  
   while (1) {
       temp += randNumber(-2,2);  
       send(temp);
       sleep(10000);  
   }
}

    Along with a random number, we may want to generate a random string.  This can be used to generate a random user name of length between 4 and 10.
 userName = randString(4,10);  

    The above examples look more like simple exercises,  what about some real world user case?  For example, emulating different browsers?  The following script will emulate a Chrome browser with 30%   chance,  Firefox with 30% and IE with 40%.  We can do it by

  • get a random number between 1 and 100,  this will be the random percentage.
  • if the random percentage is between 71 and 100, then it's Chrome browser.
  • if the random percentage is between 41 and 70, then it's Firefox browser
  • if the random percentage is between 1 and 40, then it's IE browser.

function VUSER() {
    r = randNumber(1,100);  
    if (r > 70) { 
        ua = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36";  
    } else if (r > 40 ) {
        ua = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0"; 
    } else  {
        ua = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; 
    } 
    httpHeader."User-Agent" = ua; 
    action(http,"http://www.example.com");  
}



    This is cumbersome, especially if there are many different browsers to emulate with different percentages.   A easier way to do it would be to use the function "rolldice"
 rolldice(<percentage0>, <percentage1>,<percentage2>...);  
    What this does is, given a list of percentages (total sum is 100%), it will return 0 with <percentage0>,  return 1 with <percentage1>, return 2 with <percentage2> ...

    Now let's look at the browser emulation example implemented with rolldice() function:
 UAs = ["Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36",   
  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0", 
  "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"];    
 id = rolldice(30,30,40);  
 httpHeader."User-Agent" = UAs[id];  
 action(http,"http://www.example.com");  
   
 
    Isn't it much simpler?  Similarly we can emulate clients who "click" one of the hyperlinks with certain percentages after visiting main page and sleeping a random time between 2 seconds and 10 seconds.

 action(http,"http://www.example.com/"); 
 thinkTime = randNumber(2000, 10000);  
 sleep(thinkTime);  
 URLs = ["http://www.example.com/page1", 
    "http://www.example.com/page2", 
    "http://www.example.com/page3",   
    "http://www.example.com/page4"];  
 #now roll dice with the percentages  
 id = rolldice(20, 40, 30, 10);  
 action(http,URLs[id]);  

    I am sure there will a lot more examples that can be easily implemented using random function like rolldice().

No comments:

Post a Comment