Saturday, March 22, 2014

Websocket Load Testing With JMETER


Apache JMeter is a load testing tool which is used to analyze and measure performance for any service that we develop. I am going to cover the usage of the tool with an example that I have developed to load test my application that I have covered in my previous post.
I have developed a websocket application using eclipse jetty . To load test my application using JMeter I have written Customized Java Request . I have followed below sequence :

  • Websocket Connection : Created  client  websocket  to connect to websocket application server.
  • Handshake Request :  Handshake request message is sent  to server with username and connection Id.
  • FriendToFriend Request :  FriendToFriend request is sent to server with a message from the current user to a random friend .

JMeter Approach 

I have divided the above the sequence into two thread groups with same no of users each . First thread group contains customized Java Sampler request which does the below :
  • Creates the websocket client to connect websocket application server. Once the connection is created , the connection object is stored in JMeter variable whose key is the username.
  • Sends the Handshake request to do login with the username .
Once the Java request is completed . I have added a BSF Postprocessor to do the following :
  •  Set the JMeter property 'ThreadValue' to current thread number of the First Thread group. The property 'ThreadValue' will be used later to activate second thread  group.
  • Set the JMeter property  current user name to connection object which is retrieved from  JMeter variable that has been stored in Java request. 
The BSF Postprocessor  will be executed for each thread of the first thread group. Once all  the threads are executed , the execution moves to second thread group . This group has If Controller which checks that all the threads from first thread group has been executed. Once the If Controller succeeds the following requests will get executed:

  • BSF PreProcessor : I have added BSF PreProcessor  . In this pre processor I am setting the JMeter variable current user name to the connection object that has been set in BSF Postprocessor of first thread group.
  • Java Sampler Request :  I have added  Java Sampler Request to send message to randomly selected users . The user's connection object will retrieved from JMeter variable that has been set in  BSF PreProcessor . 

The above requests will be executed for all the threads in second thread group.

Steps to Configure JMeter Test Plan

 1) Launch JMeter and create Test Plan . I have named the Test Plan as 'My Test Plan' .


2) In Test Plan , add thread group . I have named it to 'Login and Connection' . In Thread  properties section I have set Number of threads(users) to 10 and Ramp-Up period to 10 seconds.


3) In 'Login and Connection' thread group I have added config element CSV Data set config. I have named the data set to User Name Set. I have used csv file to store user names in ascending order i.e login1 , login2 .. etc . I have copied the file to bin directory and referred the same in FileName section.  I have kept Variable Name as 'A' which I will referred later. I have changed 'Recycle on EOF' to false and 'Sharing Mode' to current thread group.

4) Next I have added Java Sampler , named it  'Websocket Connection Request' . I have written a custom AbstractJavaSamplerClient  class WebsocketAppJavaSamplerClient . I have overridden two important methods getDefaultParameters and runTest . I have set the classname of sampler to WebsocketAppJavaSamplerClient class. I added two default parameters in the overridden method  getDefaultParameters   which I will populate in the sampler request.

5) Next I added BSF PostProcessor to Java Sampler  'Websocket Connection Request' . The post processor is executed immediately after the  Java Sampler is executed . The language is set to Beanshell .
I have already explained above about the logic that I have written in Beanshell.

6) Next I have added second thread group which is 'Message Sender Group' . I have configured same number of threads and ramp up period as with first thread group ''Login and Connection' .

7) Next I have added If controller , the purpose of adding the controller is already explained .


8) Next I  added CSV Data set config 'User Name Set 2' with the same set of user names but in different file. I have added the data set in IF controller .

9) Next I added Java Sampler ,  'Message Send Request' . I have written a custom AbstractJavaSamplerClient  class WebsocketSamplerClientTest . I have overridden two important methods getDefaultParameters and runTest . I have set the classname of sampler to WebsocketAppJavaSamplerClient class. I added one default parameter in the overridden method  getDefaultParameters   which I will populate in the sampler request.

10) Next I added BSF Pre Processor to Java Sampler ,  'Message Send Request' . The purpose to add sampler , I have explained it already .

11) I have added User Defined Variables and have defined one variable 'noOfThreads' which I am using in IF controller for comparison.

12) I have also added View Results Tree to track the responses of each threads of the thread groups 'Login and Connection' and  'Message Sender Group' .



The custom Java Samplers are wrapped in jar and placed under the JMETER_HOME\lib\ext directory. After the jar is placed the jmeter application can be started . The custom Sampler classes will be populated in the Java Samplers.

I have uploaded the Java Sampler source codes here.



10 comments:

  1. Thanks glinius for reviewing the blog. I will definitely go through the links you provided , however my intention was to create a sampler to test my websocket application here .

    ReplyDelete
  2. Hi prasenjit, did you tried that JMeter-WebSocketSampler plugin?

    ReplyDelete
    Replies
    1. Hi Dinuka,

      I have tried with the JMeter-WebSocketSampler . It works fine as expected . Thanks.

      Delete
  3. Hello Prasenjit.
    Did You worked with Faye (Ruby on Rails) websockets?
    I'm trying to do performance testing with JMeter-WebSocket Sampler https://blazemeter.com/blog/websocket-testing-apache-jmeter, send JSON object in request body, but have only this result without any response from directly websocket:

    Response code: 200
    Response message:
    [Execution Flow]
    - Opening new connection
    - Using response message pattern ""
    - Using disconnect pattern ""
    - Waiting for the server connection for 5000 MILLISECONDS
    - Connection established
    - Waiting for messages for 20000 MILLISECONDS
    [Variables]
    - Message count: 0

    ReplyDelete
    Replies
    1. HI Yura,

      Sorry I dont have hands on with Ruby on Rails or any of its framework with respect to websockets . But the basic concept here is JMeter websocket sampler plugin will create client side socket to establish the connection which is language independent . Have you tried with checking streaming connection check-box provided by the sampler , because if you don't do so the connection will be closed after first response.

      Delete
  4. can you please share Jmeter's JMX file.

    ReplyDelete
    Replies
    1. Hi ,
      I have shared step-wise JMeter test plan above, you can follow the same to achieve . Really sorry , I did not upload JMX file in the in my GIT account . I will do the same , please bare with me.

      Thanks,
      Prasenjit

      Delete
  5. Hi , I have created websocket , but in reponse date some time I got 1 and some time get more the one message as below

    [Message 1]
    This is test websocket message 1

    [Message 2]
    This is test websocket message 2

    [Message 3]
    This is test websocket message 3

    [Message 4]
    This is test websocket message 4

    ReplyDelete
    Replies
    1. HI Rahul,

      I am a bit lost here , have you load tested my example with the above test plan ? Can you please explain the exact issue .

      Thanks ,
      Prasenjit

      Delete
  6. The blog is so interactive and Informative , you should write more blogs like this Ruby on rails Online course Bangalore

    ReplyDelete