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:
- Create a simple JMeter test as given below.
- 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.
- 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)
- 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)
- 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)
- Add a ‘Debug Sampler’ under ‘Loop Controller’. Add ‘View Results Tree’ listener. Thats it!! We are done.
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.