Tuesday, October 29, 2013

HTTP Performance testing made simple


    HTTP is the most popular protocol because it's the foundation of any web site.  Over the years,  it has gotten more complex because people tweak it all the time.   So has performance testing.   There are lots of options on many platforms when testing HTTP/Web server.  As we shall see,  performance testing on HTTP can actually be fairly simple, even when you are emulating 50,000 HTTP clients (VUsers).

    This is what a pair of basic HTTP transactions look like in javascript on Netgend platform.
 action(http,"http://www.example.com");  

 #HTTPS transaction very simple too, just change "http://" to "https://"
 action(http,"https://www.example.com)  
     Piece of cake!

    A little more advanced example is to emulate a user clicking through a sequence of pages.
 for (id = 1; id < 5; id ++) {  
   action(http,"http://www.example.com/docs/tutorial${id}.html"); 
   sleep(10000);
 } 
    Here the "sleep 10000" simulates a user spending 10 seconds (10000ms) reading the content.  You can make the time to be random, i.e. replace the "sleep 10000" with the following two lines to make it more realistic.
   x = randNumber(2000, 30000);  
   sleep(x);  


     HTTP POST is important because it's used in submitting a form, login, registrations and uploading..
This example shows how easy it is
 http.POSTData = "username=jsmith&pass=letmein";  
 action(http,"http://www.example.com/login?");  
    With the support for variables in netgend javascript, we can add some variable part to the POST data.
 name = randString(5,10);
 http.POSTData = "username=${name}&pass=letmein";  


     HTTP header is absolutely important,  with netgend javascript, you can add any HTTP header,  in particular, you can add

  • REFER header (which is useful in server security protection against CSRF) 
  • User-Agent header so that your client will emulate an IE, or Firefox or Chrome.

 httpHeader.REFER = "http://www.example.com/support";  
 httpHeader."User-Agent" = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";  
 name = "John";
 http.POSTData = "username=${name}&pass=letmein";  
 action(http,"http://www.example.com/login?");  
     Note that in the scripting, "User-Agent" header is enclosed with double quote, this is to prevent the "-" in the name from being mis-interpreted (as a minus sign).

     It's common to have a test reading data from a csv file and uses that data to "fill" a form and submit to server.  Here is an example:
function userInit() {
     var db = readCSVFile("data.csv");  //global variable
     var id = 0;                        //global variable
}
function VUSER () {
     r = db[id];  
     id ++;  
     info.name = r[0];  
     info.age  = r[1];  
     info.city  = r[2];  
     info.state  = r[3];  
     http.POSTData = toJson(info);  
     action(http,"http://www.example.com/register"); 
}  
    In this example, each VUser (client) will read a line/row from the csv file.  Each row of csv file may have multiple fields:   "name",  "age, "city", "state".
    For instance,  one row may look like  "John,23,Chicago,IL".   The "toJson" function will turn a dictionary variable ("info" in our case) into a json record like
{"name": "John", "age": "23",  "city": "Chicago", "state": "IL" }

     As a final example in this blog, we show how to generate xml message as HTTP Post data.
 function VUSER () {  
      name = "John";   
      age = 23;   
      city = "Chicago";   
      state = "IL";   
      http.POSTData = q|<user>   
      <name>${name}</name>   
      <age>${age}</age>   
      <city>${city}</city>   
      <state>${state}</state>   
 </user>|;   
      action(http,"http://www.example.com/register");  
 }  
    It looks very intuitive, doesn't it?   By the way, you can expand this example so that it will read data from the csv file just like the previous example.   Just don't forget to change the variables in the xml template string from "${name}"  to "${info.name}",   "${age}" to  "${info.age}" etc.

   There are many more interesting examples in performance testing on HTTP/web servers, we will cover them in other blogs, stay tuned.


No comments:

Post a Comment