Home » Java » Enterprise Java » Websockets with Jmeter: Getting Performance Numbers

About Abhijeet Iyengar

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

Websockets with Jmeter: Getting Performance Numbers

Problem:

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 .

Solution:

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Luis
Luis
1 year ago

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

Pipe V.
7 months ago

Hi!
can you share your code?