Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMeter - Read Files from Directory

Tags:

jmeter

Using Apache JMeter

First question: I was able to read one single file (containing all the data) from a directory and use its data.

However now the requirement is to have a separate file for each data point, that means I will have to put many data files in a directory and then read the files off that directory. I have a set of data files, but I do not know how to read each of the files in a loop and send it to JMeter.

Second Question: the data we are talking about here is a JSON msg, and it's indented and multi-line, how to read a multi-line file into a single input variable? Again, I had no problem when the JSON msg is a single line.

like image 249
vamsi-vegi Avatar asked Sep 01 '15 00:09

vamsi-vegi


People also ask

How run XML file in JMeter?

FileServer; //Open the file FileInputStream fstream = new FileInputStream("C://QC//qa//Testlink//Jmeter//Expected//test.xml"); //Get the object of DataInputStream DataInputStream instream = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(instream));


1 Answers

  1. Use Beanshell Sampler to convert files list into a JMeter Variable:

    • Add a Beanshell Sampler to your test plan
    • Put the following code into the Sampler's "Script" area:

      File folder = new File("/path/to/your/folder");
      File[] files = folder.listFiles();
      int counter = 1;
      for (File file : files) {
          vars.put("file_" + counter, file.getAbsolutePath());
          counter++;
      }
      

      It will result into variables set like:

      file_1=/path/to/your/folder/file1.txt file_2=/path/to/your/folder/file2.txt etc.

  2. Add ForEach Controller after Beanshell Sampler and configure it as follows:

    • Input variable prefix: file
    • Start index for loop: 0
    • End index for loop: leave blank
    • Output variable name: anything meaningful, i.e. current_file
    • Check Add "_" before number
  3. Put your main sampler as a child of the ForEach Controller. Where required address each file contents using __FileToString() function as ${__FileToString(${current_file},,)}
like image 93
Dmitri T Avatar answered Sep 28 '22 01:09

Dmitri T