Monday, January 20, 2014

Performance testing on JSON RPC based server


    Just like the fact that JSON is overtaking XML as a more dominate data format for communication between clients and servers, JSON-RPC will eventually top the RPC mechanisms because of its flexibility and light weight.  For a performance test professional,  the natural question is, how do we run performance test on a JSON-RPC based server?

    NetGend, thanks to its great support for the processing on JSON messages,  is a good choice for the job.   In a typical script, you start by using the method createJSONRPCObject to create a JSON RPC object.  After that,  you can use method jsonrpc2string to serialize the JSON object into a string to be sent to the server.  Here is the syntax:
 jsonrpc2string(<rpcObject>, <method>, <parameters>);  

    Let's look at a concrete example, taken from an article in simple-is-better.
 function VUSER() {  
      a = createJSONRPCObject();  
      http.POSTData = jsonrpc2string(a, "echo", ["Hello World"]);  
      action(http,"http://www.example.com/rpcService");  
 }  
    Here the method is "echo" and it has one parameter("Hello World" in this case). If there are more  parameters, you can add them to the array like:
    http.POSTData = jsonrpc2string(a, "echo", ["Hello World", "please discard"]);

     According to the same article,  JSON RPC excels in the support for the named parameter(s), let's look at such an example (also taken from the above mentioned article).

 function VUSER() {  
      a = createJSONRPCObject();  
      http.POSTData = jsonrpc2string(a, "search", {"last_name": "Python"});  
      action(http,"http://www.example.com/rpcService");   
 }  
    In this example, the parameter field is {"last_name": "Python"},  It's not an array, but a dictionary/hash table where each field has a name (hence "named parameters").  It's very flexible in that it can easily support optional parameters and nested structures.

    We have talked about parsing and processing JSON message in earlier blogs, let's just look at a quick example:  suppose the server response is like the following and we need to print the number of results in the response.
 {"jsonrpc": "2.0", "result": [  
      {"first_name": "Brian", "last_name": "Python", "id": 1979, "number": 42},   
      {"first_name": "Monty", "last_name": "Python", "id": 4, "number": 1}  
      ],   
      "id": 0  
 }  
    Here is the script for it:
 function VUSER() {   
      a = createJSONRPCObject();   
      lastName = "Python";  
      http.POSTData = jsonrpc2string(a, "search", {"last_name": "${lastName}"});   
      action(http,"http://www.example.com/rpcService");    
      response = fromJson(http.replyBody); //http.replyBody has the server response, a JSON message
      size = length(response.result);  
      println("there are ${size} results in search for ${lastName}");  
 }   
   This example can be made a little more advanced by reading a list of last names from, say, a CSV file.  We have blogs covering this topic.

    As the last example, let's take a look at an interesting question in stockoverflow.  User needs to parameterize some fields in the named parameters, like the values of x and y in
{ "userid": 123456, "x": 1, "y": 3 } 

     Here is a possible implementation.
 function VUSER() {   
      a = createJSONRPCObject();  
      x = randNumber(100, 300);  
      y = randNumber(200, 500);  
      http.POSTData = jsonrpc2string(a, "move_to_tile", { "userid": 123456, "${x}": 1, "${y}": 3 });  
      action(http, "http://www.example.com/rpcservice/");  
 }   
    It's fairly simple, isn't it?

    JSON is clearly the winner among all the data formats for the internet, so a good performance test platform/tool should make handling JSON messages easier, that's exactly what NetGend is designed to do.

No comments:

Post a Comment