Deleting a particular CSV file using Jmeter

You might come across situations where you will have to delete a document before running a process. Say you have a situation where you have to create a document with all the data in that is needed for the particular test. Every time you run the test you would need create new documents. For that case you would need to delete the previously created document. If that is the case how do you delete the document. Following is the way to do so.

  1. Initially Add a BeanShell PreProcessor in the start of the Thread if you need to Delete the document in the beginning of the Thread.Screen Shot 2016-04-11 at 11.24.58Screen Shot 2016-04-11 at 11.26.02
  2. Once you have added the Beanshell PreProcessor, now you can add the following script. to delete the document.Screen Shot 2016-04-11 at 11.47.52
    import java.text.*;
    import java.util.*;
    downloadDoc = "download.csv";
    csvDir = vars.get("fileLocation");
    File file1 = new File( csvDir + downloadDoc);
    if (file1.exists()) {

Now that the the Delete script is ready to be run and now you can delete the document you want to delete from the specific directory.


Randomly selecting values from .csv file for Jmeter Tests im on the

There might be instances where you will have to grab dynamic values from .csv file to run your Jmeter Performance tests. You can not use the CSV Data Config as the point here to select values randomly from the .csv file

The following script in a BeanShell PreProcessor will help to randomly select values from the CSV file that has the values. It randomly selects each row from the the CSV you can later break down the row as you prefer, in case a row has multiple values.

import java.text.*;
import java.util.*;

String [] params = Parameters.split(“,”);
csvTest = “download.csv”;
csvDir = vars.get(“fileLocation”);
ArrayList strList = new ArrayList();

try {
File file = new File( csvDir + csvTest);

if (!file.exists()) {
throw new Exception (“ERROR: file ” + csvTest + ” not found in ” + csvDir + ” directory.”);

BufferedReader bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null) {


Random rnd = new java.util.Random();
//String NodeId= strList.get(rnd.nextInt(strList.size()));
//System.err.println(“Parts jjj “+q);
//String[] parts = q.split(“,”);

//System.err.println(“Parts 0 “+parts[0]);
//System.err.println(“NodeName “+parts[0]);
System.err.println(“NodeId “+NodeId);

//String correctName = parts[0].replace(“%20″,””);
catch (Exception ex) {
IsSuccess = false;
catch (Throwable thex) {

Screen Shot 2016-04-08 at 11.14.15 AM (2).png

You can now use this scripts and randomly select values from CSV file when running your Jmeter Tests.

Uploading same document by renaming with Jmeter

Most of the time when we do performance testing we would require heavy data uploaded in the the system. When creating the data we would face issues where we might not be able to upload documents with duplicate names. How do we cater to this need.

The solution is quite simple. Rename the document every time you try to upload the document in that way the same document can be uploaded with different names.

  1. Create a Thread Group within a Test Plan. (You can name Thread Group as you wish)Screen Shot 2016-04-07 at 10.41.14
  2. Add a Loop Controller within the Thread Plan, and mention total amount of documents you would like to upload in the system with in the Loop Count as shown below.Screen Shot 2016-04-07 at 10.54.36
  3. Add two Counters one would have your the minimum value as the Start and the maximum value as Maximum. The other would have have the maximum value plus one as Maximum and minimum value as the Start. Mention the preferred Number Format and Increment. Reference Names for Counter1 and Counter2 should be given, as these names will be used for renaming.Screen Shot 2016-04-07 at 11.25.36   Screen Shot 2016-04-07 at 11.25.56
  4. Add the HTTP Request in the file data mention the loop_counter1 infront of the name of the document as shown below.Screen Shot 2016-04-07 at 12.11.48.pngMake sure that you have the initial or the minimum number mentioned in the name of the document in the Directory. Here the initial number we use 123000 as the Number Format is 123000.Screen Shot 2016-04-07 at 12.16.22.png
  5. Now Create the Script as shown below.Screen Shot 2016-04-07 at 12.31.19import java.text.*;
    import java.util.*;
    //Take the fileLocation from the user defined variable
    fileLocation = vars.get(“fileLocation”);
    //Take the counter 1 value to indexNum
    indexNum = vars.get(“loop_counter”);
    //Take the counter 1 value to indexNum1
    indexNum1 = vars.get(“loop_counter1”);
    //The string portrays the current location of the document (with current name)
    mainFile = indexNum + “Download.ppt”;
    //The string portrays the location of the document which is renamed.
    fileName = indexNum1 + “Download.ppt”;
    //Get current document location
    File file = new File( fileLocation + mainFile);
    //Create location for renamed location
    File file2 = new File( fileLocation + fileName);
    //Rename document with the new location
     boolean success = file.renameTo(file2);
  6. Now you will be able to upload same document with different with the amount you need.
  7. Finally if you would like to rename the thread to the initial name you can run a BeanShell Sampler in the tear down Thread Group do the same scripting and rename the document.Screen Shot 2016-04-07 at 13.14.29.png