Dmitri Tikhanski is a Contributing Writer to the BlazeMeter blog.

Become a JMeter and Continuous Testing Pro

Start Learning
Slack

Test Your Website Performance NOW!

arrow Please enter a URL with http(s)
Nov 24 2015

How to Run External Commands and Programs Locally and Remotely from JMeter

Apache JMeter is a multi-protocol load testing tool and it supports a variety of transports including:

 

 

The modular, plugin-based design allows one to extend JMeter to support almost any protocol.

 

This post looks at how to invoke OS commands and 3rd-party programs on local and remote machines. I refer to “local” as the host where JMeter is running, and “remote” as a different host where the application/web server or database lives.

 

Baseline use cases can include:

 

  • running a script
  • reading a log file
  • performing a configuration change
  • deploying/starting an application under test before the test starts, and stopping/uninstalling it after the test ends
  • and more
     

Local Commands


Executing a command on a local machine is fCommand Parametersairly easy via the OS Process Sampler, which is available out-of-the-box. The sampler GUI and an explanation of possible parameters are as follows:

 

 


Here are the various parameters and the description  for each:

 

  • Command - An OS command or program to execute. If the target exists in PATH, just the command name will be enough. If not, you’ll need to provide the full path.
  • Working Directory - The command will be executed in the directory specified. If omitted, the execution context will be JMeter’s base directory (usually JMeter’s “bin” folder - where JMeter startup scripts live).
  • Command Parameters - Any command-line arguments, if needed. Each argument should go on the separate line.
  • Environment Variables - If your command relies on specific set environment variables, you can provide keys and values here.
  • Standard Input - If your command expects the console input, the default stdin (standard input) can be overridden with a file using this parameter.
  • Standard Output - If set, the output of your command will go to the file specified. If not, the output will be returned as a Sample Result.
  • Standard Error - Same as above but for overriding the stderr (standard error) output.
  • Check Return Code & Expected Return Code - These parameters work similarly to the Response Assertion on the Status Code. If the check for the response code is enabled, JMeter will compare the actual response code with the Expected Return Code and fail the sampler in case of a mismatch. For Windows, the exit code for a program is stored in the %ERRORLEVEL% environment variable. For Linux and MacOSX, the exit code can be accessed as $?.
  • Timeout  The time in milliseconds for the command’s completion. If exceeded, JMeter will interrupt the process and fail the sampler.

 

For example, to invoke Notepad on Windows and open the JMeter readme in it, configure the OS Process Sampler as the following:

 

  • Command: notepad.exe
  • Working Directory: C:\Windows\System32
  • Command Parameter: C:\apache-jmeter-2.13\README

 

 

Remote Commands: Windows


If both the JMeter host and Application Under Test host are running Windows, you can use the above OS Process Sampler and 2 options:

 

  1. Use applications from the PsTools package from the OS Process Sampler
  2. Use  Windows Powershell Remoting.

 

As the PsTools approach is pretty straightforward, let’s focus on the Powershell one. Powershell Remoting can be enabled by the next steps on Application Under Test side:

 

  1. In Powershell execute the following commands:
    • Enable-PSRemoting -Force
    • Set-Item wsman:\localhost\client\trustedhosts * (You can use the hostname, IP address or comma separated list of hostnames/addresses instead of the asterisk.)
  2. In Windows Firewall, open port 5985 if you use HTTP and 5986 if you use HTTPS.

 

On the JMeter host side, you can just execute the Powershell Invoke-Command cmdlet from the OS Process Sampler:

 

  • Command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • Parameter: $password = ConvertTo-SecureString -String ${password} -AsPlainText -Force; $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "${username}",$password; Invoke-Command -Computer ${host} -cred $credentials -scriptBlock {Stop-Process -processname notepad}

 

Change:

 

  • ${username} to remote Windows username
  • ${password} to remote Windows password
  • ${host} to remote Windows hostname or IP address
  • -scriptBlock to your own Powershell script

 

This example stops the Windows Notepad process on a remote machine.

 

 

Remote Commands: Linux/MacOSX

 

Linux, Unix and MacOSX operating systems can be remotely accessible (in majority of cases) through SSH (Secure Shell) protocol. And again you have 2 choices:

 

  1. Use the plink program (it can be downloaded from the same page as the popular PuTTY SSH and Telnet client) from the OS Process Sampler.
  2. Use the JMeter SSH Sampler plugin.

 

Again, since running the “plink” program via the OS Process Sampler is quite easy, let’s see how *nix hosts can be controlled with the SSH Sampler. 

 

Installation:

 

  1. Download ApacheJMeter_ssh-x.x.x.jar and jsch-x.x.x.jar from the SSH Sampler Releases page.
  2. Drop Apache_JMeter_ssh-x.x.x.jar to /lib/ext folder of your JMeter installation.
  3. Drop jsch-x.x.x.jar to /lib folder of your JMeter installation.
  4. Restart JMeter.
  5. You should see 2 new Samplers: SSH Command and SSH SFTP.
     

 

 

So it is quite enough to provide host, port, credentials or a private key file and command line, and the command will be executed on the remote Linux, Unix or MacOSX system. The image below demonstrates remote execution of JMeter on Linux from the Windows host. 

 

 

Blazemeter is 100% JMeter-compatible so feel free to use any of the above approaches in your tests (just don’t forget to upload any extra .jar files with your test plan).

 

This has been an explanation on how to run commands and programs on local and remote machines right from your JMeter test. If anything is still unclear - don’t hesitate to ask your questions below.

 

LEARN MORE
 

If you are new to JMeter, and you’d like to learn more, please sign up for our free online JMeter training course.

 

For more experienced JMeter users, you'll want to view the on-demand webcast, How to Create Advanced Load Testing Scenarios with JMeter.

 

Be sure to read through all our JMeter list of resources

 

     
arrow Please enter a URL with http(s)

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