Friday, March 28, 2014

Test a performance test tool using keyword search


    There are many performance test tools, when choosing the right one for your project,  it's important to know how scalable, flexible the test tool is.  It's relatively straightforward to find the scalability of a test tool  - how many virtual clients it can emulate on one load generator. So in this blog we will focus on testing the flexibility of a test tool.   It turns out there is a simple test scenario that can give you a quick idea how flexible a test tool is - emulating keyword search.


   Keyword search test scenario is to emulate internet user typing a searching phrase in his/her browser. As he/she is typing, a partial keyword is sent to the server and the server will return a list of results matching that partial keyword so the browser can give recommendations as the user is typing.  You may be surprised that this simple yet interesting test scenario are not easy on many test tools  - some test tools will ask tester to write code that is hard for them to understand or create.

  It's fairly simple on NetGend platform.  In the following script, we emulate users searching for the keyword "country music" with a simple loop:
 function VUSER() {  
     keyword = "country music";  
     len = length(keyword);  
     for (count = 1; count <= len; count ++) {  
         partial = substr(keyword, 0, count);  
         action(http,"http://localhost/?q=${partial}");  
         sleep(300); //sleep 300ms   
     }  
 }  
    This script basically sends a bunch of HTTP requests with parts of the keyword "country music".  The partial keywords were constructed using the function "substr(keyword, 0, count)". This function returns a substring of the input (keyword in our case) starting from position 0 (the first position) with count characters.

    In the above script, each emulated user will pause 300ms between typing each characters, what if you want to be a little more realistic by pausing a random amount of time?  That's easy, just replace the sleep(300);" in the above script with the following:
     x = randNumber(300, 2000);  
     sleep(x); //sleep random time between 300ms to 2000ms   

  What if you want to emulate a user typing a few keys and stopping after he/she sees the recommendation that he/she likes?  That's easy too, just change the line "len = length(keyword); " in the above script to the highlighted line in the following.
 function VUSER() {   
   keyword = "country music";   
   len = randNumber(1, length(keyword));   
   for (count = 1; count <= len; count ++) {   
     partial = substr(keyword, 0, count);   
     action(http,"http://localhost/?q=${partial}");   
     x = randNumber(300, 2000);  
     sleep(x); //sleep random time between 300ms to 2000ms    
   }   
 }  

   Finally, what if we need to test a set of keywords?  We can read the keywords from a file.  The following script shows we can read the keywords from a csv file in the userInit() function which executes before any virtual user runs.
 function userInit() {  
      var keywordRows = fromCSV("Phrases.txt");  
 }  
 function VUSER() {   
   keyword = getNext(keywordRows);  
   len = randNumber(1, length(keyword[0]));   
   for (count = 1; count <= len; count ++) {   
     partial = substr(keyword, 0, count);   
     action(http,"http://localhost/?q=${partial}");   
     x = randNumber(300, 2000);  
     sleep(x); //sleep random time between 300ms to 2000ms    
   }   
 }   
    The "var" in above script means that the variable "keywordRows" is visible to all the virtual users. The reason why we use keyword[0] is because getNext() returns a row of the csv file. A row can contain multiple keywords and keyword[0] means the first one.   We can emulate users trying all the keywords in a row,  but it's left as an exercise for the reader;-)

    Hope you get a good idea on how to test the flexiblility of a test platform using the keyword search test scenario.  At NetGend, we are proud that our platform is very flexible and  easy to use in addition to being hyper-scalable - it can emulate 1 million such virtual clients on on box.

No comments:

Post a Comment