JMeter – Looping 2 CSV files

This article outlines reading 2 CSV files in JMeter. ie, For each row of outer CSV file, we need to read all the rows of inner CSV file.

Scenario:

I have 2 CSV files. Lets assume I have named them as file1.csv and file2.csv respectively.

file1.csv contains

csvfile1

& file2.csv contains

csvfile2

So what I am trying to achieve here is something like as given below

for( each row of CSV file 1 )
{
    for( each row of CSV file 2 )
    {
        //do something
    }
}

We can get this done by using 1 CSV Data Set Config as well if we can merge CSV files programmatically.
But I was curious how it can be done in JMeter using 2 CSV Data Set Config (because I get 2 files often for my requirement – I do not want to merge them every time).

Steps:

  • Create a simple JMeter test as given below.

 csv1

  • Thread Group is set to run ‘For Ever’. This is fine as CSV Data Set config will stop the test automatically once we read all the rows.

csv1a

  • CSV Data Set Config – File 1 Settings – should be done as given below.
    • Recyle on EOF?‘ -> FALSE (As we are going to read each row only once for outer CSV file)
    • Stop thread on EOF?‘ – TRUE (Yes, as we are going to stop JMeter once we have read all the rows of outer CSV file)

csv2

  • Loop Controller – This is like the inner for loop. So, Loop Count should be equal to no of rows present in the file2.csv.
    (for the time being lets hard code this as 10 (no of rows present file2.csv) – We will see how to get this line count using beanshell)

csv3

  • CSV Data Set Config – File 2 Settings – should be done as given below.
    • Recyle on EOF?‘ -> TRUE (As we are going to read all the rows again and again for each row of outer CSV file)
    • Stop thread on EOF?‘ – FALSE (We do not want to stop the test once we have read all the rows of file2.csv. CSV Data Set Config – File 1 – settings will take care of stopping the test)

 csv4a

  • Add a ‘Debug Sampler’ under ‘Loop Controller’. Add ‘View Results Tree’ listener. Thats it!! We are done.

csv5a

Output:

If we run our JMeter test, we get below output beautifully – for each row outer csv file – all the rows of inner csv file gets executed.

csv6

Updating Loop count programmatically:

We had hardcoded the Loop Count of the Loop Controller.  To parameterize this, We can either pass the line count as the property to the JMeter test via command line argument Or we can add a Beanshell Sampler in a ‘Setup Thread Group’.  In Beanshell Sampler, Lets add below code to get the line count.


import java.io.File;
import java.io.FileReader;
import java.io.LineNumberReader;

FileReader fileReader = new FileReader(new File("C:\\workspace\\file2.csv"));
LineNumberReader lineReader = new LineNumberReader(fileReader);
int linenumber = 0;
while (lineReader.readLine() != null){
   linenumber++;
}
linenumber--; // do not include header row
props.put("LineCount", Integer.toString(linenumber));
lineReader.close();

Now instead of using hard-coded 10, Lets use ${__P(LineCount)} to get the line count of file2.csv programmatically.


Share This:

6 thoughts on “JMeter – Looping 2 CSV files

  1. I was looking for a way to use three csv files. I followed your example, added one more Loop controller and it works exactly as I wanted thanks to your detailed explanation. Thanks a lot 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.