Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.

Learn JMeter in 5 Hours

Start Learning
Slack

Test Your Website Performance NOW!

arrow Please enter a valid URL

Performance Testing: Upload and Download Scenarios with Apache JMeter

In some performance testing use cases it is not enough to just check how long it takes for a server to render a page, produce a set of search results or create an entity. For certain application types – let’s take enterprise content management systems as an example – the performance of file upload and download requests is vital and must be tested. In this article we'll describe how File Upload and Download scenarios can be implemented using JMeter, highlight the areas which may be trickier in this respect and suggest some best practices.

 

File Upload

 

HTML Form-based File Upload is described in RFC-1867 which identifies “multipart/form-data” as a media type for file attachments. In turn, JMeter's HTTP Request Sampler offers a“Use multipart/form-data for POST” checkbox. When the checkbox is ticked it indicates that the request contains file data. The file(s) need(s) to be supplied through the “Send Files With the Request” input.

 

The HTML source upload form will look as follows:

 

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

 

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

 

    <input type="file" name="dataFile" id="fileChooser"/><br/><br/>

 

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

 

</form>

 

Mention “dataFile” in the name of “file” input. It has to be the parameter which needs to be passed as “Parameter Name”.

 

So the HTTP Request should look as follows:

 

 

Where

  • 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 is to use JMeter's “Superpower”: The HTTP Proxy Server. JMeter is smart enough to detect and populate values during recording process and you should take advantage of this capability.

 

Now the tricky parts:

 

File Path.

 

It is recommended to use full path, since if you provide a relative path such as file name only, JMeter will look it up under the FileServer base folder. To see whuch folder is being looked into you can check the jmeter.log file. Search for lines which contain “FileServer”.

 

2014/03/05 15:52:37 INFO  - jmeter.services.FileServer: Default base='/home/jmeter'

 

2014/03/05 15:52:37 INFO  - jmeter.services.FileServer: Set new base='/home/jmeter'

 

This value can be get or set using Beanshell as follows:

 

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

 

 

But again:

  • If you're running tests locally – better use the full paths

  • If you're uncertain regarding where to put files – get or set FileServer base

  • If you're using Blazemeter – no need to worry, we're here to make everyone’s lives easier, any file uploaded to the File Upload Area will be detected and placed into a relevant location, requiring only a file name for CSV Data Set Config, HTTP Multipart Upload Request, etc.

 

Parameter Name.

 

The Parameter name must be carefully specified. Otherwise your file will be going nowhere.  In order to determine the correct value do one of the following:

 

  • Inspect page source and get “name” attribute of “file” type input.

  • Or let JMeter do it for you via the HTTP(S) Test Script Recorder.

 

MIME Type

 

When provided, the MIME Type is being used as “Content-Type” request header value. From a “realistic test” point of view it's important to provide correct content MIME types as an application or web server may behave differently depending on the content type stated, producing an error in case of mismatch. JMeter's HTTP Proxy server is smart enough to guess the MIME type of the file sent, however if you are uncertain and/or sending files sequentially or in parallel from a test file store you can use the Beanshell Pre Processor to detect the file’s MIME Type as follows:

 

import java.nio.file.Files;

 

import java.nio.file.Paths;

 

String mimeType = Files.probeContentType(Paths.get("/path/to/your/fileextension")); // 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 Request in multipart/form-data mode respects all cookies and headers like any other HTTP Sampler.  Pre and Post processors can be used to generate or save dynamic data and Multipart request details can be seen in View Results Tree listener as for any GET/POST/PUT/etc. request. Just don't forget to disable or delete it when running your real performance test.

 

File Download

 

The File Download action from the JMeter perspective is basically just a HTTP GET request. When a file is being used in the request path it means that a download request is issued and being served and related performance metrics can be reported.

 

If your use case assumes checking file integrity, for example when it doesn't live at the server and is being auto-generated or you need to compare downloaded files’ content with another file, you probably want to save the response. JMeter offers a Save Responses to a file Listener component.

 

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 the file download.

 

If you want total control which isn't achievable with the above options there is always a possibility to use scripting to manipulate response data.

 

For the Beanshell Post Processor “data” pre-defined variable is available. It is a  byte array which contains server response for the parent sampler.

 

For JSR223 or BSF Post Processors a “prev” pre-defined variable exists. It stands for SampleResult class instance and exposes:

 

 

All the entities above can be written to files, stored as JMeter Variables, used in Assertions, etc.

 

End-to-End

 

For proof of concept we'll use an Amazon micro instance running Tomcat with a deployed simple Upload Servlet, storing files under /data folder. We’ll hit it with the  Blazemeter engine to perform upload file action followed by downloading the same file.

 

Using the JMeter GUI, create upload/download scenario as described above.

 

Upload it to Blazemeter “Files” area along with file(s) to be tested. Configure test properties like number of threads, iterations, rampup period, etc.

 

 

Set additional dynamic test properties like host and port.

 

 

And once the test is finished check out the load report:

 

                

Summary

 

The check list for a successful implementation of File Upload and Download scenarios is:

 

  • Check that “Use multipart/form data for POST” is ticked

  • Check that files for upload actually exist in their relative location or use the full paths

  • Provide the correct MIME Type

  •  Save Downloaded files using “Save responses to a file” listener or a script.

 

Want to learn more?

Check out our free Webinar "Ask the Expert - What's new in JMeter 3.0?"

     
arrow Please enter a valid URL

Interested in writing for our Blog?Send us a pitch!

We're working on starting your first test

Testing 20 Virtual Users

Starting your test in around 2-4 minutes. Your report will appear once we've gathered the data.

0

Status: Preparing Your Test

Your email is required to complete the test. If you proceed, your test will be aborted.