JMeter Performance Testing: Upload and Download Scenarios
May 17, 2022

JMeter Performance Testing: Upload and Download Scenarios

Open Source Automation
Performance Testing

In some JMeter performance testing use cases, it is not enough just to check the time it takes for a server to render a page, produce a search result set, or create an entity. For certain application types, such as enterprise content management systems, the performance of file upload and download requests is vital. 

In this article, we will describe how to do JMeter performance testing by implementing File Upload and Download scenarios. We will also highlight areas that are known to be problematic and suggest some best practices. 

Table of Contents:

What is JMeter Performance Testing?

JMeter performance testing is the process of testing the performance of a web application using JMeter. As open-source, Java-based software, JMeter can also be used for functional testing and load testing.

Why is JMeter Testing Important

There are many benefits to testing with JMeter, including:

  • The ability to test different application types. JMeter can support performance testing for web applications, web services, shell scripts, databases, and more.
  • Platform independence. JMeter can run on multiple platforms since it is purely Java-based.
  • Open source. Not only is there zero licensing cost to start, but developers can also customize the source code since JMeter is open source.

How to Do JMeter Performance Testing

File Upload

Now that we have given a brief overview of JMeter performance testing, let us dive into uploading files to JMeter. 

A form-based File Upload in HTML is described in RFC-1867, which identifies “multipart/form-data” media type for file attachments. JMeter's HTTP Request Sampler in its turn offers a “Use multipart/form-data for POST” checkbox. Ticking this checkbox indicates that the request contains file data. The file(s) needs to be supplied through “Files Upload”' input. 

In HTML, the source upload form looks like this:

method="post" action="UploadServlet" enctype="multipart/form-data">


    Welcome to Blazemeter upload test servlet, select file to upload:

    type="file" name="dataFile" id="fileChooser"/>


    type="submit" value="Upload" />

It is critical to mention “dataFile” as the name of the “file” input, since this must be the parameter that needs to be passed as “Parameter Name”. 

The HTTP Request should look like the following:

HTTP request for JMeter Performance Testing

Pay attention to the following parameters:

  • File Path: should be the location of the file to upload.

  • Parameter Name: relevant form of “file” type name.

  • MIME Type: the file's Internet media type.

The easiest way to get these fields populated for you is using JMeter's Superpower: The HTTP Proxy Server. JMeter is smart enough to detect and populate values during the recording process. Just make sure to copy the file(s) you will be uploading to your JMeter installation’s “bin” folder before uploading the file in the browser, as this is the only way that JMeter can properly capture the request. 

File Path

 There are two options for providing a file path:

  1. Full path, i.e. c:\somefolder\myFile.txt
  2. Relative path, i.e. myFile.txt only 
     

When using the full path, JMeter will upload the given file as it is. 

When providing a relative path such as file name only, JMeter will look for it in the FileServer base folder. You can check the jmeter.log file to see which folder is being investigated by searching for lines that contain “FileServer.”

2022-05-01 09:36:59,585 INFO o.a.j.s.FileServer: Default base='C:\jmeter\bin'

You can get or set the value using any suitable JSR223 Test Element with Groovy language, such as:

import org.apache.jmeter.services.FileServer;

FileServer fs = FileServer.getFileServer(); //instantiate FileServer class

log.info(fs.getBaseDir()); // get FileServer base dir and write it to jmeter.log

fs.setBasedir("/path/to/test/files/location"); // set a new base dir
Setting file path with JSR223

Yet it is important to consider the following when choosing a file path:

  • It is preferable to use full paths when running tests locally. Relative paths may differ depending on where the .jmx file is and/or how you launch JMeter.
  • If you are uncertain regarding where to put files, it is recommended to set up a FileServer base.

BlazeMeter also simplifies the file path process by automatically detecting any files uploaded with a test script and placing them in a relevant location. You would only need a file name for a CSV Data Set Config or an HTTP Multipart Upload Request. 

Parameter Name

To make sure your file is going somewhere, it is important to specify the parameter name. There are two ways to determine the correct value: 

  • Inspect the page source and get the “name” attribute of the “file” type input. 
  • Let JMeter do it for you via the HTTP(S) Test Script Recorder. 

MIME Type

When provided, a MIME type is used as a “Content-Type” request header value. It is important to provide the correct content MIME type, since the application or web server may behave differently based on what content type is being stated and produce an error in case of a mismatch. 

JMeter's HTTP Proxy server is typically smart enough to guess the MIME type of file being sent. Yet if you are sending files sequentially or in parallel from a test file store you can use a JSR223 Pre-Processor to detect the file MIME Type:

import java.nio.file.Files;

import java.nio.file.Paths;

String mimeType = Files.probeContentType(Paths.get("/path/to/your/file.extension")); // detect file's MIME Type

vars.put("mimeType", mimeType); // store file's MIME Type into "mimeType" variable

After that, you can just type ${mimeType} into the corresponding HTTP Request input.

Headers, Cookies, Pre/Post Processors, and Listeners

HTTP requests in multipart/form-data mode respect all cookies and headers, just like any other HTTP Sampler.  Teams can use pre and post-processors to generate or save dynamic data, as well as see multipart request details in the View Results Tree listener as for any GET/POST/PUT or other requests. Just don't forget to disable or delete these processors when running your real performance test. 

File Download

The File Download action from JMeter perspective is essentially an HTTP GET request. When using a file in the request path it means that the download request is issued and being served. Plus, teams can see reports on related performance metrics. 

As an example, you can save a response if your use case assumes checking file integrity after being auto-generated or when comparing the content of a downloaded file to another expected file. JMeter offers a Save Responses to a file Listener component to help with this task.
 

Attribute

Description

Required

Name

Descriptive name for this element that is shown in the tree.

No

Filename Prefix

Prefix for the generated file names; this can include a directory name. Relative paths are resolved relative to the current working directory (which defaults to the bin/ directory). JMeter versions post  2.4 also support paths relative to the directory containing the current test plan (JMX file). If the path name begins with "~/" (or whatever is in the jmeter.save.saveservice.base_prefix JMeter property), then the path is assumed to be relative to the JMX file location.

Yes

Variable Name

Name of a variable in which to save the generated file name (so it can be used later in the test plan). If there are sub-samples then a numeric suffix is added to the variable name. E.g. if the variable name is FILENAME, then the parent sample file name is saved in the variable FILENAME, and the filenames for the child samplers are saved in FILENAME1, FILENAME2 etc.

No

Save Failed Responses only

If selected, then only failed responses are saved

Yes

Save Successful Responses only

If selected, then only successful responses are saved

Yes

Don't add number to prefix

If selected, then no number is added to the prefix. If you select this option, make sure that the prefix is unique or the file may be overwritten.

Yes

Don't add suffix

If selected, then no suffix is added. If you select this option, make sure that the prefix is unique or the file may be overwritten.

Yes

 

This listener can be placed anywhere in the Test Plan. However, for a Download Scenario we recommend making it a child of the HTTP Request which performs file download so it will be applied only to this specific HTTP Request sampler. 

It is also possible to use scripting to manipulate response data if you want total control of the File Download action, which is not achievable with the above options.

In the JSR223 Post-Processors, Assertions and Listeners there is a prev pre-defined variable . It stands for previous SampleResult class instance and exposes the following functions: 

The above entities can be written to files, stored as JMeter variables, and used in assertions. 

End-to-End JMeter Performance Testing With BlazeMeter

For a proof of concept, let us use an Amazon micro-instance running Tomcat with a deployed simple Upload Servlet which stores files under /data folder to hit it with BlazeMeter engine to perform upload file action followed by downloading the same file. 

Using JMeter GUI to create an upload/download scenario as described above, upload the file to BlazeMeter and configure test properties such as number of threads, iterations, ramp up period, etc. 

You can also set additional dynamic test properties like host and port.

Creating upload/download scenario during JMeter performance testing

Override JMeter properties like host and port

 

Once the test is finished, you can see the summary report:

BlazeMeter summary report for JMeter Performance Testing

You can also request a statistical view and see a timeline report.
 
 

BlazeMeter reporting statistical view

BlazeMeter reporting timeline view

Bottom Line

The checklist for successful implementation of File Upload and Download scenarios would be:

  • Check that “Use multipart/form data for POST” is ticked
  • Check that files assumed for upload do exist in relative location or use full paths 
  • Provide correct MIME Type
  • Save Downloaded files using “Save responses to a file” listener or a script. 

START TESTING NOW

 

Related Resources