How to create a dynamic request variables in JMeter - jmeter

I have a few values in CSV files. I need to pass this values in JSR223 sampler. if suppose, in CSV files it has 4 variables are present in first row then i need to write two line below.
Example 1 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
If 6 variables are present in second row of CSV files, they my script should have three lines.
Example 2 in JSR223 sampler :
('usr', '${Var1}', '${Var2}')
('usr', '${Var3}', '${Var4}')
('usr', '${Var5}', '${Var6}')
My scenario here is, each row will have different count of values would be present. So, how can i create my JSR223 sampler request count based on the values counts that is present in CSV files. May i know how to create this scenario in JMeter.

Something like:
def lines = new File('test.csv').readLines()
lines.each { line ->
log.info('Line from CSV: ' + line)
def request = new StringBuilder()
def values = line.split(',')
def counter = 0
1.upto(values.size() / 2, { it ->
request.append("('usr',").append(values[counter]).append(", '").append(values[counter + 1]).append(" ')")
if (it != values.size() / 2) {
request.append(System.getProperty('line.separator'))
}
counter = counter + 2
})
log.info('Generated request: ' + System.getProperty('line.separator') + request.toString())
}
should do the trick for you.
Demo:
More information:
Reading a File in Groovy
The Groovy Templates Cheat Sheet for JMeter

Related

Need help in write file operations in Jmeter

Need help in write file operations in Jmeter with below code:
a = vars.get("PARAM_1");
b = vars.get("PARAM_2");
f = new FileOutputStream("FILEPATH/filename.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(a +","+ b);
f.close();
I am using in bean shell post processer but above code only will able to add 2 variables in the CSV file but I tried and defined more than 2 like
a = vars.get("PARAM_1");
b = vars.get("PARAM_2");
c = vars.get("PARAM_3");
d = vars.get("PARAM_4");
e = vars.get("PARAM_5");
f = new FileOutputStream("FILEPATH/filename.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(a +","+ b","+ c","+ d","+ e);
f.close();
With the above code not write with all 5 variables, write with only 2.
And also for me, data writing needs to happen with the 2nd line of the CSV not with the 1st line of the CSV because the 1st line has the headline of the data.
Using Beanshell is some form of a performance anti-pattern, since JMeter 3.1 you're supposed to be using Groovy for scripting, more information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown
You can amend you code to look like:
def file = new File('FILEPATH/filename.csv')
1.upto(5, {
file << vars.get('PARAM_' + it)
if (it != 5) {
file << ','
}
})
file << System.getProperty('line.separator')
If you run your script with 2 or more users you might face a race condition so when 2 or more threads will be concurrently writing into the same file it will result in data corruption or loss. So it's better to consider using i.e. Flexible File Writer instead
In any case make sure that the variables exist and have their respective values using Debug Sampler and View Results Tree listener combination as if the variable is not there you will have null value written to the file

Use same random variable in all threads

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:

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.

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.

Resources