Search code examples
websocketjmeterstomp

Jmeter websocket testing: how to use existing connection between thread groups


Firstly I was using the JMeter WebSocket Samplers. here is the order of operations that I needed to accomplish:

  1. Presenter connect
  2. Presenter create session; save session id
  3. Participant connect
  4. Participant subscribe to commands for saved session id
  5. Start latency test

So based on the flow, I'm gonna need 2 connections to different endpoints (one is for presenter and the other is for participant). enter image description here I needed to make sure the first 4 steps are completed and both presenter and participant connect to the web socket successfully before I start the latency test.
I created 2 thread groups and connected to the endpoints for presenter and participant accordingly. My question is how do I manage to have the requests in the 2 thread groups executed in a specific order? Follow the order, I'd like to execute

  1. Login->Connect->Subscribe in the Presenter thread group
  2. Connect->Subscribe in the participant thread group
  3. send request in the Presenter thread group
  4. The loop controller(which contains a WebSocket Single Read Sampler) in the Participant thread group.

I tested it by putting the send request in a 3rd thread group, and then check run thread groups consecutively option in test plan. But the WebSocket single write sampler in the send request was not be able to use the existing connection anymore. (And there were 2 connections out there for presenter and participant)
enter image description here
And I am a little confused about the order of execution with my current setting. Looks like there were being executed randomly. The first 4 requests within participant thread group looks like being executed before any requests in the presenter thread group in the snapshot below, which messed up the data because the highlighted subscribe request in participant thread group was dependent on the session id that will be passed from request (WS-Subscribe-Presenter-/v1/create/session)in the presenter thread group. Due to the wrong order of execution, the session id that was passed to here was actually not the same session id that was created from the presenter request in this round of test run. It was actually still using the session Id that was created in the previous round of test run.
Note: I passed the session ID as a property such that the value can be passed between thread groups.

import groovy.json.JsonSlurper;

def response = prev.getResponseDataAsString();
String[] res = response.split();
def json = new JsonSlurper().parseText(res[res.size() -1]);

vars.put("sessionUUID", json.data.sessionUUID);
props.put("sessionId", vars.get("sessionUUID"));

enter image description here


Solution

    1. Add a JSR223 PostProcessor as a child of the last WebSocket Sampler in 1st Thread Group

    2. Put the following code into "Script" area

      def connection = sampler.threadLocalCachedConnection
      props.put('connection', connection.get())
      
    3. Add a JSR223 PreProcessor as a child of the first WebSocket Sampler in 2nd Thread Group

    4. Put the following code into "Script" area:

      def connection = props.get('connection')
      sampler.threadLocalCachedConnection.set(connection)
      

    More information on what do these sampler and props mean: Top 8 JMeter Java Classes You Should Be Using with Groovy