This is one of our most popular blog posts – File Uploads. Check out a new section #3: Adding Parameters to your HTTP Request. Read below for lots of great information.
There are many cases of testing performance of API Endpoints for file uploads. We chose to demonstrate scaling out such a test building our load test plan in JMeter. JMeter supports file uploads natively within the HTTP Request, and Redline13 supports attaching files to your test plan. However, user uploads are random, so we need to generate files sized from 10,000 to 100,000 characters.
1. Load Plan
We wanted each load agent to simulate 60 users, each user loading 20 photos of random size.
2. Create HTTP Request Defaults and HTTP Request for File Uploads
The Request Default is your standard setup of Server Name.
Within the HTTP Request, we can configure our File Upload from variables.
In the ‘Send Files with Request’ section, we set
- File Path to be a variable ${filename}
- Parameter Name to the variable name the server side expects
3. Adding Parameters to your HTTP Request
In many cases, we need to send additional parameters alongside the file to be uploaded. In order to do this we need to click “Use multipart/form-data” option.
It is not mandatory to specify the “Content Type” but it is a good practice. Fill in the “MIME Type” field to specify what type of a file we are sending . In our example we are sending a simple text file and we write a “text/plain” on this field. If you were uploading an image with png format you would use “image/png” as the MIME Type.
4. Generate Random Files in Pre-Processor
To achieve this for each request, we used the ‘Bean Shell PreProcessor‘ with the following code snippet
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
// Create a temp file
File myFile = File.createTempFile("upload-", "" );
// Generate Random length string and write to file
FileUtils.writeStringToFile(myFile, RandomStringUtils.random( RandomUtils.nextInt( 1000, 100000 ) ), "UTF-8" );
// Store file name in variable.
vars.put( "filename", myFile.getCanonicalPath() );
5. Clean Up Files in Post Processor
Clean up is a pretty simple task.
import org.apache.commons.io.FileUtils;
// Delete file and do not throw error
FileUtils.deleteQuietly(new File( vars.get("filename")));
6. Example Test and JMX File Uploads
Here is an example 4 Server test executing almost 5,000 uploads under 5 minutes.
You can now download the JMX file and browse the results at File Upload Test Plan. With RedLine13, you can run a JMeter Load Test with your JMX script of any mobile application, web application, or API.
You can run your own test with file uploads on RedLine13 for free.