简体   繁体   中英

How to read multiple csv files dynamically with jmeter

I'm trying to read multiple csv files from a specific folder using Jmeter

Until now I read one csv file and I could loop it with a thread that runned X times when X- is the number of lines in the file. I was counting the number of lines in the csv file before the thread with a bean_shell like that:

    import org.apache.jmeter.services.FileServer;
String DirPath = FileServer.getFileServer().getBaseDir();
FileReader fileReader = new FileReader(new File(DirPath + "/files/App1.csv"));
LineNumberReader lineReader = new LineNumberReader(fileReader);
int linenumber = 0;
while (lineReader.readLine() != null){
linenumber++;
}
props.put("rowsnumberApp", Integer.toString(linenumber));

Now, I want to read multiple files from a folder, when the amount of files in the folder can dynamically change,

The files names will be like that:

app1

app2

app3

app...

I have this code to read all files from the folder:

import org.apache.jmeter.services.FileServer;
String DirPath = FileServer.getFileServer().getBaseDir();
File folder = new File(DirPath + "/files");
File[] files = folder.listFiles();
int counter = 1;
for (File file : files) {
    vars.put("file_" + counter, file.getAbsolutePath());
    counter++;
}

Each file has different amount of lines (but at least I have the same soap-request for all of them)

I realized that using a loop controller on csv gives only the first line.

And as you can see I already have the code part for reading files from a folder and counting the number of lines,

I just need a way to connect all of this to be able to loop for all csv files in the folder

so any idea?

Using 2 Loops, first save how many files by:

vars.put("fileLength", files.length);

Create first Loop with ${fileLength} as loop count

Add Counter start with 0 Maximum ${fileLength} Increment 1 Reference Name cnt

Inside loop read as your first beanshell change to use counter

FileReader fileReader = new FileReader(new File(DirPath + "/files/file_" + vars.get("cnt")));

Update your current implementation with the counter:

import org.apache.jmeter.services.FileServer;
String currentFileIndex = vars.get("cnt");
String DirPath = FileServer.getFileServer().getBaseDir();
FileReader fileReader = new FileReader(new File(DirPath + "/files/file_" + currentFileIndex ));
LineNumberReader lineReader = new LineNumberReader(fileReader);
int linenumber = 0;
while (lineReader.readLine() != null){
linenumber++;
}
props.put("rowsnumberApp" + currentFileIndex , Integer.toString(linenumber));

Directory Listing Config plugin would be the easiest option to proceed

目录列表配置示例

This way you will get a single JMeter Variable which you will be able to use in the CSV Data Set Config to parameterize your API calls

See Introducing the Directory Listing Config Plugin on JMeter for more details.

You can install the Directory Listing Config Plugin using JMeter Plugins Manager

JMeter目录列表配置

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM