Monday, January 13, 2014

Test a performance test tool by JSON

    In earlier blogs, we have covered various cases on handling JSON messages and we saw how easy it was to extract any field(s) from a JSON message on the NetGend platform.  Saw this stackoverflow question, it's quite interesting because it's more than just value-extraction.

   The task is to analyze server responses (which are in the format of JSON) and find out how many unique types that each message contains.  The following is a sample server response. We can see there are 3 unique types: 8, 9, 14.


   A complex solution for JMeter was suggested, it involved using multiple regular expressions, loop controller, complicated manipulations of the variables.  It could be well above the level of a typical user.  It's quite easy on NetGend platform:

 function VUSER() {   
      action(http, "");  
      a = fromJson(http.replyBody); //reply may be different for different users 
      types = {};  
      count = 0;  
      for (i=0; i<length(a); i++) {  
           type = a[i].type;  
           if (types.$type != 1) {  
                types.$type = 1;  
                count ++;  
    The script is pretty simple, here is a summary of what the script does:

  • parse the http reponse "http.replyBody" using "fromJson" function,
  • Since we know this JSON message is an array, we use the "for" loop to go through the elements,  each element has the fields "type" and "id".    We assign the type of the element ("a[i].type" in our case) to the variable "type",
  • For each element, we use "if (types.$type != 1)" to check if we have seen the "type" before (e.g. if we have seen the value contained in "$type"), if not, we set "types.$type = 1" to mark this "type" as seen (hence we don't count it when we see this type again) and increment the count.
    The only part that needs a little explanation is "types.$type",  it's slightly different from an expression like "types.XYZ", which means the value of  the field "XYZ" of the compound variable "types".   "types.$type" refers to the field whose fieldName is the value of variable "type" ( assigned from "a[i].type").  In another word, if  "$type" has value "ABC",  "types.$type" means "types.ABC".

   As we been emphasizing, using the regular expression to process JSON message is not a good solution, it's hard, cumbersome and error-prone.  With the growing popularity of JSON message in web applications,  it's high time for other test platform to implement a good support for it.   If there is a set of questions to test the flexibility of a performance test platform,  this one should be one of them.

No comments:

Post a Comment