How to handle Token expiration and regeneration during load test in JMeter - jmeter

There is an LoginAPI which will generate a bearer token and same token is used as part of header in subsequent apis(API-1, API-2, API-3).
LoginAPI is called under SetupThreadGroup, which will write the tokens to file
OtherAPIs(API-1,API-2,API-3 are called in a seperate thread group which will read tokens and use it in API's)
Currently, token expiration time is 30 minutes.
If we execute load test for 1 hour then post 30 minutes test, all the request gets failed with authentication issue(As tokens have expired)
Can any one suggest a solution to handle re-generation of tokens when expired and re-use the same in load test.
This way, i can run test for a longer duration.

Switch from setUp Thread Group for "LoginAPI" to normal Thread Group and configure it to run either desired number of iterations or forever
Add Flow Control Action sampler to the end of the "LoginAPI" thread group and configure it to "sleep" for i.e. 25 minutes
This way first thread group will execute the Login generating or refreshing the token each 25 minutes so "Other" thread group should be safe
If you need to pass the token from LoginAPI to OtherAPIs you can use either __setProperty() and __P() functions combination or Inter-Thread Communication Plugin

Related

Manage in Jmeter the Expiration time of a Token

How do I manage in Jmeter, the expiration time of a token, so that it does not affect the continuity of execution of the other requests?
Normally when you're getting the token you're getting its TTL as well which could be extracted into a JMeter Variable
So you have 2 options:
Either periodically send the request for the token refresh in another Thread Group (token TTL minus some reasonable number of seconds), the pause between "refreshes" can be introduced using Constant Timer or Flow Control Action sampler
Or make the token refresh a part of the main Thread Group.
When you create/refresh the token you can get the current timestamp using __time() function and store it into a JMeter Variable
Then the current time can be compared to the token refresh timestamp in the If Controller so you will be able to run the refresh request before the current token expires

Passing token from one threadgroup to other and handling the token expiry time

I have generated the auth token in setup threadgroup and have passed the auth token to other threadgroups using setProperty and property.But the token expiry time is 30mins. How do I setup the scenario in jmeter so that this can be handled
Convert setUp Thread Group to "normal" Thread Group
Add a Flow Control Action sampler which will introduce delay of, say, 29 minutes and add more loops to the Thread Group
This way each 29 minutes JMeter will request for the new token and update the property holding the token with the new value.
Also be aware of Inter-Thread Communication Plugin, this way you will not only be able to pass objects between different threads but implement custom logic like the other thread will wait until the object is present in the FIFO queue.

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

execute sampler every 10 minutes in Jmeter

I have sampler which generates token, this token need to be passed to APIs as header. I want to execute token generator sampler every 10 minutes as the token will expire after 10 minutes. How can I handle this in JMeter?
The easiest solution would be creating a separate Thread Group with 1 virtual user and 1 HTTP Request sampler which will be refreshing the token.
In order to make it happen each 10 minutes add Flow Control Action sampler and configure it to introduce delay of 600000 milliseconds
To pass the token to other Thread Group(s) you can convert the JMeter Variable into a JMeter Property via __setProperty() function and read it back using __P() function. If you need a token per thread (virtual user) consider using __threadNum() function (in this case the number of threads must be equal to the total number of virtual users)
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

Running specific sampler in jmeter in intervals

I am running my load tests in jmeter. I have login service and a serviceX which is to be tested. I want Authtoken from login service to run serviceX. Token expires every minute. Currently I am having login service in the same thread group and running as much as serviceX. I dont want to continue this. I want to run login service once in every minute in single thread and pass the token to serviceX and ServiceX runs the defined number of threads and time. how to achieve this?
Use Once Only Controller to achieve this. This controller executes the request inside it only once per thread and passes over any other requests under it during further iterations through the test plan.
So, you can put your login service inside the Once Only Controller and serviceX outside of the controller. You have to configure your Thread Group accordingly for the iterations or you can wrap up your serviceX under Loop Controller.
Example:
Say, you want to login your first thread only once and then wants to run serviceX for 10 times, here is the below test plan sample:
Remember, as you want to log in once in a minute and once the only controller works on per thread, so you have to use rampup your thread groups accordingly. Suppose, there are 2 threads, and they will log in in a one-minute interval, then the Thread group configurations will be like:
Now, if you want to run your serviceX for 5 times after the 1st thread login, put your serviceX under the loop controller scope and the loop count value to 5
This is the results of this sample test plan:
Hope this helps!
If you use only one token across all threads (virtual users) it makes sense to add another Thread Group with 1 thread and infinite number of loops to your Test Plan and add a HTTP Request sampler to it along with the relevant Post-Processor to extract the token. Also add a Constant Timer to add pauses between requests, i.e. use 55000 milliseconds as the thread delay value.
Once you have the token you can convert it into a JMeter Property via __setProperty() function
In your "main" Thread Group you can read the current token value using __P() function

Resources