Enterprise Java

Websockets with Jmeter: Getting Performance Numbers


I can across a problem recently where it was needed to test performance of SockJs/Stomp Spring websocket broker for multiple users . As WebSockets are asynchronous in nature its trickier then normal http/https request-response testing .


Well it doesn’t need to explain why Jmeter is a good tool for perform this kind of testing , but needed a plugin to establish a websocket connection with server from Jmeter using stomp protocol .

This is where I found this plugin pretty useful , as it does take care of boiler plate code for setting up Stomp protocol connection with server layer .

But this plugin has a limitation , once it creates the connection and subscribes to a websocket topic ,it sleeps and once it comes out , it prints all the received messages during the sleep time and ends its execution .

This is normally not suited for interactive applications where our websocket keep receiving notifications based on various activities by other users / activities performed by current user without stopping other screen functionalities to stop .

So we need to make change to above plugin so that websocket keeps listening for messages and our application keeps performing various other activities .

The way to implement is very simple :

a) Change the SockJsSampler.java file to remove the sleep and closing of websocket at the end . Its basically line 181 and 182 .

b) Write a new Java class that extends AbstractJavaSamplerClient.java . This class would be responsible for listening messages coming into your websocket.

c) Now as we have 2 Sampleres ( one for websocket and one for listening from websocket , they could be connected through a queue . In my case I used ArrayBlockingQueue .

d) Modify SockJsSampler to put the message in this ArrayBlockingQueue . This could be done in SockJsWebsocketSubscriptionHandler class of the plugin in handleframe method .

So the idea was to create websocket connection once at the start of thread group execution and keep populating the queue with incoming messages and at various points in test plan use the new sample created in point (b) as Jmeter Java Request Sampler to read the messages from the queue and validate the data .

Regarding time taken to receive the message back on websocket , we could start capturing the time when our custom sampler starts and subtracting time when it receives its expected message. This is more easily done by using result.sampleStart() and result.sampleEnd() of SampleResult API which is inherited in runTest method when you extend this class from AbstractJavaSamplerClient .

Note : If the idea is to run thread group with more then 1 threads to simulate multiple users , you might have to make 1 ArrayBlockingQueue per thread so that the messages are not read by listeners of different threads . This is important because we need to remember , we have just 1 websocket connection shared by all threads . If you are planning to have all threads have their own websocket connections , then you might not have to worry about this point .

Published on Java Code Geeks with permission by Abhijeet Iyengar, partner at our JCG program. See the original article here: Websockets with Jmeter : Getting Performance Numbers

Opinions expressed by Java Code Geeks contributors are their own.

Abhijeet Iyengar

Abhijeet is a Software Engineer working with financial client . He has been involved in building UI and service based applications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Hi Abhijeet, can you share the .jar already modified? Thanks! :)

3 years ago

can you share your code?

Back to top button