JMeter - How to loop through "bsh.shared" ArrayList in a single Thread Group? - performance

I have a BeanShell PostProcessor under the setUp Thread Group.
I put the ArrayList into the "bsh.shared" namespace like:
List personIdsList = new ArrayList();
...
bsh.shared.personIds = personIdsList;
I know how to read the value via __BeanShell function as:
${__BeanShell(bsh.shared.personIds)}
And I want to loop through this array in other Thread Group. (${personId} - it should iterating value from list)
Could you, please, tell me how to do that?
Thanks!

The easiest way would be using While Controller together with the Counter test element like:
Add While Controller to your 2nd Thread Group and put the following expression into the "Condition" area:
${__BeanShell(Integer.parseInt(vars.get("counter")) < bsh.shared.personIds.size()-1,)}
Add Counter as a child of the While Controller and configure it as follows:
Start: 0
Increment: 1
Maximum: ${__BeanShell(bsh.shared.personIds.size()-1,)}
Reference Name: counter
Refer the "current' person ID as ${__BeanShell(bsh.shared.personIds.get(Integer.parseInt(vars.get("counter"))),)}
where required
Demo:

Related

How to create a counter in JMeter and save the value for the next execution?

i've been trying to save the value of a counter once the execution finishes, with the idea that the next one starts with that same value. For example: I start with a counter that has 1 as value, loop it 5 times and the execution finishes with that counter having his value in 5. Then, i want that counter to start with his value in 5, how is this doable?
You can save it into a file using a suitable JSR223 Test Element like:
new File('counter.txt').text = vars.get('your-counter-variable-name-here')
where vars stands for JMeterVariables 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
the same for initialization, you can use __groovy() function with the following code:
${__groovy(file = new File('counter.txt'); if (file.exists()) {return file.text} else { return '0'},)}

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 increment a variable while each time test plan is executed in Jmeter

I have a scenario to run a test plan multiple times in a day, during the first execution of my UDV sequence should be "xxxx-1". Subsequent execution within the day the UDV sequence should get incremented like "xxxx-2", "xxxx-3", etc. I tried by putting a Bean Shell Post processor with an if condition.
Need to run daily, run the test every four hour interval and reset the counter back to 1 at 5th execution.
The only way to store the variable between Test Plan executions is to write it into a file or a database table.
To do it with the file:
Add setUp Thread Group to your Test Plan
Add JSR223 Sampler to the setUp Thread Group and put the following code into "Script" area
def file = new File('number')
if (!file.exists() || !file.canRead()) {
number = '1'
}
else {
number = file.text
}
props.put('number', number as String)
Add tearDown Thread Group to your Test Plan
Add a JSR223 Sampler to the tearDown Thread Group and put the following code into "Script" area:
def number = props.get('number') as int
number++
new File('number').text = number
You can refer the generated value using __P() function as xxx-${__P(number,)} where required.
More information: Apache Groovy - Why and How You Should Use It

JMeter-For each implementation

I have an API which takes multiple IDs as a parameter:
http://pqa-volpqa.unknown.com:8080/items/batch/?Ids=00000017072571,00000017072588,00000017072595,00000019786230,00000019987460,00000019988238,00000019988283,00000019990170,00000020015206,00000020015213
Now these IDs are mention in a CSV file like below:
00000017072571
00000017072588
00000017072595
00000019786230
~~
~~
~~
00000020015213
How can I implement this?
If they are as a single string in CSV file you can just use __StringFromFile() or __FileToString() function to read them directly into request parameter
If they are each one a separate line it is still possible with __groovy() function like:
${__groovy(def line = new File('/path/to/your/file.csv').getText().replaceAll(System.getProperty('line.separator')\,'\,'),)}
See Apache JMeter Functions - An Introduction to learn more about JMeter Functions concept.
You can add a Loop controller with Loop count = number of IDs you want to add. Then add a Counter inside the loop controller with the below configurations:
Start: 1
Increment: 1
Reference Name: Counter
Check the boxes for Track counter independently for each user and Reset counter on each thread group iteration
Add a CSV Data Set Config after the counter with the below configurations:
Filename : The full name to your file // if the csv file is not in the same folder with your jmx file you should enter the full path to your CSV file.
Variable Names : id
Finally, add a BeanShell Sampler after the csv data set config with the below code in the code area:
String id = vars.get("id");
int Counter = Integer.parseInt(vars.get("Counter"));
if (Counter == 1){
vars.put("IDs", id);
}
else{
vars.put("IDs", vars.get("IDs") + "," + id);
}
All of the above should be before your API, now you can use your API as below:
http://pqa-volpqa.unknown.com:8080/items/batch/?Ids=${IDs}

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

Resources