Site icon Vinsguru

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

& file2.csv contains

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:

 

 

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.

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:

Exit mobile version