How to get Maximum value from correlated IDs in Jmeter? - jmeter

I have scenario where I need to capture dynamic Order-value IDs from response and among those values, I need to select maximum value and pass it to the next request.
How can we achieve this with the help of Jmeter tool?

Assuming you have the following JMeter Variables from the PostProcessor:
foo_1=1
foo_2=5
foo_3=10
foo_matchNr=3
You can get the maximum value as follows:
Add JSR223 PostProcessor as a child of the request, make sure it goes after the PostProcessor which returns your Order-ID values
Put the following code into "Script" area:
List values = new ArrayList()
for (int i=1; i <= (vars.get('foo_matchNr') as int); i++) {
values.add((vars.get('foo_' + i) as int))
}
vars.put('foo_max', Collections.max(values) as String)
Assuming everything goes well you should be able to access the maximum value as ${foo_max} where required.
See Apache Groovy - Why and How You Should Use It article for more information on Groovy scripting in JMeter tests

Related

User Beanshell processor to write code so that data driven testing performed rather than using csv

I want to test a Get Request with list of values. I dont want to use CSV ,
so i started using Beanshell Preprocessor and has those values in Array. Then used for loop to use those values and send to Get Request in HTTP Request. Every this was Successful except sending values to Get request. It is reading all values and sending the last read value to Get request.
Question : I want my request to run for each value when code reads the data one by one.
var TtValuedetails;
int i;
n=22;
String[] ttvalue = {"34324324224","fdadsfadsf","dfdsfdsfds","dafadsfa",
"45435435","dfadsfads"
};
for(int i=0;i<n;i++)
{
if(i==0)
{
TtValuedetails=ttvalue[i];
if(ttvalue[0]=="34324324224")
{
vars.put("TtValuedetails",TtValuedetails);
log.info(TtValuedetails);
log.info("first value is executed" );
Org.Apache.......startRequest();
}
}
} ;
We cannot help you without seeing the full code and knowing what you're trying to achieve but one thing is obvious: you should not be using Beanshell, since JMeter 3.1 it's recommended to use Groovy for scripting.
Current problem with your code is:
ttvalue array contains 6 elements
n=22
the line TtValuedetails=ttvalue[i]; will cause failure on 7th iteration of the loop due to IndexOutOfBoundsException
If you want to send a request for each value of the ttvalue array the easiest is converting it into separate JMeter Variables like:
ttvalue_1=34324324224
ttvalue_2=fdadsfadsf
etc.
and using ForEach Controller for iterating the values.
Example Groovy code for storing the values into JMeter Variables:
String[] ttvalue = ["34324324224", "fdadsfadsf", "dfdsfdsfds", "dafadsfa",
"45435435", "dfadsfads"];
for (int i = 1; i <= ttvalue.size(); i++) {
vars.put("ttvalue_" + i, ttvalue[i - 1]);
}

Jmeter- how to pass a fixed number of values from an array at a time until iterating through all values to a http request

In my get htttp request, I get an array of ids ex: list=[1, 2, 3.....1000]
And then for my next http request, I want to pass all the values in the list 10 at a time, so it will be 100 requests total and each time, it takes 10 values from the list array. I'm using a loop controller to call the http request 100 times. but I dont know how to retrieve 10 values at a time, and then go to the next 10, next 10 until all values are used.
How should I do that?
Add a regular expression extractor to your get request as a child.
Add the following properties shown in the screenshot to extract List
Add a Beanshell sampler/ JSR223 sampler and add the following code.
The code below creates a series of variables and store series of 10 values in a variable starting from Final_0 to Final_99
import java.util.Arrays
String complete_List=vars.get("List");
String[] complete_List_Array = complete_List.split(",");
int i;
for(i=1;i<=complete_List_Array.length;i++)
{vars.put("List_"+i,complete_List_Array[i-1]);}
int j;
int loopcount=complete_List_Array.length/10;
vars.put("loopcount",Integer.toString(loopcount));
for(j=0;j
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for(i=j*10+1;i<=(j+1)*10;i++)
{
sb.append(vars.get("List_" + i));
if (i < (j+1)*10)
sb.append(",");
}
sb.append("]");
vars.put("Final_"+j,sb.toString());
}
vars.put("counter","0");
Add a loop counter and mention loop count as ${loopcount} as shown below
You can add HTTP Request as a child of loop counter and to pass series of 1o value at a time use ${__V(Final_${counter})}
Add a beanshell post processor to the http request to that will increment the counter
add the following code to the beanshell sampler
int counter = Integer.parseInt(vars.get("counter")) +1;
vars.put("counter",Integer.toString(counter));
You can see in the results its passing series of 10 values at a time and loop will run for 100 times
For more info on beanshell please follow the link
Please let me know if it helps.........
Considering you have the list array available at your disposal by using any extractor.
Extract the sublist from the array and put them in properties. Then, fetch the properties where ever you want it.
In the below I am fetching the sublist from the array and putting it in jmeter properties.
Here, I am fetching the values from the properties. This is just for demonstration and you dont need this. After putting list in properties just fetch in HTTP sampler as shown in the last image.
Now, to fetch it in HTTP sampler you can use loop and counter and fetch the properties using groovy. Loop for iteration and counter for increment the variable mylist_x.
Hope this helps.

Jmeter: Comparison of multiple result for result response with JDBC variables

In j meter: In a bean-shell assertion or any other I want to match the content of response which I have fetched using Jason extractor suppose:
Result[1]=A, Result[2]=b, Result[3]=c
and so on Versus variables I have fetched using JDBC pre-processor which has saved as:
Result_1=, Result_2=B, Result_3=c.
I am able to match 1 variable at a time but not all at one time. so need help with bean-shell code to compare all result at once.
Add JSR223 Assertion somewhere to your test plan (normally as a child of a Sampler you would like to fail)
Put the following code into "Script" area
for (int i = 1; i <= Integer.parseInt(vars.get('ResultFromJSON_matchNr')); i++) {
if (!vars.get('ResultFromJSON' + i).equals(vars.get('ResultFromJDBC_' + i))) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Variables mismatch')
}
}
Make sure you have groovy selected as the "Language" and have Cache compiled script if available box ticked
The above script assumes ResultFromJSON and ResultFromJDBC JMeter Variables reference names, amend them according to your real definitions.
More information: Scripting JMeter Assertions in Groovy - A Tutorial

How to post the exact number of values which are retrieved from post processor?

I'm landing into a page where some Customers listed out. I'm retriving all the Customer Ids by Regular Expression Extractor Post-Processor.
Now, in a subsequent request I need to pass those Ids. It's being passed as a 'Body Data' by the following format:
[19327,15947,14421,18813,20942]
Let say, there are 5 Customer Ids retrived, then I can use the variables for each record as follows:
Passing Variables: [${CustomerId_1},${CustomerId_2},${CustomerId_3},${CustomerId_5}]
Posting Variable Values: [19327,15947,14421,18813,20942]
But let say, there are only 3 Customer Ids retrived, and if I pass the variables as above then the sampler will fail because currently it just retrived 3 Customer Id records.
Passing Variables: [${CustomerId_1},${CustomerId_2},${CustomerId_3},${CustomerId_5}]
Posting Variable Values: [19327,15947,14421,${CustomerId_4},${CustomerId_5}]
How to deal with this issue, plz. do help.
Thanks in advance
Add Beanshell PreProcessor as a child of your 2nd request
Put the following code into the Pre-Processor's "Script" area
int customers = Integer.parseInt(vars.get("CustomerId_matchNr"));
StringBuilder requestBuilder = new StringBuilder();
requestBuilder.append("[");
for (int i = 1; i <= customers; i++) {
requestBuilder.append(vars.get("CustomerId_" + i));
if (i != customers) {
requestBuilder.append(",");
}
}
requestBuilder.append("]");
sampler.getArguments().getArgument(0).setValue(requestBuilder.toString());
The above code will build required request line depending on number of matches and automatically set request body.
Referenced classes:
vars - shorthand to JMeterVariables
sampler - provides access to the parent sampler, in case of HTTP Request it will be HTTPSamplerProxy
StringBuilder - comes with Java SDK
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on Beanshell scripting, explanation of pre-defined variables and some useful examples.
refName_matchNr will return the number of matches. So in your case CustomerId_matchNr will give you number of customerIDs.
Use a beanshell pre-processor to run a loop and construct the BodyData string and save it to a new variable, use that variable in your subsequent sampler.

jmeter unique id per thread for http request

My jmeter tests make a http request which contains a unique id.
http://myserver.com/{uniqueId}/
I want to set the base number (say 35000) and increment for each thread, for example my ids would be 35001, 35002, 35003 ...
http://myserver.com/{base + count}
I see functions for __threadnum and __counter but how would I:
set it in a variable so that I can substitute it in my url
add this count to my base value
I would simply go with Beanshell pre processor.
int threadNo = ctx.getThreadNum()+1; //to get the thread number in beanshell
int base = 35000;
int uniqueId = threadNo + base;
vars.put("uniqueId", Integer.toString(uniqueId));
Now your HTTP requests as given below should have the value substituted.
http://myserver.com/${uniqueId}/
To set a variable per thread you can use User Defined Variables.
To sum, you can use functions:
__intSum
__longSum
Or use a JSR223 PRE Processor or JSR223 POST Processor + Groovy and do it in Groovy.

Resources