Wednesday, February 12, 2014

Random (II)

    Randomness is important for performance testing because realistic users behave randomly to some degree, we need to have some randomness to better emulate the real users.  In one of earlier blogs, we covered some randomness related functions, here we show a more complete list.

 randNumber(<minNum>, <maxNum>, ["float"])  
    This function is the most commonly used function, it will give a random number between min and max. The third parameter is optional, if it's omitted, it will return an integer.   Here is a simple but interesting example:   randomly pick a product from a web page and view it
 numOfItems = //get number of items from a web page  
 id = randNum(1, numOfItem);  
 action(http, "http://www.example.com/viewProduct?id=${id}");  

 randString(<min>, <max>)  
     This function will return a random string whose length is between min and max. I haven't got a chance to use it real testing yet.

 rolldice(<percentage0>, <percentage1>,<percentage2>...);   
    This function is covered in detail in one of the earlier blogs.

 randElement(<arrayVariable>)  
    This function is what makes it so easy to pick an item from a set of web page.  As an example, suppose you want to emulate a virtual user who clicks a link in the navigation region of a web page, the following code snippet will suffice:
 action(http,"http://www.example.com/mainPage/");  
 links = fromHttp(http.replyBody, '//div[@class="navigation']//a/@href', text)  
 url = randElement(links);  
 action(http,url);  
    If you have used some other test platform(s), you may remember how hard it is to do something this simple -- you may have to write a long script ladened with API calls.

 randSequence(<min>, <max>)  
    This function will return a subset of numbers between min and max.  For example,  randSequence(1,10) returns a sub sequence of 1,2,3,...8,9,10.   Here is where it can be useful:  suppose a web page contains n items, you want to emulate user who is going to click a random sub sequence of 1,2,3, ...n.
 action(http, "http://www.example.com");  
 items = fromHtml(http.replyBody, '//a/@ref', "text");  
 sequence = randSequence(1, length(items));  
 for (i=0; i < length(sequence); i++) {  
    action(http, sequence[i]);  
 }  

 randSubset(<array>)  
    This function is the latest in this series, it's more general than the previous function (randSequence) in that the input may not be a sequence of consecutive numbers.  It treats the input array as a set and return a subset of it.   Here is the test scenario where it can be quite handy:   on a social media site,  user can look for interests news on a subset of people (the celebrities),  using this function, it was easy to emulate such a user behavior.

 action(http, "http://www.example.com");   
 celebrites = fromHtml(http.replyBody, '//span[@class="keypeople"]', "text");   
 subset = randSubset(celebrites);  
 str = join(subset, ",");  //concatenate the people with "," as the separator
 action(http, "http://www.example.com/track?people=${str}");   

    Are these all the random functions we will ever need?  Absolutely no, but given the flexibility on NetGend platform, I am sure adding support for the new ones will be easy.

No comments:

Post a Comment