Use same random variable in all threads - jmeter

I've got test plan :
Thread groups ( users 3, loop 2)
Random Variable
HTTP Request
I want variable to be changed only per loop, so under each iteration all three threads should send same value.
So I want something like this :
request where random var = X
request where random var = X
request where random var = X
request where random var = Y
request where random var = Y
request where random var = Y
I tried a lot of workounds but can't find proper solution.
P.S. I don't want to read variables from file. I need to generate them

No matter whatever you "want" the best option would be pre-generating random values somewhere in setUp Thread Group and writing it to the file and then using CSV Data Set Config in the "main" Thread Group to read the values.
However if this is still not something you "want" here is yet another "workaround", hopefully it's "proper" enough for you:
Add JSR223 PreProcessor as a child of the request which you "want" to parameterize with the random variable
Put the following code into "Script" area:
if (props.get('foo_' + vars.getIteration()) != null {
props.put('foo_' + vars.getIteration(), org.apache.commons.lang3.RandomUtils.nextInt(0, 100))
}
Refer the "generated" random value using the following __groovy() function where required:
${__groovy(props.get('foo_' + vars.getIteration()),)}
Demo:

Related

Generate random values in JSR223 sampler in JMeter

def index = [];
def randoms = [];
def size = new File("C:/Users/320027671/Desktop/JmeterPerformanceSuit/CompleteSuit/STU3/Post/index.csv").readLines().size();
File file = new File("C:/Users/320027671/Desktop/JmeterPerformanceSuit/CompleteSuit/STU3/Post/index.csv");
file.each { line ->
index << line
randoms << __Random(0,size,)
}
The script is giving error
the method does not exists
the scirpt is working uptil index << line, the problem is with random function
I assume you use groovy as language (otherwise it won't work)
You can't use JMeter functions inside JSR223
You can randomize every line using for example RandomUtils:
org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
Your approach may fail to produce "random" numbers, especially on lesser file sizes you can get duplicate values in the randoms list so I would recommend doing something like:
1.upto(size, { i ->
randoms.add(i)
})
Collections.shuffle(randoms)
This will populate randoms list with the numbers from 1 to the length of size and then calls Collection.shuffle() function in order to "randomise" the list.
Just in case check out Writing JMeter Functions in Groovy for more insights.

Assigning values to different variable in JMeter

I have two different array with values as below:
Code = [8,9,10]
Value = [4,5,6]
I need to get the values from each array (above mentioned) randomly and assign it to different variable like below:
Code 1 = 9 , Code2=10
Value1 = 4 , Value2=6
Or is there any way in Jmeter to Pass that array to another sampler thereby assigning it to different variables.
How can we achieve it on Jmeter ? Any help / Suggestions is welcome!
Your values look utterly like JSON Arrays so my expectation is that you could handle it more easy using JSON Extractor
Just in case I'm wrong you can get random code and/or value using the following Groovy code in any of JSR223 Test Elements
import org.apache.commons.lang3.RandomUtils
def codes = vars.get('Code').findAll(/\d+/ )*.toInteger()
def values = vars.get('Value').findAll(/\d+/ )*.toInteger()
def randomCode = codes.get(RandomUtils.nextInt(0,codes.size()))
def randomValue = values.get(RandomUtils.nextInt(0,values.size()))
log.info('Random code: ' + randomCode)
log.info('Random value: ' + randomValue)
Demo:
You can use "Config Element" > "Random Variable" where you can give a range and ask for a random number within that given range.
Hope it helps.

How to call Jmeter Counter Reference Name variable incrementing one

In my testplan, I'm using Counter config element and have configured start = 1, increment = 1, maximum = 4 and the ReferenceName = loopCount.
And in a sampler, I have used Json Extractor as well where I want to set the
Match No: ${loopCount} + 1
since in the Json file I'm extracting always the first match is null.
Unfortunately I think I can't give it like
${loopCount} + 1.
Is there any work around for this.. please help.
You can add a BeanShell Sampler after the Counter config element with the below code in the code area:
int Counter = Integer.parseInt(vars.get("loopCount"));
vars.put("Increment", String.valueOf(Counter+1));
And now you can use the variable ${Increment} as the Match No.
OR
You can use ${__intSum(${loopCount},1)} which will add 1 to the value of loopCount variable directly.

Jmeter: Comparing 2 JSON Extractor Variables and updating .jtl accordingly

Scenario: I have 3 HTTP requests to make as follows:
Step1: Make request1 and use JSON extractor to extract a value from JSON response and store it in a variable say x
Step2: Make request2 and wait for 2minutes(I'm using Constant Timer).
Step3: Make request3 and use JSON extractor to extract a value from JSON response and store it in a variable say y
Step4: Compare 'x' and 'y' and Pass the test in jtl file if y > x else fail.
Issue: I'm not able to find out the way to complete step4.
x and y are numbers insider JMeter variables, for step 4 use JSR223 PostProcessor as a post processor of request3,
In code convert variables to numbers and compare it as and if y >x fail the sampler:
x = vars.get("x");
y = vars.get("y");
if (Integer.parseInt(x) >= Integer.parseInt(y)) {
log.info("x is bigger than y, continue test");
} else {
prev.setSuccessful(false);
}
Example in Java/Beanshell language, but you can use groovy also.

how to use variable in jmeter like counter

I'm using loop controller inside the jmeter script, and I'm not able to fetch previous variable value in pre-processor beanshell.
Example:
var temp = 1; log.info("before : "+temp.toString()); temp++; prev.put("t",temp.toString());
Thanks in advance
To save values b/w iterations:
Following is one of the ways to store and retrieve the values b/w iterations:
log.info("temp prev value " + vars.get("temp")); // first iteration returns null
vars.put("temp","something"); // store or override the value, so it will be available in next iterations.
To know iteration number:
If your need is to know the iteration number, then use Counter:
In beanshell preprocessor, access using reference name (counter) as shown below:

Resources