Saturday, November 2, 2013

JSON message processing in performance test


    We all know that JSON as a message format has become more popular,  databases such MongoDB, CouchDB support it, so do many web services.  So it's not a surprise that performance test tools need to deal with it.    However,  it's not easy to process JSON messages on many existing test platforms,  in some cases, users are desperate enough to use regular expressions!

    Here we are going to show how easy it is to process json messages in netgend javascript, even for user with modest coding background.  For those who have read previous blogs which touched on the subject of processing JSON messages,  this blog is a continuation and it will cover some more examples on how to use netgend javascript to process JSON messages in performance testing.

    Before diving into the examples,  the author would like to thank the sites where examples/questions were taken from. Readers of this blog are encouraged to read more at those sites and try to solve questions/challenges raised there.

Example 1

    This example is based on a stackoverflow question:   given a JSON string like the following, find the id's whose value is not null.
 [{"id":"123","value":"abc","description":"something"},
 {"id":"456","value":null,"description":"something else"},
 {"id":"789","value":"def","description":"something more"}]
    The solution is fairly simple.  In the following solution, we assume variable "str" contains the above JSON string.
 a = fromJson(str);  
 i = 0;  
 while (i < getSize(a)) {
      if(a[i].value != "") {  
           println(a[i].id);  
      }  
      i ++; 
 } 
    Note that function "fromJson" parses the JSON string and assign to variable "a",   "a" will contain an array of json objects, each containing "id",  "value" and "description" fields.  The variable "a[id].value" means the "value" field of id'th item of the array  "a".   Finally, as a reminder, all the variables here are local to the VUser.


Example 2

    Based on another stackoverflow question (there have lots of questions, don't they?):  given a JSON string like the following, print all id's of the rows whose type is "B".
[{"type":"A","id":"2093638401"}
{"type":"B","id":"20843301"}
{"type":"A","id":"20743305"}
{"type":"B","id":"20645303"}
{"type":"C","id":"20564501"}]
    The solution is very similar to example 1.
 #assume str has the content of above json string
 a = fromJson(str);   
 id = 0;  
 cnt = 0; 
 while (id < getSize(a)) {  
    if (a[id].type === "B") {   
      cnt ++;   
    }  
    id ++;   
 }   
   


Example 3



      Suppose we have the following JSON string as HTTP response,  we need to get value of certain fields.
 {  
   "response_time": 0.014376163482666016,  
   "applications": [  
     {  
       "api_key": "blted0e7982e1cf62a8",  
       "name": "gta",  
       "uid": "gta",  
       "account_name": "jack"  
     },  
     {  
       "api_key": "blt1423c40d23e4a423",  
       "name": "cellapp",  
       "uid": "cellapp",  
       "account_name": "max"  
     }  
   ]  
 }  
    The JSON structure is a little more complex, so are the compound variables like "resp.applications[0].api_key".  However, it should be very easy to map the structure of the variable to the fields in JSON string.
 resp = fromJson(http.replyBody);  
 //now you can access fields in the json string.  
 //In the following expressions, "application" is an array of two item, first item has index 0  
 resp.applications[0].api_key    
 //The above will have value "blted0e7982e1cf62a8"  
 resp.applications[1].name  
 //the above will have value "cellapp"  


Example 4

     Again based on an example in stockoverflow.  The JSON string in this example is long and complex.  Here we need to find the names of two nodes after "CURRENT", in our case, we need to find  "36735928", "37851136". 
 {  
 "payload": {  
       "userName": {  
             "firstName": "Peter",  
              "lastName": "Pan"  
              },  
       "reservationLists": {  
               "CURRENT": {  
                    "36735928": {  
                           "startDate": "2013-10-03",  
                            "destination": "Balearen",  
                            "mainProductType": "Mietwagen",  
                           "allProductTypes": [  
                                    "Mietwagen"  
                                     ]  
                           },  
                    "37851136": {  
                           "startDate": "2013-10-14",  
                           "destination": "Nevada, Las Vegas",  
                           "mainProductType": "Camper",  
                          "allProductTypes": [  
                                     "Extra",  
                                     "Extra"  
                                     ]  
                           }  
                      },  
                 "PAST": {  
                     "12061210": {  
                           "startDate": "2012-05-05",  
                           "destination": "Loire / Nivernais",  
                            "mainProductType": "Boot",  
                            "allProductTypes": [  
                                     "Boot",  
                                     "Extra"  
                                     ]  
                           }  
                     }     
                  }  
          }  
     }  
    Despite the long JSON string, the solution is quite short and easy to understand.
 //assume variable "jsonstr"contains the complex json string in the question  
 a = fromJson(jsonstr);  
 b = keys(a.payload.reservationLists.CURRENT);  
 //now b[0] will contain first node name ("36735928" in this case)   
 //and b[1] will contain second node name ("37851136" in this case)  


Example 5.

    Finally an example JSON from Alex Ersenie's blog,  we need to print all the child categoryId's.
 {  
   "code":"200",  
   "description":"Ok",  
   "categoryTree":{  
    "categories":[  
      {  
       "categoryId":3,  
       "parentId":1,  
       "name":"Root Catalog",  
       "isActive":1,  
       "includeInMenu":0,  
       "position":3,  
       "level":1,  
       "children":[  
         {  
          "categoryId":45,  
          "parentId":3,  
          "name":"Featured",  
          "isActive":1,  
          "includeInMenu":0,  
          "position":2,  
          "level":2,  
          "children":[ ]  
         },  
         {  
          "categoryId":46,  
          "parentId":3,  
          "name":"Might Like",  
          "isActive":1,  
          "includeInMenu":0,  
          "position":4,  
          "level":2,  
          "children":[ ]  
         },  
         {  
          "categoryId":47,  
          "parentId":3,  
          "name":"Recent Additions",  
          "isActive":1,  
          "includeInMenu":0,  
          "position":5,  
          "level":2,  
          "children":[       ]  
         },  
         {  
          "categoryId":48,  
          "parentId":3,  
          "name":"Hero Products",  
          "isActive":1,  
          "includeInMenu":0,  
          "position":6,  
          "level":2,  
          "children":[ ]  
         }  
       ]  
      }  
    ]  
   }  
 }   

     The solution, as usual, is fairly simple.
 a = fromJson(str);  
 b = a.categoryTree.categories[0].children;  
 for (id = 0; id < getSize(b); id ++) { 
      println(b[id].categoryId);  
 } 

    Now that we have shown processing complex JSON structure can be so simple,  I am sure you may want to apply this technique to more challenging issues encountered in your performance testing!



No comments:

Post a Comment