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

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

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'},)}

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

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

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

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:

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