Uploading a document from same source document with jMeter.

This is another script that you can use to upload the same document to your system using jMeter. This is another way of doing as I have mentioned here.

import java.text.*;
import java.io.*;
import java.lang.*;
import org.apache.commons.io.FileUtils;

//Take the fileLocation from the user defined variable
//fileLocation = “/Users/cthalayasingam/Documents/jmeterTest/PerformanceTests/”;
fileLocation = vars.get(“fileCreatedPath”);
// The string portrays the current location of the document (Source file)
mainFile = “images.png”;
//The string portrays the location of the document which is renamed.
System.out.println(System.currentTimeMillis());
start = System.currentTimeMillis();
fileName = start + “images.png”;
//Save the name of the document with the time so that can be DELETED later
vars.put(“fileName1”, fileName);
//Get current document location
File file = new File( fileLocation + mainFile);
//Create location for renamed location
File file2 = new File( fileLocation + fileName);
System.out.println(fileName);
//Copy the file to the document to the new location
FileUtils.copyFile(file, file2);

After you have entered the above script in the Bean Shell pre processor. Then put the script in the delete script found below in a BeanShell Post Processor, that applies the delete for the document you have copied to the new destination.

import java.text.*;
import java.io.*;
import java.util.*;
import java.lang.*;

csvDir = vars.get(“fileCreatedPath”);
fileName2 = vars.get(“fileName1”);
File file1 = new File( csvDir + fileName2);
if (file1.exists()) {
file1.delete();
}

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.io.*;
    import java.util.*;
    downloadDoc = "download.csv";
    csvDir = vars.get("fileLocation");
    File file1 = new File( csvDir + downloadDoc);
    if (file1.exists()) {
    file1.delete(); 
    }

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.

 

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.io.*;
    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