Tuesday, March 25, 2014

Performance test on real time HTTP communication: Websocket


    Over the years,  there are many enhancements to HTTP.   One of them is Websocket.

   Websocket makes the communication between a client and a server fully duplex - each part can independently send a message at any time, which is essential for the real-time communication.  This is in contrast to the old style where the client sends a request to the server and waits for a reply.  In order to get real time updates from the server, the client has to either poll the server with requests or send a request to the server with a long time out and repeats it after it receives a message. None of these is very efficient in terms of message size or message count.
 
    Here is a great video that shows the difference that websocket makes on an online drawing web app.  The following is a snapshot in the video.   There are 3 windows, a user draws a graph on the left one and expect the graph to also show up on the other two windows.

  • The middle one is developed using websocket
  • the right one is a standard web app (no websocket)
     One can see the the middle one catches up with the graph that user draws on the left, while the right window is lagging behind.




 








    How do we run performance test on the websocket based web apps?   There are many performance test platforms but unfortunately not many support websocket.    Luckily it's supported on the NetGend and it's fairly easy too!   The following is a simple script that shows how to initiate a connection and do basic operations such as send and receive data.

 function VUSER () {   
    connect("websocket", "ws://echo.example.com");   
    send("hello1");   
    send("hello2");   
    recv(reply);  
    println(reply.msg);   
    recv(reply);  
    println(reply.msg);   
 }  
    The first step is to connect to a server that talks websocket:
           connect("websocket", "ws://echo.example.com");
After that, you can use the function "send()" to send data and the function "recv()" to receive data.  The received message is in reply.msg, you can process it in man different ways,  such as substring matching, regular expression matching etc. NetGend is packed with features for user to process these messages.

     Some of the websocket based apps are for streaming.   Examples of the streamed data include social feed data and  financial ticker data.  The following example shows how easy it is to start testing streaming over websocket - by using a while loop.

 function VUSER () {    
      connect("websocket", "ws://echo.example.org");    
      send("hello");    
      send("exit");  
      while (1) {  
           recv();  
           //println(reply.msg); you can do other processing on the received data.
           if (reply.msg == "exit") { break; }  
      }  
 }   

    Another popular application of the websocket is the location updating, used in many location aware social media apps.  The following is a simple example where we emulate clients who starts from a point and move randomly every 10 or 20 seconds and send location updates to the server.

 function VUSER () {    
      connect("websocket", "ws://locationUpdate.example.org");    
      oc.x = randNumber(10.0, 12.0, "float");
      oc.y = randNumber(20.0, 25.0, "float");  
      while (1) {  
           n = randNumber(10000, 20000);   
           sleep(n); //sleep 10 to 20 second  
           loc.x += randNumber(-0.01, 0.01, "float");  
           loc.y += randNumber(-0.01, 0.01, "float");  
           send(toJson(loc));  
      }  
 }   

    As the popularity of the websocket grows, it's certain that there will be more applications built with it. At Netgend, we are glad that we got the performance testing of it covered.

No comments:

Post a Comment