Introduced with HTML5, the WebSocket protocol allows for more interaction between a browser and website, facilitating real-time applications and live content. WebSocket technology creates a persistent connection between the client and server, circumventing the requirement for a client-initiated HTTP request to trigger a server response. Providing a full-duplex communication channel over a single TCP connection, WebSocket is the most efficient protocol for real-time responses over the web.
If you’re utilizing WebSocket technology, performance testing will boil down to simulating the bi-directional nature of your application.
Synchronous vs. asynchronous calls
First, you’ll need to understand the kind of WebSocket communication your application is using: synchronous or asynchronous calls.
In addition to facilitating real-time applications, WebSockets are also used by web developers as a way of maintaining a faster, longer connection between client and server, even for traditional request-response purposes. This traditional request-response communication via WebSockets results in synchronous calls.
Asynchronous calls, on the other hand, do not require a client request to initiate a server response. The server automatically pushes information and updates over a single TCP connection (which remains open), allowing for an ongoing, bi-directional conversation.
Testers must be aware of the differences between the two in order to properly measure response times and validate the performance of their applications.
Things can get a bit tricky when it comes to measuring the response times of asynchronous calls. Traditionally, testers would measure the time it takes from when a client sends a request and receives a response. With asynchronous calls, the end user’s actions will determine server interactions, and as such, it can be difficult to measure the time it takes to transport the message to the client, or latency.
Because messages are generated by external events and the server decides when to send messages to all connected clients, it’s in testers’ best interest to measure the time it takes for a client to receive a message after the triggering of an external event.
Compared to asynchronous calls, measuring response times for synchronous calls is much easier and more straightforward. It’s related to the Q&A approach where testers merely send a request and wait for the response.
Designing test cases for synchronous calls is simple, as testers only need to understand each request/response as it relates to user interaction. The real challenge lies in designing tests for asynchronous calls.
The nature of asynchronous calls changes the logic required in designed load testing scenarios, and testers face many of the issues also associated with testing streaming media and long polling.
Testers may face hardware and browser compatibility limitations when testing WebSocket technology. An open WebSocket channel facilitates a direct, open connection between the client and server. If there are thousands of customers or connections accessing data via your server, testers will need to adjust the backend accordingly based on the number of sockets a single server can handle.
There are also a few browsers that don’t support WebSocket communication. When this is the case, the application will replace the WebSocket communication with long polling. For performance engineers, this means creating two user paths for each use case (one using WebSockets, the other using long polling). To ensure realistic load testing, testers must take into account the ratio of browsers that are WebSocket-compatible and ones that are not.
Tips for load and performance testing WebSocket technology
The way you measure latency for asynchronous calls directly relates to the application framework. For example, when using Socket.IO, the inclusion of a timestamp within the WebSocket message should be a requirement. Testers can immediately send a message and then, after receiving a response at the client level, calculate the time between the timestamps. There isn’t a standard framework for WebSockets, and of the frameworks that do support WebSocket communication, few automatically include the timestamp. Testers may need to work with developers on including this information in messages. It may be a pain, but testing WebSocket performance is necessary.
To measure response times for synchronous calls, you’ll need to make sure that your load testing solution first supports WebSocket technology. It should also be able to link the WebSocket request with the proper WebSocket response. It’s important to note that the capability to test this asynchronous communication is a rarity among software testing products — choose your tool wisely.
For newer testers and testers used to designing normal web scenarios, designing tests to handle calls via WebSocket can be confusing. It’s going to come down to understanding your application and the nature of the request-response communication. When designing your tests, make sure you’re reproducing the behavior of your application communicating with a real browser.
Designing test cases for synchronous calls is, again, fairly simple because these calls employ traditional request/response communication. To measure their performance, you’ll need to equip your testing team with a load testing solution that enables testing synchronous calls over WebSockets.
Designing test cases for asynchronous calls is a bit more challenging. In this case, users connected via WebSockets will take a specific action from the moment information is displayed on the screen. For example, a user might decide to purchase stock when the price reaches a certain level. Otherwise, the user may take no action at all. Keep in mind that the user action included in your use case depends on the information that does or does not arrive via the WebSocket channel.
To address hardware issues, you’ll need to ensure that you have several servers to balance the load accessing your WebSocket connections. Unlike HTTP communication where the connection is closed after a successful request-response interaction, WebSocket connections remain open. These connections will close if your servers are unable to handle the load, resulting in poor application performance for end users.
To combat browser incompatibility, you can introduce a WebSocket framework as a workaround. Otherwise, you’ll need to design and execute polling scenarios during your load and performance testing.
The nature of WebSockets also poses challenges — it’s a transport layer, so your project could be exchanging text data, binary data, etc. Performance engineers will need to decode or deserialize the WebSocket messages in order to correlate testing scenarios.
WebSockets simply provide a way to exchange data, so this technology isn’t going to drastically change the way organizations deal with tests. Testing teams just have to understand the challenges they’ll face when testing WebSocket technology — like browser incompatibility and collecting response times of asynchronous calls.
Ultimately, equipping your testing team with a load testing solution that not only provides the ability to test request-response apps that leverage WebSockets but can also manage the uninitiated responses sent by the server will result in the most effective, realistic performance testing.
In terms of ensuring a seamless user experience, measuring the latency isn’t enough. To truly validate the performance of an application utilizing WebSockets, you should combine your WebSocket load testing scenarios with scenarios on a browser-based tool like Selenium — but that is a topic for another day.
This blog was originally published in January 2017 and was refreshed in July 2021.