Using CSV file to read test data from
Asked Answered
R

2

14

I need to test a various links of a site (no need to login) with 100's of users and loop it for some number of times using JMeter. I want to put those links in a "CSV file", so that all the links to be tested are read from file.

How do I accomplish this task?

Roxie answered 23/1, 2013 at 9:37 Comment(2)
Why not to try first to search around in jmeter tag? Asked many times here.Dennett
Related: sqa.stackexchange.com/q/5577/11978Leatherneck
D
22

Prepare kind of csv-file with list of your test-params and use it to parametrize your test-samplers, using at least the following:

  1. CSV Data Set Config

    Look into the following links for details:

    How to get Jmeter to use CSV data for GET parameters?
    Use jmeter to test multiple Websites
    use csv parameters in jmeter httprequest path
    Force a thread to use same input line when using CSV Data Set Config

  2. Jmeter functions:

  3. Variables From CSV sampler from jmeter-plugins.


1. Prepare your test-urls in csv-file, e.g. in the following format:

    url1
    url2
    ...
    urlN

Ensure that test-URLs don't contain http:// prefix (as per HTTP Request params -> Server).

2. Use schema for your script as below:

    CSV Data Set Config:
    Filename: [path to your csv-file with test-urls]
    Variable Names: testURL
    Recycle on EOF?: True
    Stop thread on EOF?: False
    Sharing mode: Current thread

    Thread Group:
    Number of Threads: N
    Loop Count: M
            HTTP Request // your http call
            Server Name or IP: ${testURL} // use variable with extracted URL

This will start N users, each users will read M entries from list of test-urls. If M > number of entries in list of test-urls then user will recycle the list on EOF.

Dennett answered 23/1, 2013 at 10:24 Comment(8)
Thanks, But the problem that I have been facing is that the test ends after reading all the requests in the csv file. I am not being able to run it for multiple threads or even run it in a loop. Could you help with it.Roxie
Well, set "Recycle on EOF?" and "Stop thread on EOF?" as well as "Sharing mode" options of CSV Data Set Config correspondingly. If you want read both users credentials and test link from csv-file you have to set 2 CSV Data Set Config instances: one to read users credentials and assign they to threads, another one - to read test links to use along with each thread.Dennett
Well, update your answer first and specify your problem more clearly, provide your test script schema.Dennett
I am just reading the test link from the CSV file. All, i want to do is to test the links in the CSV file with multiple threads/users(BY mentioning the number of threads in the thread group and mentioning the number of loops) I have tried it accomplish this test , but the major problem is the settings of "EOF" in the CSV Data set config. If the "Recycle on EOF" is set to False then the test stops on a single thread. And, If "Recycle on EOF ?: is set to True" then the test will run for infinite number of times. So, can you please advice me on how to handle this?Roxie
Well, I especially posted above 2 links to the working solutions (this and this) that are almost what you want but seems you haven't even tried to look into.Dennett
I've updated answer above with working schema but I really don't like your "give me the code" approach.Dennett
I am sorry my comment sounded that way. Actually, i was expecting the test to work a bit differently than the way it actually works. I was expecting that I would have 32 request, if I have a file with 4 urls, and then set the Threads to 2 and loop count to 4. But, that did not happen with many script I tried. The above schema works well, when the number of urls in the file matches the number of loops(or in multiple of the no of urls). i.e it will give 8 request testing each of the url two times with the setting mentioned previously. Thank you so much for advice. Sorry, once again.Roxie
To get construction I've described (2 threads x 4 loops @ 4 urls in list -> 32 requests) you could possibly use schema from here: https://mcmap.net/q/829376/-use-jmeter-to-test-multiple-websites. I.e. use CSV DataSet Config under While Controller with condition until string from csv is not EoF, Recycle on EOF? = False, Stop thread on EOF? = True, Sharing mode = Current thread group, - this will iterate all the urls list each loop for each thread. Configure Thread Group as previously (N threads, M loops).Dennett
F
1

In one of the comments, it's mentioned that you can't read the CSV more than once per loop. You can go and have multiple threads, each reading the CSV file once, but then the file is close and won't be read on the next loop. Also, if you set the CSV to recycle, then CSV file is read over and over again indefinitely. So the question becomes how do you loop a CSV file a certain number of times as opposed to indefinitely?

I posted my answer to that in another post (https://mcmap.net/q/829379/-jmeter-how-to-reuse-reopen-a-csv-in-several-thread-group-loops), but I'll copy & paste it incase that link doesn't work in the future.


I couldn't find a simple solution to this. I ended up using beanshell scripts, which let you use code very similar to java to do some custom stuff. I made an example JMeter project to demonstrate how to do this (yes it's ridiculously complicated, considering all I want to do is repeat the CSV read):


  1. Files:

my file structure:

JMeterExample
|
⊢--JMeterTests.jmx             // the JMeter file
⊢--example.csv                 // the CSV file

contents of my CSV:

guest-id-1,"123 fake street",
guest-id-2,"456 fake street",
guest-id-3,"789 fake street",

so in this thread group, I'm going to just have 1 user, and I'll loop 2 times. I intend to send 1 request per CSV line. So there should be 6 requests sent total.

  1. Thread Group

enter image description here


  1. User Defined Variables

This is kind of optional, but the filepath is subject to change, and I don't like changing my scripts just for a change in configuration. So I store the CSV filename in a "User Defined Variables" node.

If you are storing the CSV file in the same directory as your JMeter test, you can just specify the filename only.

If you are saving the CSV in a folder other than the directory containing your JMeter file, you will need to supply an absolute path, and then slightly modify the beanshell script below: you'll need to comment out the line that loads the file relatively, and comment in the line that loads from an absolute path.

enter image description here


  1. BeanShell Sampler to parse and store CSV lines

Add a Beanshell Sampler which will basically take in a path, and parse & store each line as a variable. The first line will be stored as a variable called csv_line_0, the 2nd line will be csv_line_1 and so on. I know it's not a clean solution but... I can't find any clean simple way of doing this clean simple task. I copied and pasted my code below.

enter image description here

import org.apache.jmeter.services.FileServer;
import java.text.*;
import java.io.*;
import java.util.*;

String temp = null;

ArrayList lines = new ArrayList();

BufferedReader bufRdr;

ArrayList strList = new ArrayList();     

// get the file
try {
    // you can use this line below if your csvFilePath is an absolute path
    // File file = new File(${csvFilePath});

    // you can use this line below if your csvFilepath is a relative path, relative to where you saved this JMeter file
    File file = new File(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir() + "/" + ${csvFilePath});

    if (!file.exists()) {
        throw new Exception ("ERROR: file " + filename + " not found");
    }

    bufRdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
} catch(Exception e){
    log.error("failed to load file");
    log.error(e.getMessage());
    return;
}

// For each CSV line, save it to a variable
int counter = 0;
while(true){
    try{
        temp = bufRdr.readLine();
        if(temp == null || temp.equals("<EOF>")){
            break;
         }
         lines.add(temp);
         vars.put("csv_line_" + String.valueOf(counter), temp);
        counter++;

        

    } catch(Exception e){
        log.error("failed to get next line");
        log.error(e.getMessage());
        break;
    }
}

// store the number of CSV lines there are for the loop counter
vars.put("linesCount", String.valueOf(lines.size()));

  1. Loop Controller

Add a Loop Controller that loops once for each CSV line. ${linesCount} is a count of the number of CSV lines and is calculated from the above beanShell script.

enter image description here


  1. Beanshell script to extract data from current CSV Line

This script will run once per CSV line. It will go and grab the current line, and parse out whatever data is on it. You'll have to modify this script to get the data you want. In my example, I only had 2 columns, where column 1 is a "guestId", and column 2 is an "address".

__jm__loopController__idx is a variable JMeter defines for you, and is the index of the loop controller. The variable name is __jm__{loop controller name}__idx.

enter image description here

String index = vars.get("__jm__loopController__idx");
String line = vars.get("csv_line_" + index);
String [] tokens = line.split(",");
vars.put("guestId", tokens[0]);
vars.put("address", tokens[1]);

  1. Http request sampler

Here's the HTTP request that's using the data extracted.

enter image description here


  1. result

When running this, as desired, I end up sending 6 http requests over to the endpoint I defined.

enter image description here

Failure answered 27/9, 2020 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.