This chapter discusses the various components of JMeter and helps you develop the first JMeter test script. You will find out about the various ways (GUI/non-GUI) of running and stopping/shutting down JMeter test scripts in standalone test execution and server mode of test execution.
At the end of this chapter, you will have a good idea of the various components of JMeter and be able to write a simple JMeter test script, as well as start/stop/shut down tests from GUI and non-GUI modes.
Before starting with the JMeter test script development, let’s walk through the components of a test plan.
Components of a JMeter Test
Caution
JMeter test scripts have a filename extension of .jmx. This is not related to Java Management Extensions (JMX) .
A JMeter test script consists of hierarchical and ordered components organized in the form of a tree.
Each of these components has properties that can be configured in the following ways:
By jmeter.properties file
By using command-line parameters
By editing the .jmx file directly using a text editor
By using the GUI
By using values extracted from the responses received to sampler requests
Test Plan
The test plan is the top-most element and is the root of the tree. For a test plan , the name, description, and user variables can be configured.
Thread Group
Every test has one or more thread groups. A thread group is a child element of a test plan. Each thread group represents a use-case. As you will see later in the book, a thread group can be configured with the number of threads, ramp-up time, and other useful properties that allow you to control its behavior.
Controller
Each thread group has one or more controller elements. Logical controllers decide the flow of execution. They determine the next Sampler to execute . JMeter comes with many built-in logical controllers that provide precise control flow. For example, the If controller and Switch controller provide branching; the ForEach, While, and For provide iteration flow. There is a controller for every programming construct. A custom controller, if needed, could be developed using the plugin API mechanism provided by JMeter.
Sampler
Sampler is a child element of a thread group or a controller. It sends requests to the server. For every protocol, we need a separate sampler. Out of the box, JMeter comes with many samplers. For example, to send a HTTP request, you use a HTTP Request Sampler. Custom samplers can be developed using the JMeter plugin mechanism.
Listener
Listeners listen to the responses from the server and assemble and aggregate the performance metrics. They are used to display graphs. We need at least one listener per test script so that we can interpret and understand the results of the performance test.
Timer
A timer introduces a delay in the flow. Delay is needed between sampler requests for the following reasons:
To simulate the time that the user takes to perform the next action on the web page
To simulate a realistic load distribution on the server
Add timers as child elements of samplers or logic controller that need the delay.
Assertions
Assertions are used to verify that the server responses are as expected. Assertions test various status codes, and then pause, alert, or log bad request/responses. It is important to ensure that the server is not returning any error codes during the execution.
Adding an assertion as a child of the sampler restricts it to a single sampler. Otherwise, assertions will apply to all samplers that are in scope.
Add an Assertion Results Listener to the thread group to view the assertion results. Failed assertions will also be displayed in the View Results Tree and the View Results in Table Listeners, and will count toward the error percentage, for example, in the Aggregate Reports and Summary Reports.
Config Element
Configuration elements are placeholders for properties, including user-defined properties and variables. For example, the HTTP Cookie Manager is a configuration element. Configuration elements can be scoped out with a nesting level.
Pre-Processors
Pre-processors take the request and modify it (substitution, enhancement, dereferencing variables, etc.) before the sampler sends it to the server.
Post-Processors
Post-processors process the response from the server. They are used to process the server response and modify the component settings or to update variables.
Order of Component Execution
Programming languages evaluate expressions by following certain rules based on operator precedence. For example, in the expression a + b * c, the operation b * c is performed first and the result is added to a. Although the operator * is not the first operator appearing in the expression, it is applied first as it has a higher precedence value.
Similarly, JMeter follows certain rules while executing the components in a test plan. Within a thread group, the order of execution of the components follows the order shown in Figure 3-1. Even if a Listener component was placed as the first element in the thread group, it will be executed after samplers and post processors. However, controllers and samplers have the same precedence, so they will be executed in the order in which they appear in the test plan.
Figure 3-1. Order of execution
JMeter allows the elements to be nested. For example, a listener, a timer, and a sampler can be nested under a logical controller. In this case, when the logical controller is being executed, the order of execution of its child nodes would be the timer, the sampler, then the listener.
Figure 3-2 shows the order of execution in the test plan .
Figure 3-2. Test plan
As you can see in the JMeter test plan, the components are not in sequence.
However, JMeter will rearrange them according to the Order of Execution diagram shown in Figure 3-1. Note that the CSV Data Set Config is nested under the transaction controller. The child elements nested under the transaction controller would again follow the Order of Execution diagram and get sequenced.
For the given test plan, the execution order will be:
BeanShell Pre-processor
Constant Timer
Constant Throughput Timer
Transaction Controller
CSV Data Set Config
HTTP Request
BeanShell PostProcessor
Aggregate Report
Note
All the examples mentioned in this book have been coded against a sample web application called Digital Toys Inc. Refer to Chapter 14 for setup instructions.
Simple JMeter Test
This first test script is very simple.
Simulate a user browsing the Digital Toys Inc. web application home page (http://localhost:8080/dt).
Check for an HTTP status code of 200.
Follow these steps or download FirstTestPlan.jmx.1
Start JMeter GUI.
Create a test plan and give it a meaningful name, such as First Test.
Click on Test Plan and go to Edit ➤ Add ➤ Threads (Users) and add Thread Group. Configure the Number of Threads (Users) as 1 and Loop Count as 1 (see Figure 3-3).
Figure 3-3. Thread group
Click on Thread Group and go to Edit ➤ Add ➤ Sampler and add HTTP Request. Configure Server Name or IP as localhost, Port Number as 8080, and Path as /dt. Uncheck Follow Redirects (see Figure 3-4).
Figure 3-4. HTTP request
Click on HTTP Request and go to Edit ➤ Add ➤ Assertions and add Response Assertion. Configure Response Field to Test as Response Code, Pattern Matching Rules as Equals, and Patterns To Test as 200 (see Figure 3-5).
Figure 3-5. Assertions
Click on Thread Group and go to Edit ➤ Add ➤ Listener and add View Results Tree (see Figure 3-6).
Figure 3-6. Run test
Save the test plan .
Go to Run ➤ Start to run the test (on Mac OSX, type CMD+R).
Verify the responses in the View Results Tree (see Figure 3-7).
Figure 3-7. Test results
Now, let’s simulate a load of 10 users by following these steps.
Highlight Thread Group and configure Number of Threads (Users) as 10, Ramp-Up Period (in seconds) as 0, and Loop Count as 1.
Save the test plan .
Go to Run ➤ Start to run the test (on Mac OSX, type CMD+R).
Verify the responses in the View Results Tree (see Figure 3-8).
Figure 3-8. Execution results
Navigate to each response under the View Results Tree and verify the response and response time (Load Time). Calculate the average response time as the sum of the response time of all threads/number of threads.
GUI Mode
JMeter GUI mode provides several options to start/stop test(s) (see Figure 3-9).
Figure 3-9. JMeter test execution options
Start: Start a test on the local machine
Stop: Abruptly stop the test that is running on the local machine
Shutdown: Stop the test gracefully, allowing the threads to wind down
Remote Start All: Start the test on all the remote JMeter hosts
Remote Stop All: Abruptly stop the test on all the remote JMeter hosts
Remote Shutdown All: Gracefully shut down the test on all the remote JMeter hosts
Note
Use the GUI to create and debug the tests scripts. Use the non-GUI mode to execute the test and collect the results.
Non-GUI Mode
GUI mode is not desirable because:
It consumes a large amount of resources, thus interfering with the test results
It may not be available in some environments (such as a remote shell)
To avoid these issues, use the non-GUI mode.
To see the command line options provided by JMeter, issue the following command in the CMD prompt.
jmeter --help
This will show you the help options provided by JMeter .
C:apache-jmeter-3.0in>jmeter --help
Writing log file to: C:apache-jmeter-3.0injmeter.log
_ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ | _ / / ___| | | | ____| | | / | ____|_ _| ____| _
/ _ | |_) / _ | | | |_| | _| _ | | |/| | _| | | | _| | |_) |
/ ___ | __/ ___ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_ 3.0 r1743807
Copyright (c) 1999-2016 The Apache Software Foundation
To list all command line options, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -?
--------------------------------------------------
To run Apache JMeter in GUI mode, open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) [-p property-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l log-file]
--------------------------------------------------
To run Apache JMeter in NON_GUI mode and generate a report at end :
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l log-file] -e -o [Path to output folder]
--------------------------------------------------
To generate a Report from existing CSV file:
Open a command prompt (or Unix shell) and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -g [log-file] -o [path to output folder (empty or not existing)]
--------------------------------------------------
To tell Apache JMeter to use a proxy server:
Open a command prompt and type:
jmeter.bat(Windows)/jmeter.sh(Linux) -H [your.proxy.server] -P [your proxy server port]
---------------------------------------------------
To run Apache JMeter in server mode:
Open a command prompt and type:
jmeter-server.bat(Windows)/jmeter-server(Linux)
---------------------------------------------------
C:apache-jmeter-3.0in>
Executing a Single Test
Let’s explore command-line options using the following examples.
Download FirstTestPlan.jmx 2 and issue the following command in the CMD prompt.
C:>jmeter -n -t FirstTestPlan.jmx -l test-run.jtl
Note that test-run.jtl is the test execution log file.
Proxy Server Setting
If you are behind a proxy server, append the following options.
-H <proxy host server name>, -P <port number>
Start JMeter in Server Mode
JMeter also runs in master-slave formation, known as server mode. To run JMeter in server mode, issue the following command in the CMD prompt.
C:>jmeter-server
... Trying JMETER_HOME=..
Found ApacheJMeter_core.jar
Writing log file to: C:apache-jmeter-3.0injmeter-server.log
Created remote object: UnicastServerRef [liveRef: [endpoint:[172.17.65.111:61425](local),objID:[-10c0a943:15c299a388a:-7
fff, -3710696168706880958]]]
Stop/Shutdown JMeter
To stop JMeter test execution, issue the following command in the CMD prompt. Once the command executes, it sends a stop signal to the JMeter test. The response message in the CMD prompt should read StopTestNow request to port 4445.
C:apache-jmeter-3.0in>stoptest.cmd
Sending StopTestNow request to port 4445
To shut down JMeter, issue the following command in the CMD prompt. Once the command executes, it will send a shutdown signal to the JMeter test. The response message in the CMD prompt should read Shutdown request to port 4445.
C:apache-jmeter-3.0in>shutdown.cmd
Sending Shutdown request to port 4445
Conclusion
In this chapter, you learned about the components of a JMeter test and wrote a simple JMeter test. You also learned how to start/stop/shut down JMeter test execution via GUI mode and from the command prompt. In the next chapter, you will learn to record user actions and create JMeter test scripts by using HTTP(s) Test Script Recorder.