Executing a test script
Sometimes, the test scripts to execute have already been recorded and handed over to you to run. In this recipe, we will show you just how to go about executing a script that was prerecorded.
How to do it…
To execute a test script, perform the following steps:
- Open the command line prompt.
- Change to the directory of your JMeter install. We'll refer to this as
JMETER_HOME
.Note
Refer to Appendix for JMeter installation instructions.
- Change
JMETER_HOME
to thebin
directory. - To launch the JMeter GUI on Windows, type in
jmeter.bat
. Alternatively, for Unix/Mac OS, type in./jmeter
. - Navigate to File | Open.
Note
Alternative key binding: Mac OS (Command + O), Windows (Ctrl + O).
- Navigate to the script you want to execute. For example,
ch1/getting_started.jmx
provided with the sample code. - Press the green start icon at the top.
Note
Alternative key binding: Mac OS (Command + R), Windows (Ctrl + R).
- View the results from one of the added listeners. If you are using
getting_started.jmx
from step 6, click on the View Results Tree listener as shown in the following screenshot:
How it works…
Test scripts are a series of prerecorded requests issued against an application. They are captured interactions of a user's actions with the application. These include visiting a URL, navigating around several pages, logging in, and so on. JMeter, like most test tools, has the ability to record and replay back test scripts. JMeter test scripts are stored in the XML (extendable markup language) format with the .jmx
extension. Curious users can open the test script in a text editor of their choice (for example, Sublime Text, Notepad, and so on) and view exactly what it is doing, though, it is much more clear to understand what a script does by looking at it in JMeter's GUI.
After executing the test, results are reported through any configured listeners. A script can have one or several listeners attached to it (see recipes in Chapter 7, Building, Debugging, and Analyzing the Results of Test Plans).
There's more…
Scripts can also be executed in what JMeter refers to as non-GUI mode, which is completely on the command line without launching the JMeter GUI. To do this, instead of executing jmeter.bat
or JMeter, like we did in step 4, we'll use certain JMeter command-line arguments to specify the test script and the resulting output file, which we can then go back and view with the JMeter GUI after the tests are executed.
On Unix/Mac OS, type in the following command:
./jmeter -n -t [path to test script] -l [path to results files]
On Windows, type in the following command:
jmeter.bat -n -t [path to test script] -l [path to results files]
An example of running in non-GUI mode
Here is an example of running code in non-GUI mode: This is how we executed the google_simulation.jmx test script in our bundled sampled in non-GUI mode. As you can see, we supplied parameters –n and –t and the absolute path to where our script is located (/Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx
) followed by the path to the results file (/Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation_result.csv
).
./jmeter -n -t /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx -l /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation_result.csv
Created the tree successfully using /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx
.
Starting the test @ Tue Feb 25 05:07:45 EST 2014 (1393322865378)
Waiting for possible shutdown message on port 4445
.
Tidying up ... @ Tue Feb 25 05:08:46 EST 2014 (1393322926156)
... end of run
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
To view the results after the test has been executed, open up the JMeter GUI, create a new test plan, and add a listener to it by navigating to Test plan | Add | Listener | Aggregate Report. Click on the Browse… button to navigate to the location of the results file and open it. This is demonstrated in the following screenshot:
There are several reasons why you might want to execute a test in non-GUI mode. Some of the reasons include:
- Running on a Unix machine with no GUI layer (that is, the JMeter GUI does not even open up)
- Running a distributed test
- Performance reasons of the JMeter GUI, which can sometimes be resource intensive; running in non-GUI mode alleviates this issue