jmeter unique id per thread for http request - jmeter

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.

Related

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.

How to get Maximum value from correlated IDs in 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

Not able to access Jmeter property in the other thread group.

In 1st Thread group, in bean-shell Post processor I have added following code to set Jmeter Property with name "id":
int abc=10
int start=${abc}+1;
${__setProperty("id",start)};
print(props.get("id"));
In Second thread group, I am trying to access the value of "id" in beanshell using:
int pq=${__P("id",1)};
Now, The value of 'pq' should be 11 but it takes default value which is '1'.
When I check in Debug PostProcessor, the value of id is string 'start' and not 11. I am not sure what changes are required. One more interesting thing I noticed is: in console it prints 11 for "print(props.get("id"))" where as in jmeter property it stores the string value 'start'.
Any help is appreciated.
First of all, usual notice, don't inline variables and function calls into scripting-based test elements as they may misbehave.
So you should amend your code like:
First Thread Group:
int abc=10
int start=abc+1
props.put('id', start)
Second Thread Group
int pq = props.get('id')
log.info('Property value: ' + pq)
NB: The above code assumes using of JSR223 Test Elements and Groovy language

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.

Same random number and string for each thread

I need to generate the same JSON data twice for each thread for an HTTP request. I am having problem setting that up in JMeter.
My structure is:
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- Random Variable,
- HTTP Request
I tried the combination of Per Thread User set to true and using seed for random function, but I can't achieve what I want. It keep on generating new number/string per loop.
Basically for each user, I want the exact same JSON request data.
I see three decisions for your case:
Use User Defined Variables before HTTP Request. Specify variables here. In this case it will be better to use __Random() function instead Random Variable.
Test Plan
- HTTP Header Manager
- Thread Group 1 users, 5 loop
- User Defined Variables, (Name:varName; Value:${__Random(1,100)})
- HTTP Request
Use Loop Controller (loop count=5) before HTTP Request (instead loop=5 in Thread Group)
I can suggest another solution which seems to me verbose and awkward but no other idea.
You create file using BSF (or BeanShell) PreProcessor and write value of random variable into file. Then read file before every request. In the next example I used Groovy and BSF PreProcessor.
import java.util.Random
def out= new File('File1.txt') // create file if it is not exists
if(!out.exists())
{
out.createNewFile()
Random rand = new Random()
int max = 10
def a = rand.nextInt(max+1)
out << a // write text to file
}
//then read value of generated variable
String fileContents = new File('File1.txt').text
//then put your variable into User defined Variable that I named HELLO
vars.putObject("HELLO",fileContents)
And in needed request use ${HELLO}

Resources