Using specific CSV rows in JMeter CSV dataset (based on condition) - csv

I have a single CSV file with following sample data..
Column headers:
USER_VAR,PASSWORD_VAR,APP1,APP2,APP3
Actual data:
username1,password1,true,blank,blank
username2,password2,true,blank,blank
username3,password3,blank,true,blank
username4,password4,blank,true,blank
username5,password5,blank,blank,true
I have 3 different (independent) JMeter Scripts.
JMeterScript-APP1
JMeterScript-APP2
JMeterScript-APP3
I want to use the same CSV file with all these 3 scripts in such a way that each script only use the specific rows in given CSV.
JMeterScript-APP1 should only process first 2 rows (APP1=true).
JMeterScript-APP2 should only process 3rd and 4th row (APP2=true).
JMeterScript-APP3 should only process 5th row (APP3=true).
I have tried implementing this scenario using CSV dataset config. But it seems that JMeter does not provide any built in support for implementing this. Can anyone share workarounds for doing it?
If required I can also manipulate the data in last 3 columns of CSV before feeding it to JMeter scripts.

I believe the easiest way is using If Controller where you can check if this or that variable is set like:
${__javaScript(vars.get("APP1") != null,)}
So if ${APP1} variable is not defined underlying samplers will not be executed, this way you will be able to "skip" the unwanted samplers.
vars is a shorthand to JMeterVariables class instance, it provides read/write access to all JMeter Variables in scope.

Related

How to pass different CSV value for every occurance in Jmeter

I am trying to pass different CSV Value in sequential manner for every occurrence in Jmeter.
I applied
Loop count
Counter
Beanshell Sampler
Value from CSV
JMS Point to Point Request
With this i am able to pass different value for every occurrence for multiple users.
But my script fails when i run for multi user multiple iterations.
It is not picking up sequential value.
My beanshell sampler code-
String variablename=vars.get("variable");
String csvvalue=vars.get("valuefromcsv");
vars.put(variablename,csvvalue);
It doesn't seem to me that you need to use scripting at all, it's quite sufficient to set up CSV Data Set Config and:
Point it to CSV file
Set up "Sharing Mode" to be All Threads
This way each thread (virtual user) will be picking up the next value from the CSV file on each iteration.
Also be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting
You don't need to write the script for this. you can "Config Element-> CSV data set config" for the HTTP request. under CSV data set config page set the below values.
Variable names: give column names.
Delimiter : should be ,
Recycle of EOF: True
stop thread on EOF : false
Sharing mode : All threads.

In Jmeter, How can we use JSR223 Preprocessor to parse a CSV and filter on a particular column value to pass it as a variable to a sampler

I'm new to JMeter and trying my best learn various things in JMeter especially regarding the CSV data processing as the application which I am working needs loads of parameterized data and I just can't create 100's of CSV filed as per the requirement and instead use a single CSV and filter the values based on specific conditions.
However, I'm finding it very difficult to write the code for this particular operatoin.
Name|Identity|Date
"001, A"|"3409ADD9"|05-01-2020
"002, B"|"BA47D76A"|05-01-2020
"003, C"|"2BC92A2D"|05-02-2020
"004, D"|"AB9AEEBE"|05-23-2020
"005, E"|"09FF417D"|05-29-2020
Note: Here I'm using | to parse as my data will contain as part of the Name as shown.
As you can see, The data is grouped by Date value. I want to use this CSV file to pass the Identity and Name value based on currentDate.
For Ex, Current Date is May-01-2020, I want to pass only those records whose date is May-01-2020 and pass it as a variable to my samplers in a loop. Once we reach the end of the file (I mean values which don't have date May-01-2020 associated with, I want to start the loop again ) and repeat from first till the time I mention in the RunTime Controller.
I went through different questions and trying to find a solution as I couldn't write one for myself in Groovy, Hence asking for help.
As per many suggestions in different questions regarding the use of JSR223 preprocessor with Groovy instead of a bean shell, I would like to seek some guidance to solve this problem to move further.
Add setUp Thread Group to your Test Plan
Add JSR223 Sampler to the setUp Thread Group
Put the following code into "Script" area:
SampleResult.setIgnore()
def original = new File('/path/to/original.csv').readLines()
def current = original.findAll() { line -> line.contains(new Date().format('MM-dd-yyyy')) }
def newFile = new File('/path/to/new.csv')
newFile.withWriter { out ->
out.println(original.get(0))
current.each { line ->
out.println(line)
}
}
the above code will filter the original CSV file and write only those lines which contain the current date to the new CSV file
Configure CSV Data Set Config in the main Thread Group to use the "new.csv" file from step 3

How to write response data of 2 different independent threadgroup into 2 different sheet of excel in JMeter

There are 2 independent scenarios. I want to write the specific response data from each threadgroup but into 2 different excel sheet. Currently I am ble to write response data from first threadgroup to excel sheet1, how can I do the same for resonse data of second request to excel sheet2?
There is no such thing as "Sheet" in CSV files as they are plain text files.
If you want to distinguish Samplers coming from different Thread Groups you can add __threadGroupName() function as the prefix/postfix for the Samplers - this way you will get the name of the Thread Group added to the sampler label
__threadGroupName() function is available since JMeter 5.0

Read Excel data .csv file sequentially in Jmeter

I have a scenario to use data from each row for validation using a HTTP Request. have tried with the CSV config but it reads the first row only for the iteration.
I have a single iteration and all my samplers are in a single thread group. The data from csv file is retrieved sequentially only when i give the iterations to a value say 3 (each iteration each row is taken)
How to achieve on reading the csv file rows sequentially for single iteration ,where the thread group contains many HTTP request and i need the value from each row for each requests.
Kindly suggest me a solution
As per CSV Data Set Config documentation:
By default, the file is only opened once, and each thread will use a different line from the file. However the order in which lines are passed to threads depends on the order in which they execute, which may vary between iterations. Lines are read at the start of each test iteration. The file name and mode are resolved in the first iteration.
So implementing your scenario using CSV Data Set Config doesn't seem to be possible, I would recommend considering using JMeter functions instead such as:
__StringFromFile()
__CSVRead()
These functions read next line from the file each time they're called so you can use them instead of CSV Data Set config in each of the HTTP Request samplers. Check out Apache JMeter Functions - An Introduction to get familiarized with JMeter Functions concept.

Random selection from CSV file in Jmeter

I have a very large CSV file (8000+ items) of URLs that I'm reading with a CSV Data Set Config element. It is populating the path of an HTTP Request sampler and iterating through with a while controller.
This is fine except what I want is have each user (thread) to pick a random URL from the CSV URL list. What I don't want is each thread using CSV items sequentially.
I was able to achieve this with a Random Order Controller with multiple HTTP Request samplers , however 8000+ HTTP Samplers really bogged down jmeter to an unusable state. So this is why I put the HTTP Sampler URLs in the CSV file. It doesn't appear that I can use the Random Order Controller with the CSV file data however. So how can I achieve random CSV data item selection per thread?
There is another way to achieve this:
create a separate thread group
depending on what you want to achieve:
add a (random) loop count -> this will set a start offset for the thread group that does the work
add a loop count or forever and a timer and let it loop while the other thread group is running. This thread group will read a 'pseudo' random line
It's not really random, the file is still read sequentially, but your work thread makes jumps in the file. It worked for me ;-)
There's no random selection function when reading csv data. The reason is you would need to read the whole file into memory first to do this and that's a bad idea with a load test tool (any load test tool).
Other commercial tools solve this problem by automatically re-processing the data. In JMeter you can achieve the same manually by simply sorting the data using an arbitrary field. If you sort by, say Surname, then the result is effectively random distribution.
Note. If you ensure the default All Threads is set for the CSV Data Set Config then the data will be unique in the scope of the JMeter process.
The new Random CSV Data Set Config from BlazeMeter plugin should perfectly fit your needs.
As other answers have stated, the reason you're not able to select a line at random is because you would have to read the whole file into memory which is inefficient.
Rather than trying to get JMeter to handle this on the fly, why not just randomise the file order itself before you start the test?
A scripting language such as perl makes short work of this:
cat unrandom.csv | perl -MList::Util=shuffle -e 'print shuffle<STDIN>' > random.csv
For my case:
single column
small dataset
Non-changing CSV
I just discard using CSV and refer to https://stackoverflow.com/a/22042337/6463291 and use a Bean Preprocessor instead, something like this:
String[] query = new String[]{"csv_element1", "csv_element2", "csv_element3"};
Random random = new Random();
int i = random.nextInt(query.length);
vars.put("randomOption",query[i]);
Performance seems ok, if you got the same issue can try this out.
I am not sure if this will work, but I will anyways suggest it.
Why not divide your URLs in 100 different CSV files. Then in each thread you generate the random number and use that number to identify CSV file to read using __CSVRead function.
CSVRead">http://jmeter.apache.org/usermanual/functions.html#_CSVRead
Now the only part I am not sure if the __CSVRead function reopens the file every time or shares the same file handle across the threads.
You may want to try it. Please share your findings.
A much straight forward solution.
In CSV file, add another column (say B)
apply =RAND() function in the first cell of column B (say B1). This will create random float number.
Drag the cell (say B1) corner to apply for all the corresponding URLs
Sort column B.
your URL will be sorted randomly.
Delete column B.