JMeter - Self Training For Novice Users

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

JMeter – Performance

Testing Tool
Environment Setup, Record/Enhance Script, Execution & Analysis
Contents
1. Setup and Launch JMeter Tool................................................................................................................................4
1.1 Introduction – Apache JMeter™......................................................................................................................4
1.2 Install JAVA......................................................................................................................................................4
1.3 Install Jmeter...................................................................................................................................................5
1.4 Start Jmeter – GUI Mode.................................................................................................................................5
1.5 Using JMeter behind a proxy...........................................................................................................................6
1.6 Start JMeter - Non-GUI Mode (Command Line Mode)....................................................................................6
1.7 Install JMeter Plugins.......................................................................................................................................7
2. Record, Enhance, Replay & Debug..........................................................................................................................8
2.1 Recording Script – jmeter_proxy_step_by_step..............................................................................................8
2.2 Enhance Script with Test Plan Elements..........................................................................................................8
ThreadGroup...........................................................................................................................................................8
Controllers...............................................................................................................................................................8
Listeners.................................................................................................................................................................. 9
Timers...................................................................................................................................................................... 9
Assertions................................................................................................................................................................9
Configuration Elements.........................................................................................................................................10
Pre-Processor Elements.........................................................................................................................................10
Post-Processor Elements.......................................................................................................................................10
Execution order.....................................................................................................................................................10
Properties and Variables.......................................................................................................................................10
Using Variables to parameterise tests...................................................................................................................11
Regular Expressions...............................................................................................................................................11
Correlation in JMeter (handling dynamic values)..................................................................................................12
2.3 Script – Replay, Debug, Load Test Execution & Analysis................................................................................12
3. JMeter Distributed Testing....................................................................................................................................14
3.1 Prerequisites..................................................................................................................................................14
3.2 Details............................................................................................................................................................14
3.3 Terminology...................................................................................................................................................14
3.4 Step-by-Step..................................................................................................................................................15
3.5 Starting the Test............................................................................................................................................16
Start a single client................................................................................................................................................16
Start all clients.......................................................................................................................................................17
References–.................................................................................................................................................................. 19

Page No. 2
Page No. 3
1. Setup and Launch JMeter Tool
1.1 Introduction – Apache JMeter™

The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test
functional behavior and measure performance. It was originally designed for testing Web Applications but has
since expanded to other test functions.

Ability to load and performance test many different applications/server/protocol types:

 Web - HTTP, HTTPS (Java, NodeJS, PHP, ASP.NET, …)


 SOAP / REST Webservices
 FTP
 Database via JDBC
 LDAP
 Message-oriented middleware (MOM) via JMS
 Mail - SMTP(S), POP3(S) and IMAP(S)
 Native commands or shell scripts
 TCP
 Java Objects

1.2 Install JAVA

1. Download link for JAVA SE Runtime Environment 8:


http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html
2. Follow the installation process and complete the installation.
3. Set Environment variable JAVA_HOME = <Your JRE folder> (i.e. C:\Program Files\Java\jre1.8.0_131)
4. Set PATH variable = ;%JAVA_HOME%\bin; C:\Program Files\Java\jre1.8.0_131\bin
5. Open Command prompt.
6. Write java and enter. It should show the following:

Page No. 4
1.3 Install Jmeter

1. Download link for Jmeter: http://jmeter.apache.org/download_jmeter.cgi


2. Download apache-jmeter-3.2.zip (Requires Java 8 or later) file.
3. Extract it to your specified folder. (I.e. C:\)

1.4 Start Jmeter – GUI Mode

1. Go to: <Jmeter Folder>\bin (I.e. C:\apache-jmeter-3.2\bin).


Page No. 5
2. Double click on jmeter.bat file.
3. It should open the following Jmeter window:

1.5 Using JMeter behind a proxy

If you are testing from behind a firewall/proxy server, you may need to provide JMeter with the
firewall/proxy server hostname and port number. To do so, run the jmeter[.bat] file from a command line
with the following parameters:

-H [proxy server hostname or ip address]


-P [proxy server port]
-N [nonproxy hosts] (e.g. localhost)
-u [username for proxy authentication - if required]
-a [password for proxy authentication - if required]

Example:
jmeter -H my.proxy.server -P 8000 -u username -a password -N localhost

1.6 Start JMeter - Non-GUI Mode (Command Line Mode)

For load testing, you must run JMeter in this mode (Without the GUI) to get the optimal results from it. To
do so, use the following command options:

-n This specifies JMeter is to run in non-gui mode


-t [name of JMX file that contains the Test Plan].
-l [name of JTL file to log sample results to].
-j [name of JMeter run log file].
Page No. 6
-r Run the test in the servers specified by the JMeter property "remote_hosts"
-R [list of remote servers] Run the test in the specified remote servers
-g [path to CSV file] generate report dashboard only
-e generate report dashboard after load test
-o output folder where to generate the report dashboard after load test. Folder must not exist
or be empty

The script also lets you specify the optional firewall/proxy server information:

-H [proxy server hostname or ip address]


-P [proxy server port]

Example
jmeter -n -t my_test.jmx -l log.jtl -H my.proxy.server -P 8000

If the property jmeterengine.stopfail.system.exit is set to true (default is false), then JMeter will


invoke System.exit(1) if it cannot stop all threads. Normally this is not necessary.

Additional script in bin folder:

jmeter-server.bat start JMeter in server mode


mirror-server.cmd runs the JMeter Mirror Server in non-GUI mode
shutdown.cmd Run the Shutdown client to stop a non-GUI instance gracefully
stoptest.cmd Run the Shutdown client to stop a non-GUI instance abruptly

1.7 Install JMeter Plugins

1. Download link for Jmeter Plugins: https://jmeter-plugins.org/


2. Download Required Plugin / Plugin Manager and copy the .jar file to folder - “<Jmeter folder>\lib” and
“<Jmeter folder>\lib\ext”.
3. Close and Reopen the JMeter to reflect the Plugin in JMeter.
4. If working behind company proxy then add following argument in jmeter.bat to allow JMeter Plugin
Manager to work.

JVM_ARGS="-Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=8080"

If required user name and password, add below parameter

JVM_ARGS="-Dhttps.proxyHost=myproxy.com -Dhttps.proxyPort=8080 -Dhttp.proxyUser=john


-Dhttp.proxyPass=***"

Page No. 7
2. Record, Enhance, Replay & Debug
2.1 Recording Script – jmeter_proxy_step_by_step

2.2 Enhance Script with Test Plan Elements

The Test Plan is where the overall settings for a test are specified and explains sequence of steps execute at run time.
A final test plan made up of one or more Thread Groups, Sampler, logic controllers, listeners, timers, assertions, and
configuration elements. Each Sampler can be preceded by one or more Pre-processor element, followed by Post-
processor element, and/or Assertion element. It saves in Java Management Extensions (JMX) format.

The Test Plan object has a checkbox called "Functional Testing". If selected, it will cause JMeter to record the data
returned from the server for each sample. If you have selected a file in your test listeners, this data will be written to
file. This can be useful if you are doing a small run to ensure that JMeter is configured correctly, and that your server
is returning the expected results. The consequence is that the file will grow huge quickly, and JMeter's performance
will suffer. This option should be off if you are doing stress-testing (it is off by default).

If you are not recording the data to file, this option makes no difference.

You can also use the Configuration button on a listener to decide what fields to save.

List of Basic Test Plan Elements and General Description – Refer Apache Documentation for more details –

http://jmeter.apache.org/usermanual/index.html

ThreadGroup

Thread group elements are the beginning points of any test plan. All controllers and samplers must be under
a thread group. Other elements, e.g. Listeners, may be placed directly under the test plan, in which case they
will apply to all the thread groups. As the name implies, the thread group element controls the number of
threads JMeter will use to execute your test.

Controllers

JMeter has two types of Controllers: Samplers and Logical Controllers. These drive the processing of a test.
Samplers tell JMeter to send requests to a server.

1) Samplers

Samplers facilitate JMeter to deliver explicit types of requests to the server. It simulates
a user’s request for a page from the target server. So that, to avail POST, GET, DELETE
functions on a HTTP service, user can add HTTP Request sampler. Apart from HTTP
Request sampler, there are other simpler too,

 HTTP Request
 FTP Request
Page No. 8
 JDBC Request
 Java Request
 SOAP/XML Request
 RPC Requests

2) Logical Controllers

Logic Controllers decide the order of processing of Samplers in a Thread. It offers a


mechanism to control the flow of the thread group. Logic Controllers facilitates to
customize the logic that JMeter uses to resolve when to send requests. Logic Controllers
can alter the order of requests coming from their child elements.

Logic Controllers of JMeter provide,

 Runtime Controller
 IFController
 Transaction Controller
 Recording Controller
 Simple Controller
 While Controller
 Switch Controller
 ForEach Controller
 Module Controller
 Include Controller
 Loop Controller
 Once Only Controller
 Interleave Controller
 Random Controller
 Random Order Controller
 Throughput Controller

Listeners

Listeners provide access to the information JMeter gathers about the test cases while JMeter runs. The
Graph Results listener plots the response times on a graph. The "View Results Tree" Listener shows details of
sampler requests and responses, and can display basic HTML and XML representations of the response.
Other listeners provide summary or aggregation information.

Additionally, listeners can direct the data to a file for later use. Every listener in JMeter provides a field to
indicate the file to store data to. There is also a Configuration button which can be used to choose which
fields to save, and whether to use CSV or XML format. Note that all Listeners save the same data; the only
difference is in the way the data is presented on the screen.

Listeners can be added anywhere in the test, including directly under the test plan. They will collect data
only from elements at or below their level.

Timers

The timer will cause JMeter to delay a certain amount of time before each sampler which is in its scope; by
default, a JMeter thread sends requests without pausing between each request

Page No. 9
Assertions

Assertions allow you to assert facts about responses received from the server being tested

Configuration Elements

A configuration element works closely with a Sampler. Although it does not send requests (except for
HTTP(S) Test Script Recorder), it can add to or modify requests.

A configuration element is accessible from only inside the tree branch where you place the element

The “User Defined Variables” Configuration element is different. It is processed at the start of a test, no
matter where it is placed. For simplicity, it is suggested that the element is placed only at the start of a
Thread Group.

Pre-Processor Elements

A Pre-Processor executes some action prior to a Sampler Request being made

Post-Processor Elements

A Post-Processor executes some action after a Sampler Request has been made

Execution order

1. Configuration elements
2. Pre-Processors
3. Timers
4. Sampler
5. Post-Processors (unless SampleResult is null)
6. Assertions (unless SampleResult is null)
7. Listeners (unless SampleResult is null)

Please note that Timers, Assertions, Pre- and Post-Processors are only processed if there is a sampler to
which they apply. Logic Controllers and Samplers are processed in the order in which they appear in the tree.
Other test elements are processed according to the scope in which they are found, and the type of test
element. [Within a type, elements are processed in the order in which they appear in the tree].

Properties and Variables

Properties are global to jmeter, and are mostly used to define some of the defaults JMeter uses (JMeter
properties are defined in jmeter.properties)

JMeter variables are local to each thread. The values may be the same for each thread, or they may be
different.

Note that the values defined by the Test Plan and the User Defined Variables configuration element are
made available to the whole test plan at startup. If the same variable is defined by multiple UDV elements,
then the last one takes effect. Once a thread has started, the initial set of variables is copied to each thread.
Other elements such as the User Parameters Pre-Processor or Regular Expression Extractor Post-Processor
may be used to redefine the same variables (or create new ones). These redefinitions only apply to the
current thread.

Page No. 10
The setProperty function can be used to define a JMeter property. These are global to the test plan, so can
be used to pass information between threads - should that be needed.

Using Variables to parameterise tests

Variables don't have to vary - they can be defined once, and if left alone, will not change value. So you can
use them as short-hand for expressions that appear frequently in a test plan. Or for items which are constant
during a run, but which may vary between runs. For example, the name of a host, or the number of threads
in a thread group.

For example, you might define the following on the Test Plan:

HOST www.example.com
THREADS 10
LOOPS 20

Regular Expressions

Regular Expression is used to extract string/value from response by giving contains and matching values as
expression; JMeter includes the pattern matching software Apache Jakarta ORO.

EXAMPLES

Extract single string

Suppose you want to match the following portion of a web-page:


name="file" value="readme.txt">
and you want to extract readme.txt.
A suitable regular expression would be:
name="file" value="(.+?)">

The special characters above are:

 ( and ) - these enclose the portion of the match string to be returned


 . - match any character
 + - one or more times
 ? - don't be greedy, i.e. stop when first match succeeds

Note: without the ?, the .+ would continue past the first ">until it found the last possible "> - which is
probably not what was intended.

Note: although the above expression works, it's more efficient to use the following expression:
name="file" value="([^"]+)">where
[^"] - means match anything except "
In this case, the matching engine can stop looking as soon as it sees the first ", whereas in the previous case
the engine has to check that it has found "> rather than say " >.

Extract multiple strings

Suppose you want to match the following portion of a web-page:


name="file.name" value="readme.txt" and you want to extract both file.name and readme.txt.
A suitable reqular expression would be:
name="([^"]+)" value="([^"]+)"

Page No. 11
This would create 2 groups, which could be used in the JMeter Regular Expression Extractor template as $1$
and $2$.

The JMeter Regex Extractor saves the values of the groups in additional variables.

For example, assume:

 Reference Name: MYREF


 Regex: name="(.+?)" value="(.+?)"
 Template: $1$$2$

Do not enclose the regular expression in / /

The following variables would be set:

 MYREF: file.namereadme.txt
 MYREF_g0: name="file.name" value="readme.txt"
 MYREF_g1: file.name
 MYREF_g2: readme.txt

These variables can be referred to later on in the JMeter test plan, as ${MYREF}, ${MYREF_g1} etc

Regular expressions use certain characters as meta characters - these characters have a special meaning to
the RE engine. Such characters must be escaped by preceeding them with \ (backslash) in order to treat
them as ordinary characters. Here is a list of the meta characters and their meaning (please check the ORO
documentation if in doubt).

() - grouping
[] - character classes
{} - repetition
*+? - repetition
. - wild-card character
\ - escape character
| - alternatives
^$ - start and end of string or line

Correlation in JMeter (handling dynamic values)

Most web applications generate session id for first access by user and then pass the same session id to sub-
sequent requests to maintain workflow and security; to automate the application workflow we required to
identify and extract such value to variable and then pass this variable as parameter to sub-sequent requests

Regular Expression Extractor allows the user to extract values from a server response using a Perl-type
regular expression; as a post-processor, this element will execute after each Sample request in its scope,
applying the regular expression, extracting the requested values, generate the template string, and store the
result into the given variable name.
Refer JMeter Documentation link for Regular Expression Extractor or add Regular Expression Extractor in
Test Plan and Help from menu bar to see the usage instructions.

Page No. 12
2.3 Script – Replay, Debug, Load Test Execution & Analysis

Debug Script Replay with Single Thread/User Load

a. Keep number of Thread = 1 and Loop=1 in Thread Group Properties (these are the default and one
can see by selecting Thread Group in Test Plan Tree at left pane)
b. Add below Listener to Test Plan
i. View Results Tree
ii. Aggregate Report
c. Click on Start Button (Play button of Green colour) or Ctrl+R
d. Test will replay and results can be verified in above added Listeners

Multi User Load Test Execution

a. Update number of Thread and Loop Properties as desired


b. Add below Listener to Test Plan
i. Aggregate Report, Save result file if required by giving full path with name (usually to be save
in .JTL extension)
ii. Add other listeners as required
iii. View Results Tree – Only add if required (log only errors)
c. Click on Start Button (Play button of Green colour) or Ctrl+R
d. Test will replay and results can be verified in above added Listeners

Analysis/Extract Results

a. Aggregate Report results can be copied to excel/csv for further customization if required
b. Aggregate Report result will give good idea of average response time, throughput and error rates;
refer other graphs/results from various listeners to analyse results as needed.
c. Sample Load Test Results which one can prepare for post execution analysis

Page No. 13
3. JMeter Distributed Testing
Note – The same test plan is run by all the servers. JMeter does not distribute the load between servers, each runs
the full test plan

3.1 Prerequisites

1. The firewalls on the systems are turned off


2. All the clients are on the same subnet
The server is in the same subnet, if 192.x.x.x or 10.x.x.x ip addresses are used. If the server doesn't use 192
or 10 ip address, there shouldn't be any problems.
3. Make sure JMeter can access the server.
4. Make sure you use the same version of JMeter on all the systems. Mixing versions may not work correctly.

3.2 Details

Once you've made sure the systems are ready, it's time to setup remote testing. The tutorial assumes you already
have JMeter installed on all the systems. The way JMeter works is 1 master controller initiates the test on multiple
slave systems.

3.3 Terminology

Before we dive into the step-by-step instructions, it's a good idea to define the terms and make sure the definition is
clear.

 Master – the system running JMeter GUI, which controls the test.

Page No. 14
 Slave – the system running JMeter-server, which takes commands from the GUI and send requests to the
target system(s).
 Target – the webserver we plan to stress test.

3.4 Step-by-Step

1) On the slave systems, go to JMeter/bin directory and execute JMeter-server.bat (JMeterserver on unix). On
windows, you should see a dos window appear with “jre\[version]\bin\rmiregistry.exe”. If this doesn't
happen, it means either the environment settings are not right, or there are multiple JRE installed on the
system.

Note: [version] would be the jre version installed on the system.

a) Open JMeter-server.bat in a text editor.


b) Go to line 44 and find “:setCP”.
c) edit “START rmiregistry” to the full path. Example: “START
C:\j2sdk1.4.2\jre\bin\rmiregistry”.

2) On master system acting as the console, open windows explorer and go to JMeter/bin directory.

3) Open JMeter.properties in notepad or wordpad.

Page No. 15
4) Edit the line “remote_hosts=127.0.0.1”.

5) Add the IP address. For example, if I have JMeter server running on 192.168.0.10, 11, 12,13, and 14, the
entry would like this:
remote_hosts=192.168.0.10, 192.168.0.11, 192.168.0.12, 192.168.0.13, 192.168.0.14.

6) Start JMeter.

7) Open the test plan you want to use

3.5 Starting the Test

At this point, you are ready to start load testing. If you want to double check the slavesystems are working,
open JMeter.log in notepad. You should see the following in the log.JMeter.engine.RemoteJMeterEngineImpl:
Starting backing engine. If you do not see this message, it means jmeter-server did not start correctly. For tips on
debugging the issue, go to the tips section. There are two ways to initiate the test: a single system and all systems.

Start a single client

Page No. 16
1) Click Run at the top.
2) Select Remote start.
3) Select the IP address.

Start all clients

1) Click Run at the top.


2) Select Remote start all or use CRTL-Z.

Known Limitations:

There are some basic limitations for distributed testing. Here's the list of the known items in no specific order.

1. RMI cannot communicate across subnets without a proxy; therefore neither can JMeter without a proxy.

Page No. 17
2. Since JMeter sends all the test results to the controlling console, it is easy to saturate the network IO. It is a
good idea to use the simple data writer to save the results and view the file later with one of the graph
listeners.
3. Unless the server is a large multi-processor system, in most cases 1-2 clients is sufficient to overwhelm the
server.
4. A single JMeter client running on a 1.4-3 GHz CPU can handle 100-300 threads depending on the type of test.
The exception to the is web services. XML processing is CPU intensive and will rapidly consume all the CPU
cycles. As a general rule, the performance of XML centric applications will perform 4-10 slower than
applications using binary protocols.

Page No. 18
References–
 http://jmeter.apache.org/usermanual/jmeter_distributed_testing_step_by_step.pdf
 http://jmeter.apache.org/usermanual/

Page No. 19

You might also like