Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use csv file with ForEach controller in JMeter

Tags:

jmeter

I am doing teacher tool testing in JMeter. I have 30 number of entries in .csv file. I want to iterate first 10 entries through any logic controller for thread one and other 10 entries with same controller for second thread and this process should be repeated 3 number of threads. inside logic controller i have http sampler.

Thread Group (3)- csv config file- foreach controller(1-10)- http sampler-

repeat foreach loop controller(11-20)
like image 401
Rahul Avatar asked Aug 24 '14 12:08

Rahul


People also ask

How do I iterate through a CSV file in JMeter?

Define an child CSV Data Set Config , with the file path and the variable name url which stands for the column listing the urls. Use the ${url} with the http request handler to fire the request. To stop execution after the end of the CSV file is reached: within the CSV Data Set Config set Recycle on EOF?

What is ForEach controller in JMeter?

ForEach Controller in Jmeter iterates through an array of variables. In this JMeter tutorial, we'll use the ForEach Controller to loop through a JSON Array. There are times when we need to parse a response and extract certain information from it.

How do I select a random value from a CSV file in JMeter?

In CSV file, add another column (say B) apply =RAND() function in the first cell of column B (say B1). This will create random float number.

What is CSV data set config in JMeter?

The “CSV Data Set Config” enables using CSV files as an external data source, where you can keep unique user data like names, emails and passwords. With the help of this config element, JMeter is able to read the CSV file line by line, and then use split parameters to allocate different values for different threads.


1 Answers

As far as I understood your question you need the following:

  1. Read all values from CSV file
  2. For 1st thread iterate first 10 entries
  3. For 2nd thread iterate second 10 entries
  4. etc.

In order to implement this you'll need the following test plan structure:

  • Thread Group - to define virtual user threads, loops, ramp-up, etc.
    • Beanshell Sampler - to read CSV file into JMeter Variables
    • If Controller 1 (1st thread condition)
      • ForEach Controller 1
      • HTTP Request
    • If Controller 2 (2nd thread condition)
      • ForEach Controller 2
      • HTTP Request
    • etc.

In regards to detailed configuration

Beanshell Sampler

Put the following code into Beanshell Sampler Script area:

BufferedReader br = new BufferedReader(new FileReader("/path/to/your/file.csv"));
String line;
int counter = 1;
while ((line = br.readLine()) != null) {
   vars.put("VAR_" + counter, line);
   counter++;
}
br.close();

The code above will read all the lines from file.csv and store it into JMeter Variables like:

VAR_1=first line of your CSV file
VAR_2=second line of your CSV file
VAR_3=....

If Controller 1

If you want anything under this controller to be applicable for 1st user only set the following condition:

${__threadNum}==1

ForEach Controller 1

If you want 1st thread to read first 10 lines from CSV file configure your ForEach Controller 1 as follows:

  • Input variable prefix: VAR
  • Start index for loop: 0
  • End index for loop: 10
  • Output variable name: anything meaningful, i.e. CURRENT_VAR

HTTP Request 1

Refer current line as ${CURRENT_VAR} where required.

Hope it's clear enough.

References and how-to's :

  • Beanshell
  • If Controller
  • ForEach Controller
  • __threadNum function
like image 181
Dmitri T Avatar answered Nov 25 '22 04:11

Dmitri T