TO check response of particular id should be diff through jmeter - jmeter

MY response for api is:-
"data":{"shortId":"lu131559"}}
Now i wants to check short id is different for every virtual user.
mns if second hit gone response data is
lu131560
So i want to validate that after 1000 request from virtual user every time i am getting diff id.

If you know the "current" value of the shortId and expect it to be incremented by 1 each time you call your API you can go for Response Assertion and use __intSum() and __counter() functions combination in the pattern like:
lu${__intSum(131559,${__counter(,)},)}
Each time the Assertion will be called it will expect the value to be incremented by one, i.e.
lu131560 - for 1st user or iteration
lu131561 - for 2nd user or iteration
lu131562 - for 3rd user or iteration
etc.
More information: Apache JMeter Functions - An Introduction

Related

How to keep certain thread delay between requests and store correlation id for all the request during load test

May i know how to achieve this scenario in JMeter.
Requirement 1 : Request 1 should execute for 15 mins, once 15 mins crossed request 2 should execute and request 1 to be stopped.
Requirement 2 : In request 1, we need to capture all dynamic value and store it some place and same dynamic value we should use it as request body for request 2. We like to run large numbers of users. Not sure, how to store all the response in some files or other alternatives.
Ex : Request 1 - > Trigger -> Store response somewhere(15 min run & 100 iteration) - stopped
Request 2 - > Trigger after 15 min - Execute request with above 100 iteration response)
Either take a look at Runtime Controller, using this guy you can choose how long its child(ren) will be run or just put your requests 1 and 2 into separate Thread Groups
If you want to store full responses into a file take a look at:
Post-Processors to capture the required part of the response into a JMeter Variable
Sample Variables property to tell JMeter to save this variable into .jtl results file
Flexible File Writer if you want to write the values into a separate file

Rerun getToken(authorization) requests only on expiry - Jmeter

My testplan is as follows:
get token: requests to get access token
Setting a user defined variable to get the tokenGeneratedTime, let's say tokenGeneratedTime to ${__time(,)}
Check if token is expired(if controller) - {__jexl3((${__time(,)} - ${tokenGenerationTime}) > 3599000)}
if true, goes to get token sampler
if false, goes to subsequent requests
Some https requests
So if I run this test plan for the first time(only 1 iteration), request to get token is fired, the condition would be evaluated as false and the subsequent requests are fired. All is well and good. But as you can see, if I run the test plan again immediately(not iterating) for the second time, the get token(authorization) requests are fired again, which is not needed. I need to call the get token requests, only on expiry.
I'm thinking of setting a variable to false, if the token is not expired and putting the get token requests under an if controller. But how do I set a variable in if controller?
If you have any other ways of achieving this, please do suggest.
TIA
Use a flag to check the token expiry. e.g. isTokenExpired
Define the variable in the User Defiled Variables section and set the value to true. This will ensure token request is fired for the first time.
Add an IF Controller to the token request and check the condition ${tokenExpired}. Set the flag to false when a token is created successfully. vars.put("tokenExpired","false")
Add a JSR223 Postprocessor to the top level and check the response code 401 for the token expiry response. If the token is expired, set the flag isTokenExpired to true and redirect the thread to the next iteration immediately ctx.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD .
String responseCode=prev.getResponseCode()
if (responseCode.equals("401")) {
log.info("Token is expired ")
vars.put("tokenExpired","true")
ctx.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD )
}
if I run the test plan again immediately(not iterating)
If you run the test plan 2nd time it means that your tokenGenerationTime variable gets a new value of the current timestamp equal to the start time of 2nd execution.
If you want to be able to run your test plan 2nd time within 1 hour without firing the token generation request you can consider writing the tokenGenerationTime into a file using i.e. __StringToFile() function and read it from the file on 2nd and other test executions using __FileToString() function or if your logic is more complex you can go for __groovy() function and implement whatever you want there
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction

How to pass response body values from one thread to different threads by incrementing the values in the response body in JMETER

I just wanted to extract value from the response body of thread1(1st thread am running once by passing number of threads and loop count 1 each) and need to pass same for the 2nd thread(2nd thread am running multiple times by passing number of threads 10).
This is my response body from Thread1.
{"availablePhoneNumbers":["3052191421","3052192726","3052192566","3052195123","3052194493","3052199654","3052194684","3052199164","3052190020","3052190352"]}
i just wanted to pick the first data 3052191421 and wanted to run in thread2 for all the HTTP requests.
then 2nd data 3052192726
then 3rd data and so on.
Could you please get the solution for this?
Thanks in advance..
Extract first number from the response. This can be done using JSON Extractor configured like:
Names of created variables: number
JSON Path Expressions: $.availablePhoneNumbers[0]
Later on you will be able to use __longSum() function in order to:
add 1 to ${number} variable
return the value
save the result back into the ${number} variable
Demo:

Can you track how long controller was running? jmeter

I'm doing some testing with while controller.
In my test it checks every 3 seconds for a correct response text and if condition is not met, it repeats.
I would like to track how long that while controller was running, repeating itself until the condition was correct.
Is there any way to do that?
You do it via JMeter Functions, to wit:
__time() - to get the current timestamp before entering the While Controller
__longSum() - to calculate the delta between 2 timestamps
Something like:
Before the While Controller starts put the following expression somewhere in your script:
${__time(,before)}
It will store the current timestamp into ${before} JMeter Variable
After the While Controller ends put the following expression somewhere in your script:
${__longSum(${__time(,)},-${before},)}
It will get the new current timestamp and calculate the delta by substracting ${before} variable value from it
Demo:
See How to Use JMeter Functions posts series for more information on using JMeter Functions.
you can use Counter and Bean Shell Post Processor.
Keep Counter Config Element inside the While Controller. The value of Count will be incremented in each iteration.
So, in if condition, you can add BeanShell Post Processor to the sampler inside If controller, and calculate the total time taked using the following formula:
count value * 3 = total seconds waited to reach If Condition.
Following is the placement of elements in the TestPlan:
While Controller
...Counter
...If Controller
......HTTP Sampler
.........Bean Shell Post Processor
Image reference:
Counter Configuration screenshot:
Bean Shell Post Processor code:
int count = Integer.parseInt(vars.get("count"));
int totalTime = count*3;
log.info("total time waited : " + totalTime);
vars.put("totalTime", totalTime);
Screenshot:
Note: the value is saved in totalTime variable, so you can refer the value in subsequent requests using ${totalTime}

JMeter variable scope in threads

I have a JMeter tests which does the following:
It makes a GET request. The request returns some ID which is extracted by a Regular Expression Extractor and is set to a variable myId.
Another GET request is made using this ID stored in myId. It is important that the same ID is used as returned by the request before. Not any other ID!
This simple scenario works fine. But when I increase the "Number of Threads (users)" from 1 to (let's say) 5, I run into concurrency problems:
Thread 1 makes the GET request and assigns the ID to myId.
Thread 2 makes the GET request and assigns the ID to myId.
Now thread 1 runs again and makes the second GET request but with the wrong ID as thread 2 has changed it. Now everything breaks.
How can I avoid this?
As you use the same name for the reference myId, if in the second extraction it doesn't find anything then myId will contain the previous extracted value.
What you can do to check this, is to put in Default Value field:
nv_myId
as per:
http://jmeter.apache.org/usermanual/component_reference.html#Regular_Expression_Extractor
You can use a Debug Sampler to show content of variables

Resources