How to test performance of your WebSocket connections using JMeter
A Comprehensive Guide to Performance Testing Your WebSocket Connections
Introduction to WebSocket connections:
WebSocket connections are a communication protocol that enables real-time, bi-directional communication between a client (typically a web browser) and a server.
Unlike traditional HTTP connections, which are request-response-based and require the client to initiate communication, WebSocket connections do not necessarily require the client to initiate the communication. This is how it allows for continuous and simultaneous data exchange between the client and server.
A lot of applications these days use websocket connections(particularly the ones that require real-time updates) such as chat applications, stock tickers, multiplayer online games, collaborative tools, and live streaming.
Testing the performance of WebSocket connections is an essential and often overlooked step in the realm of performance testing. While many performance testers prioritize testing HTTP connections, it is equally important to focus on WebSocket connections. WebSocket connections play a significant role in real-time and interactive web applications, and ensuring their performance is crucial in delivering a seamless user experience.
Let’s see how we can test the performance of websocket connections using JMeter:
Identify the use case:
Identifying use cases is a critical step when testing WebSocket connections. As a tester, it is important to understand the purpose of WebSocket connections within the application, why they are used instead of HTTP connections, and the expected maximum number of real-time connections. Additionally, you should determine the anticipated load, throughput, and data transfer requirements to effectively test the performance of WebSocket connections. By gaining clarity on these aspects, you can tailor your testing approach and ensure that the WebSocket functionality meets the desired performance expectations.
Test the WebSocket APIs/Endpoints without the load:
It is important to test the WebSocket endpoints without the load, to make sure that the functionality is working.
Using JMeter Websocket Plugin:
JMeter doesn’t have built-in support to test websocket (WS) Protocol. You can easily add the WebSocket Connection Test Plugin using the JMeter Plugin Manager:
Create a Test Plan:
To develop a test plan, the first step is to include a thread group, which defines the number of users and their behavior. Once the thread group is added, the next step is to incorporate the WebSocket Open Connection sampler. This sampler establishes a WebSocket connection within JMeter for communication purposes. After adding the Open Connection sampler, there are two important actions to take:
- Integrate a sampler that allows JMeter to utilize the established connection and send read/write commands. This enables stress testing of the connection by sending a substantial amount of data.
- Include a Ping/Pong sampler, which maintains the connection open and continuously sends ping requests while receiving responses. This ensures the connection remains active for the desired duration.
By incorporating these steps into your test plan, you can effectively evaluate the performance and reliability of WebSocket connections using JMeter.
Look at the structure of following test plan:
Within the test plan, you’ll find the primary thread group. In this section, I included the WebSocket Open Connection Sampler along with a header manager. The purpose of the header manager is to pass the authentication header to the sampler. Make sure you add the header correctly for the authentication of the API. I have added a CSV Data Set Config for this purpose. The CSV file contains all the tokens(upto 2000) which can be used to open unique connections.
The structure of the WebSocket Open Connection looks like this:
Inside the Loop Controller:
Inside the loop controller, we have the components like Ping/Pong Sampler and Write sampler. These will run in a loop and use the opened connections.
The structure of Ping/Pong Sampler looks like this:
The structure of Write Sampler looks like this:
In the “write sampler”, you’ll notice that we are utilizing the existing connection. This allows us to leverage the established connection for sending data. Additionally, we have included a few other components:
- Listeners: These are added to obtain comprehensive summary results. Listeners provide valuable insights and metrics regarding the test execution.
- Dynamic variables and environmental variables: These variables are incorporated to enhance the flexibility and adaptability of the test plan. They enable dynamic data generation and facilitate seamless integration with the testing environment.
- Command line execution: The test can be executed through the command line interface. This approach allows for automation and seamless integration with other processes or tools.
Conclusion:
Doing websocket stress testing gives the e2e performance testing coverage a big plus. Traditional HTTPs APIs do create bottle necks, but the bottlenecks created by websockets are in most cases non-recoverable so it is important to diagnose those bottlenecks and resolve those.
By leveraging JMeter’s capabilities, you can accurately simulate WebSocket communication, identify bottlenecks, and optimize the performance of your real-time web applications. But keep in mind that the JMeter solution will be different in case of different use cases and that this blog is the high level explanation of WebSocket connection testing.
Happy Testing!