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.

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.

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.