JMeter - Recurring request every x seconds without blocking other requests - jmeter

I'm trying to load test our web app server side with JMeter, everything works so far except one thing.
I have a heartbeat loop the app client side that pings the server every x seconds.
I tried the while processor but then the thread is just "stuck" there (as expected).
Is there a way to execute a request every x seconds while the other requests are still executing?
I also would need access to the variables of my thread since the heartbeat sends a unique id, so another thread group won't work as far as i understand.
The id is read from one of the first request responses into a variable so the heartbeat requests should only start when this variable becomes available.

Another separate Thread Group to send heartbeat requests is the easiest way to achieve, if you need to pass a value from the "main" thread group you can do it in 2 ways:
Use __setProperty() function in the "main" thread group to convert the variable into a property and use __P() function in 2nd Thread Group to read the value. If you have more than 1 thread it makes sense to add __threadNum() function to make the value thread-specific.
Use Inter-Thread Communication Plugin if you don't mind using JMeter Plugins

Related

How do we run multi concurrent sessions Websocket testing using JMeter?

I was using the JMeter WebSocket Samplers to perform WebSocket latency testing. My test plan contains 4 thread groups.
Firstly, here is the order of operations I was producing, each step is grouped in a thread group. And the connection for the 1st. operation on Presenter was cached before executing the 2nd. operation because the 3rd. operation, the same Presenter, is gonna continue to use this connection.
def connection = sampler.threadLocalCachedConnection
props.put('presenterConnection', connection.get())
Same idea for the 2nd & 4th connection for Participant. The participant at step 4 is gonna continue to use the connection created at step 2.
Presenter connects to WS, creates session & saves the session id
Participant connects to WS & subscribes to the saved session
Presenter sends out commands
Participant reads the commands that were sent by Presenter
It worked as expected with single session. However I ran into an issue with the WebSocket Single Read Sampler at the 4th operation of reading commands that were sent from Presenter when I was running 2 concurrent sessions.
I changed the "Number of Threads" to 2 in the thread properties of each thread group. The presenter was able to create 2 session IDs at the 1st. operation. And I added the "Inter-Thread Communication PostProcessor" to store the session IDs in a queue such that it's able to be passed to other thread group. It worked for the first 3 operations until it went to the WebSocket Single Read Sampler in the last operation of reading the commands that were sent from the Presenter.
I had a JSR223 PreProcessor in place to use the cached connection at step 2.
def connection = props.get('Participant1Connection')
sampler.threadLocalCachedConnection.set(connection)
The issue was found in the response body of the 2 threads of the last operation. The first session ID was not being used. The first user at step 4 was using the 2nd session ID and the 2nd response body was empty because the 2 users were using the same session. What shall I do to fix this issue to have 2 users reading data from 2 sessions created accordingly?
This is something which normally happens when people copypaste the code without understanding what it is doing. Moreover if you need to pass data between thread groups most probably there is a problem with your test design.
Whatever.
As per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So when you have > 1 thread (virtual user) the second thread overwrites the value of the presenterConnection property so no matter how many threads you have they all are trying to use single instance of the presenterConnection
My expectation is that you want a connection per thread, in this case you should add current thread number as a prefix/postfix for the property key so:
1st thread would use presenterConnection1
2nd thread would use presenterConnection2
etc.
Amendment to "your" code would be something like:
props.put('presenterConnection' + ctx.getThreadNum(), connection.get())
similarly for accessing the connection.
In the above example ctx stands for JMeterContext class instance, see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.

Jmeter ,Way to call rest API for every 3minutes to get Auth token

I am working on setting up a way to do a performance test for the rest API's using Jmeter which includes token expiry for every three minutes.
1.)Currently, I created two thread groups one for making a call to get "access_token" and setting into the property using Bean shell assertion setProperty, to be used in other thread Groups.
2.)I could see Second thread group can access the value set in the first step.
But my goal is to execute the first thread group every 2min 30 seconds continuously so that the second thread group can get the new token every 2 min 30 seconds.
I tried a constant timer, but didn't seem to work is there any way to do this or any other timer to use to achieve this token refresh?
Thanks in advance
The Constant Timer should work, but be aware that it creates a delay before each Sampler it it's scope so it might be the case your setting it up wrong, you can use i.e. a Dummy Sampler and put the Constant Timer as a child of the Dummy Sampler, this way your HTTP Request to get token will be executed, followed by the delay defined in the Constant Timer, followed by the Dummy Sampler, et.c
Example setup:
You might find Flow Control Action sampler easier to use
Also be aware that starting from JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting. Moreover it's not required to write any code, you can go either for __setProperty() and __P() functions combination or for Inter-Thread Communication Plugin

How to answer incoming ping without pausing in jmeter websocket?

I am using "WebSocket Samplers by Peter Doornbosch" plugin
The system will send a heartbeat message to the client every 30 seconds.
The client must respond for not disconnect to the system.
I want to do the heartbeat part without pausing the current testing process.
What I tried:
Create a Single read sampler in While timer. It will pause the testing process.
Create two thread groups. The Websocket object (connection ID) cannot be shared between two thread groups.
Tried plugin -- Parallel Controller & Sampler, unfortunately it cannot share the websocket object as well.
Edit:
For 2, I want the websocket object can be shared between threads.
1 thread for heartbeat job, 1 thread for doing test. Even I input the same connection ID in the second thread, jmeter still treat it as different object.
You cannot share a websocket connection between threads. This is by design. The solution for your use case is having a single read sampler with a timeout of 30 seconds, followed by a sampler that responds to the heartbeat. You can use the "optional read" option with the read sampler to avoid it generates an error when no message is read during this 30 second period.
The Websocket object (connection ID) cannot be shared between two thread groups.
actually it is possible, as per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
so there are 2 options for sharing the data between different threads (even if they're in different thread groups)
Use __setProperty() function to convert JMeter Variable into a JMeter Property in first Thread Group and __P() function to read the value in 2nd Thread Group
If your use case is more complex and you want i.e. to wait until connection ID is available you can consider using Inter-Thread Communication Plugin

JMeter - How to handle Long polling transport request method

In my Jmeter script request which contains the 'LongPolling' transport method and it takes a high load time or response time
Also, the response message content value is used for the next consecutive request. How can I handle that scenario?
I don't want this my wait time for the server response
JMeter thread model assumes that a thread (virtual user) always waits for the previous sampler to fully complete before starting the new one.
The easiest solution is moving your "long-polling" requests into a separate thread group and if you need to pass data between thread groups either use __setProperty() and __P() functions combination or go for Inter-Thread Communication Plugin
If you need to have everything within the single Thread Group - you can consider using Parallel Controller, however it might not be fully suitable for your needs as it is still a Sampler hence it waits for all its children completion prior to proceeding
Depending on your use case you can find jmeter-asynchronous-http plugin useful, see How to Load Test Async Requests with JMeter article for more details if needed.

How to start a Jmeter thread from another thread?

I have Thread A that performs user login, setting some settings and creating some records. While working on the records page, there is a background thread, Thread B, that is being called every 15s, performs some sort of sycnronisation, but only when I'm on the records page.
What I managed to do is created a second thread that will fire these requests each 15s. Using a BeanShell PreProcessor I share the cookies between the two threads, so that the Http cookie manager in the second thread uses the same variables/values as the first one. My requests are working fine.
What I can't figure out is how to trigger Thread B when the Thread A has reached the step that involves records. One option is to delay Thread B for a certain amount of time, but this isn't very reliable as I can't know before hand how long it takes Thread A to finish creating the users.
Is there a way to trigger a thread from another thread?
Take a look at Inter-Thread Communication Plugin - it allows synchronizing actions between different threads (even if they are in different thread groups) basing on a simple FIFO queue.
The logic would be:
When you did some action by Thread A put something into the queue using fifoPut function
Thread B will be scanning the queue and if anything comes up it will get the value and start execution of its own actions.
Check out SynchronizationExample.jmx test plan for reference implementation.
You can install Inter-Thread Communication plugin using JMeter Plugins Manager
Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so kindly avoid Beanshell going forward.

Resources