Skip to content
A complete look at JMeter's FTP and SSH SFTP samplers

A complete look at JMeter's FTP and SSH SFTP samplers

While the File Transfer Protocol is one of the original protocols used in the early adoption of the internet it remains a fundamental part of modern computer networks.

In this post we will look at the way the JMeter can support you in performance testing FTP and SFTP.

SFTP is a more recent development to provide a layer of security over the original protocol to make it more suitable for the modern internet and is different to FPT in many ways.

Whilst SFTP is considered more appropriate for modern systems there are still many applications that rely on, and implement, FTP and therefore we will look at how we can test both.

If you want to follow along, a JMX file containing all the steps detailed below can be downloaded here.

Elevate your Load Testing!
Request a Demo

Set-up

In order to demonstrate how we can performance test FTP and SFTP we are going to need an FTP server, for the purposes of this post we are going to install a local FTP server and we will talk you through the set-up here.

If you have an FTP server at your disposal which you may use well then feel free to skip this part of the post.

We installed Wing FTP server for this purpose which can be found here

Once installed we created a domain called octoperf and created a user called octoperf which is done at start-up and is very simple to do using the pre-configured set-up wizard.

Wing FTP Server Admin Console

We created a home directory on our local machine that acts as a file system for the FTP server called ftp-file-system which will help analysing the JMeter tests we will run shortly and we will also create a local folder that we can use for files to upload called local-folder.

For the purposes of this post we will create them in the JMeter directory.

FTP Server Local File System

We then configure our FTP Server to use the local file system

FTP Server Configure Local File System

Now we are set-up lets start to look at how we use JMeter to test the FTP and SFTP protocols.

FTP Sampler

These examples will all use a single user with a single iteration to demonstrate the process and while we understand that you will be running at much higher volumes of load and concurrency this post will not demonstrate the execution of a performance test.

Let’s create a Test Plan with a Thread Group and an FTP Sampler.

JMeter FTP Request Sampler

Sampler Field Definition
Server Name or IP The FTP servers hostname or IP address.
Port Number The port that the FTP server is listening on, the default is 21 unless otherwise configured.
Remote File The location of the file you want to download from the FTP server or used as the destination for files that are uploaded to the FTP server.
Local File The location on the local file system of files you want to upload to the FTP server or used the destination for files that are downloaded from the FTP server.
Local File Contents This can be used to provide content to upload to the FTP server, this can be used instead of the Local File for uploads, this will override the Local File if a value is entered.
get(RETR) Command to download from the FTP server.
put(STOR) Command to upload to the FTP server.
Use Binary Mode Determines whether the transfer should be done as Binary, should be checked for all files except plain text.
Save File in Response If this is checked then the contents of the file, when performing a get(RETR) command, will be stored in the sampler Response Data.
Username Username for authentication.
Password Password for authentication.

Now we have discussed the sampler let’s build a test to retrieve a file from our FTP server.

Let’s have a look at the FTP Request sampler with the fields populated.

Populated FTP Request Sampler

We have our hostname set to localhost and the port set to 21 which matches our local FTP server configuration as well as using the username and password we created when setting up our local server. Obviously if you are using a test FTP server in your organisation you need to set these values based on its configuration.

We have selected get(RETR) as our command and defined the file we want to download as octoperf-remote.txt and the downloaded file to be named octoperf-dowloaded.txt and to be stored in the local-folder location we created earlier.

We have added the following listeners, so we can check the response:

  • View Results Tree,
  • Aggregate Report,
  • View results In Table.

Before we run our test lets create the file to download, we do this in our ftp-file-system folder, as this is simulating our FTP servers file system.

FTP Server File To Download

For the purposes of our simple example this file will contain a simple string:

FTP Server File Contents

We can check that the local-folder location is empty:

Local File System

Let’s run the test, and we can see from the View Results Tree that our test was successful.

Download Test Results

If we then check our local-folder we can see that the file has been downloaded and is called octoperf-downloaded.txt

Local File System

We can also check our FTP Server logs and see the transaction taking place.

FTP Server Logs

So, we have proved that the JMeter test works but your objective will be to run a performance test and measure the throughput and response times of the downloads.

The listeners we added in addition to the Tree View provide us information we can use to measure performance, let’s look at these.

JMeter Aggregate Report

This shows us the Response Time of the requests and the Throughput Rate as well as Received KB/sec which is important information when measuring the performance of an FTP Server.

JMeter View Results In Table

This shows us the Byte Size of the download and the Latency which are again important information for measuring the performance of an FTP Server.

If we check the Save File in Response checkbox and add an Assertion, we can demonstrate how we can ensure the downloaded files contain the data we expect.

JMeter Assertion

If we run our test again:

JMeter Assertion Results

We can see that the body of the file is present in the Response Body and that our Assertion is successful.

We have looked at how we test the downloading of a file we will now look at building a JMeter test to upload a file.

JMeter FTP Sampler

We have changed the command to be put(STOR) and we have changed the local file, which we are going to upload, to octoperf-uploaded.txt and this will be created as octoperf-upload-remote.txt on the FTP Servers file system.

We have cleared the FTP Servers file system before the test to demonstrate the upload:

FTP Server File System

And we have created a file to upload:

Local File

Which again contains a simple string:

Local File Contents

We have kept the same listeners as they will give us valuable information of the upload performance.

If we execute our test:

Upload Test Results

We can see it is successful and if we check our FTP Servers file system, we can see the file is now present.

Remote File System

Before we look at more advanced techniques, we’ll show you how the Local File Contents option works.

Local File Contents Option

So, we have changed the remote file to be octoperf-upload-remote-local-file-contents.txt and removed the local file value and added text to the Local File Contents field.

If we run the test:

Remote File System

And we can now see we have a file in our ftp-file-system directory called octoperf-upload-remote-local-file-contents.txt:

Remote File System

And it contains the String we added to the sampler:

Remote File Contents

FTP Advanced Techniques

We have covered the upload and download of files between JMeter and an FTP Server and for the most part these commands will support most of your performance testing needs and are as we have seen the ones supported by the FTP Request sampler.

There are however many methods that the FTPClient supports which can be found here and you can also use JMeter to test these or even use JMeter to tidy a remote file system after your test has completed as part of a tear-down step.

If you want you can use the FTPClient to run the put and get commands we discussed earlier to the same effect.

To use the commands not covered by the FTP Request sampler you need to use a JSR223 Sampler and some Groovy.

Let’s look at how we set our sampler up:

JSR223 Sampler

Let’s look at the JSR223 Sampler in more details:

import org.apache.commons.net.ftp.FTPClient

// Create an instance of the client
def ftpClient = new FTPClient()

// Connect and logon
ftpClient.connect("localhost", 21)
ftpClient.login("octoperf", "octoperf")

// Logoff
ftpClient.logout()ftpClient.logout()

If we run this test:

JSR223 Sampler Results

We see a positive response from our test and if we look at the FTP Server logs, we can see our commands.

FTP Server Logs

Let’s say for example we wanted at the end of a test to move all the files we had uploaded to the FTP Server to a new directory we could first create a new directory on the FTP Server file system, lest look at how we would do that.

Let’s update our JSR223 Sampler to the following

import org.apache.commons.net.ftp.FTPClient

// Create an instance of the client
def ftpClient = new FTPClient()

// Connect and logon
ftpClient.connect("localhost", 21)
ftpClient.login("octoperf", "octoperf")
// Make a new directory, returns a boolean
def retVal = ftpClient.makeDirectory("old-test-results")

// Write the results to the log
log.info("--------------------------------------------")
def logString = retVal? "Directory created successfully": "Directory not created"
log.info(logString)
log.info("--------------------------------------------")

// Logoff
ftpClient.logout()

If we execute our test again, we can see that the log reports a successful creation of a new folder

JMeter Log

And we can see that a folder has been created on the FTP Server

Remote File System

We can extend this test further and move our uploaded text files to our newly created folder.

import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile

// Create an instance of the client
def ftpClient = new FTPClient()

// Connect and logon
ftpClient.connect("localhost", 21)
ftpClient.login("octoperf", "octoperf")

// Make a new directory, returns a boolean
def retVal = ftpClient.makeDirectory("old-test-results")

// Write the results to the log
log.info("--------------------------------------------")
def logString = retVal? "Directory created successfully": "Directory not created"
log.info(logString)
log.info("--------------------------------------------")

log.info("--------------------------------------------")

// Create a file array
FTPFile[] files = ftpClient.listFiles();

// Iterate over the files and move to our new folder
 for (FTPFile file : files) {

    // Write the file name to the log
    log.info(file.getName())

    // Check if its a file as we need to ignore directories
    if(file.isFile()) {

    // Move the files and write to the logs
    def moveOk = ftpClient.rename(file.getName(), "old-test-results/" + file.getName());
    logString = moveOk? "File moved": "File not moved"
    log.info(logString)
    }
    else {
        log.info(file.getName() + " is not a file")     
    }

}

log.info("--------------------------------------------")

// Logoff
ftpClient.logout()

If we now run our test, we see the logs tells us that the files have been moved and the directory ignored:

JMeter Log

And we can see that the files have been moved in the FTP Server file system:

Remote File System

This is a simple example of using JMeter to tidy data at the end of your test but you can create complex performance test scenarios using the JSR223 Sampler.

SSH SFTP Sampler

The SSH SFTP Sampler does not come with the standard JMeter installation and you will need to install SSH Protocol Support from the Plugins Manager:

Plugins Manager

Once you have installed these libraries and restarted JMeter you will have the option to add an SSH SFTP Sampler, let’s look at this:

JMeter SSH SFTP Sampler

Sampler Field Definition
Hostname The FTP servers hostname or IP address.
Port The port that the FTP server is listening on, the default is 22 unless otherwise configured.
Connection Timeout The maximum time to wait for a connection before failing.
User Name Username for basic authentication.
Password Password for basic authentication.
SSH Key File Access credentials for SSH.
Passphrase Phrase used to protect the key file.
Action Command to run on the FTP server.
Source Path The location on the local file system if you are uploading a file (put) or the location on the FTP Server of a file to download (get).
Print File Contents If this is set to True then the contents of the file, when performing a get command, will be stored in the sampler Response Data and not saved to the file system, to save to the file system this must be set to False.
Destination Path The location on the FTP Server if you are uploading a file (put) or the location on the local file system of a file to download (get).

There are obviously a lot of similarities between the FTP Request Sampler and the SSH SFTP Sampler because their objective is to communicate with an FTP Server albeit one using encryption.

For the purposes of this post we do not require a SSH Key File to communicate with our local FTP Server but in your organisation you will need to talk with your FTP Server administrator to get the credentials to connect securely and populate the SSH Key File and Passphrase.

We will follow a similar pattern to the tests we created for the FTP request Sampler and simulate downloading and uploading a file using an SSH SFTP Sampler.

Let’s start with downloading a file from our FTP Server:

Populated JMeter SSH SFTP Sampler

We have set the Action field to be get and the Source path is the location on the FTP Server file system of the file and the Destination path is the location on our local machine the file will be downloaded to.

We have set the Print file content field to False so the file downloads.

If we run the test:

JSR223 Test Results

We can see that our Destination path contains a file with this name:

Local File System

We will now look at uploading a file using the put Action:

JMeter SSH SFTP Sampler

We have changed the Action field to put and change the Source path to be the location of our file on the local file system and the Destination path is set to the filename to create on the FTP Server.

If we run our test:

JMeter SSH SFTP Sampler Results

We can see that the file is now present on the FTP Server file system:

Remote File System

SFTP Advanced Techniques

As with FTP we can use a JSR223 Sampler and some Groovy to test SFTP using JMeter.

You will need to use the Java Secure Channel library in order to use this technique which is included in the SSH Protocol Support libraries you downloaded earlier in this post.

We will look at creating a remote directory on our FTP Server which is what we did when looking at the advanced techniques for FTP.

JMeter JSR223 Sampler

This is the code which is self-explanatory:

import com.jcraft.jsch.*;

def jsch = new JSch()
    //create SSH connection
     String host = "localhost";
     String user = "octoperf";
     String password = "octoperf";

     Session session = jsch.getSession(user, host, 22);
     session.setPassword(password);

    // Supress SSH keys check, should not be used in a enterprise environment
     java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
     session.setConfig(config);

    // Connect to the server
     session.connect();

    // Connect to the secure channel
    Channel channel = session.openChannel( "sftp" )
    channel.connect()

    ChannelSftp sftpChannel = (ChannelSftp) channel

    // Make the new directory
    sftpChannel.mkdir("sftp-old-test-results")

    // Disconnect
    sftpChannel.exit()
    session.disconnect()

It's important to pick out this section:

// Supress SSH keys check, should not be used in a enterprise environment
     java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
     session.setConfig(config);

This is here purely for the purposes of our simple tests on a local FTP Server and should not be used against your organisation's FTP Server where you should be authenticating using SSH Keys.

If we run our test:

JMeter JSR223 Sampler Results

We can see that the test ran successfully and if we check our FTP Server file system:

Remote File System

We can see our new directory has been created.

Conclusion

This is an introduction in to how FTP and SFTP can be tested using JMeter, we have mainly focussed on the principles of how the JMeter tests are constructed rather than load and concurrency but given the building blocks in this post you should be able to start creating JMeter tests that do stress your FTP Servers and start to understand how they behave under load.

Remember that the script file that contains all the examples can be downloaded here.

Want to become a super load tester?
Request a Demo