jMeter Tutorial - Web Test Plan

Let's build a simple test plan which tests a web page. We will write a test plan in Apache JMeter so that we can test the performance of one web page say a page is shown by the URL:http://www.testinganswers.com/.
Start JMeter

Open the JMeter window by clicking on /home/deepak/apache-jmeter-2.9/bin/jmeter.sh. The JMeter window will appear as below: 



This is a JMeter window having nothing added yet. Details of the above window are:

  • Test Plan node is where the real test plan is kept.
  • Workbench node is where the temporary stuff is kept.

Rename Test Plan

Change the name of the test plan node to Sample Test in the Name text box. You have to change focus to workbench node and back to Test Plan node to see the name getting reflected.




Add Thread Group

Now we will add our first element in the window. We will add one Thread Group, which is a placeholder for all other elements like Samplers, Controllers, Listeners. We need one so we can configure the number of users to simulate.

In JMeter, all the node elements are added by using the context menu. You have to right-click the element where you want to add a child element node. Then choose the appropriate option to add.

Right click on Sample Test(our Test Plan )> Add> Threads(Users)> Thread Group. Thread Group will get added under the Test Plan (Sample Test) node.



We will name Thread Group as Users. For us this element means Users visiting the TutorialsPoint Home Page. 




Add Sampler

Now we have to add one Sampler in our Thread Group (Users). As done earlier for adding Thread group, this time we will open the context menu of the Thread Group (Users) node by right-clicking and we will add HTTP Request Sampler by choosing Add > Sampler> HTTP request option.



This will add one empty HTTP Request Sampler under the Thread Group (Users) node. Let us configure this node element:



  • Name: We will change the name to reflect the action what we want to achieve. We will name it as Visit TutorialsPoint Home Page
  • Server Name or IP: Here we have to type the web server name. In our case it is www.testinganswers.com. (http:// part is not written this is only the name of the server or its IP)
  • Protocol: We will keep this blank, which means we want HTTP as the protocol.
  • Path: We will type path as / (slash). This means we want the root page of the server.

Add Listener

We will add a listener. Let us add View Results Tree Listener under the Thread Group (User) node. This will ensure that the results of the Sampler will be available to view in this Listener node element. Open the context menu and Right click on Thread Group(Users) choose Add > Listener > View Results Tree option to add the listener.



Run the Test Plan

Now with all the setup, let's execute the test plan. With the configuration of the Thread Group (Users), we have kept it all default values. This means JMeter will execute the sampler only once. It will be like a single user and only one time.

This is similar to like a user visiting a web page through the browser, only here we are doing that through JMeter sampler. We will execute the test plan using Run > Start option.

Apache JMeter asks us to save the test plan in a disk file before actually starting the test. This is important if we want to run the test plan again and again. If we say not to save by clicking No option it will run without saving. 



View Output

We have kept the setting of the thread group as single thread (that means one user only) and loop for 1 time (that means run only one time), hence we will get the result of one single transaction in the View Result Tree Listener. 



Details of the above result are:

  • Green color against the name Visit TutorialsPoint Home Page indicates success.
  • JMeter has stored all the headers and the response sent by the web server and ready to show us the result in many ways.
  • The first tab is Sampler Results. It shows JMeter data as well as data returned by the web server.
  • The second tab is Request, where all the data which was sent to the web server as part of the request is shown.  

  • The last tab is the Response data. In this tab, the listener shows the data received from the server as it is in text format.


This is just a simple test plan which executes only one request. But JMeter's real strength is in sending the same request as many users are sending it. To test the web servers with multiple users we will have to change the Thread Group (Users) settings.


jMeter Tutorial - Test Plan Elements

A JMeter Test Plan comprises of test elements which are discussed below. A Test Plan would comprise at least one Thread Group. Within each Thread Group we may place a combination of one or more of other elements: Sampler, Logic Controller, Configuration Element, Listener, and Timer. Each Sampler can be preceded by one or more Pre-processor element, followed by Post-processor element, and/or Assertion element. Let's see each of these elements in detail: 

ThreadGroup

Thread Group elements are the beginning points of your test plan. As the name suggests, the thread group elements control the number of threads JMeter will use during the test. We can also control the following via the Thread Group:

  • By setting the number of Threads.
  • By setting the Ramp Up Time
  • By setting the number of test iterations.

The Thread Group Control Panel looks like this:



Details of each component on the above panel are:
  1. Action to be taken after a Sampler error: In case any error occurs during test execution you may let the test either:
  • Continue to the next element in the test
  • Stop Thread to stop the current Thread.
  • Stop Test completely, in case you want to inspect the error before continue running.
  1. Number of Threads: Simulates the number of user(s) or connection(s) to your server application.
  2. Ramp-Up Period: Defines how long it will take JMeter to get all threads running.
  3. Loop Count: Defines the number of times to execute the test.
  4. Scheduler checkbox Once selected, the Scheduler Configuration section will appear at the bottom of the control panel.
  5. Scheduler Configuration You can configure the start and end time of running the test.
Controllers

JMeter has two types of Controllers: Samplers and Logic Controllers.
Samplers

Samplers allow JMeter to send specific types of requests to a server. They simulate a user's request for a page from the target server. For example, you can add a HTTP Request sampler if you need to perform a POST, GET, DELETE on a HTTP service

Some useful samplers are:

  • HTTP Request
  • FTP Request
  • JDBC Request
  • Java Request
  • SOAP/XML Request
  • RPC Requests

An HTTP Request Sampler Control Panel looks like the following figure:



jMeter Tutorial - Build Test Plan

What is a Test Plan? 


A Test Plan defines and provides a layout of how and what to test. For example the web application as well as the client server application. It can be viewed as a container for running tests. A complete test plan will consist of one or more elements such as thread groups, logic controllers, sample-generating controllers, listeners, timers, assertions, and configuration elements. A test plan must have at least one thread group. We shall discuss these elements in detail in the next chapter Test Plan Elements. 


Follow the below steps to write a test plan: 

Start the JMeter window

Open the JMeter window by clicking on /home/apache-jmeter-2.9/bin/jmeter.sh. The JMeter window will appear as below: 



This is a JMeter window having nothing added yet. Details of the above window are: 



  • Test Plan node is where the real test plan is kept.
  • Workbench node simply provides a place to temporarily store test elements while not in use, for copy/paste purposes. When you save your test plan, WorkBench items are not saved with it.

Add/Remove Elements
Elements (which will be discussed in the next chapter Test Plan Elements) of a test plan can be added by right clicking on the Test Plan node and choosing a new element from the "add" list.

Alternatively, elements can be loaded from file and added by choosing the "merge" or "open" option.

For example let's add a Thread Group element to a Test Plan as shown below:






To remove an element, make sure the element is selected, right-click on the element, and choose the "remove" option.




Loading and Saving Elements


To load an element from file, right click on the existing tree element to which you want to add the loaded element, and select the "merge" option. Choose the file where your elements are saved. JMeter will merge the elements into the tree.



To save tree elements, right click on an element and choose the Save Selection As ... option. JMeter will save the element selected, plus all child elements beneath it. By default JMeter doesn't save the elements, you need to explicitly save it as mentioned earlier. 

Configuring Tree Elements

Any element in the Test Plan can be configured in the controls present in JMeter's right-hand frame. These controls allow you to configure the behavior of that particular test element. For example the Thread Group can be configured for number of users, ramp up period etc as below:




Saving the Test Plan

You can save an entire Test Plan either by using Save or "Save Test Plan As ..." from the File menu.



Running a Test Plan

You can run your Test Plan choosing Start (Control + r) from the Run menu item. When JMeter is running, it shows a small green box at the right hand end of the section just under the menu bar.

 




The numbers to the left of the green box are the number of active threads / total number of threads. These only apply to a locally run test; they do not include any threads started on remote systems when using client-server mode. 



Stopping a Test

You can stop your test in two ways:

Using Stop (Control + '.'). This stops the threads immediately if possible. 


Using Shutdown (Control + ','). This requests the threads to stop at the end of any current work.

jMeter Tutorial - Overview

What is JMeter?

JMeter is a software allowing to load test or performance oriented business (functional) test on different protocols or technologies. Stefano Mazzocchi of the Apache Software Foundation was the original developer of JMeter. He wrote it primarily to test the performance of Apache JServ (Now called as Apache Tomcat project). Apache later redesigned JMeter to enhance the GUI and to add functional-testing capabilities.

This is a Java desktop application with a graphical interface using the Swing graphical API, can therefore run on any environment / workstation accepting a Java virtual machine, for example: Windows, Linux, Mac, etc.

The protocols supported by JMeter are:
  • Web: HTTP, HTTPS sites 'web 1.0' web 2.0 (ajax, flex and flex-ws-amf)
  • Web Services: SOAP / XML-RPC
  • Database via JDBC drivers
  • Directory: LDAP
  • Messaging Oriented service via JMS
  • Service: POP3, IMAP, SMTP
  • FTP Service 

JMeter Features

Following are some of the features of JMeter:
  • Its free. Its an open source software.
  • It has simple and intuitive GUI.
  • JMeter can load and performance test many different server types: Web - HTTP, HTTPS, SOAP, Database via JDBC, LDAP, JMS, Mail - POP3
  • It is platform-independent tool. On Linux/Unix, JMeter can be invoked by clicking on JMeter shell script. On Windows it can be invoked by starting the jmeter.bat file.
  • It has full Swing and lightweight component support (precompiled JAR uses packages javax.swing.* ).
  • JMeter store its test plans in XML format. This means you can generate a test plan using a text editor.
  • It's full multi-threading framework allows concurrent sampling by many threads and simultaneous sampling of different functions by separate thread groups.
  • It is highly Extensible.
  • Can also be used to perform automated and functional testing of your application.

How JMeter Works?

JMeter simulates a group of users sending requests to a target server, and return statistics that show the performance/functionality of the target server / application via tables, graphs etc. The figure below depicts this process:


jMeter Interview Questions

Q.1: What is JMeter? 

A: JMeter is one of the Java tools which is used to perform load testing client/server applications. Apache JMeter is open source software, a 100% pure Java desktop application designed to load test functional behavior and measure performance of the application. It was originally designed for testing Web Applications but has since expanded to other test functions.

Q.2: What is Performance Testing? 

A: This test sets the ‘best possible’ performance expectation under a given configuration of infrastructure. It also highlights early in the testing process if changes need to be made before application goes into production.

Q.3: What is Load Test? 

A: This test is basically used for exercising\discovering the system under the top load it was designed to operate under.

Q.4: What is Stress Test? 

A: This test is an attempt to break the system by overwhelming its resources.

Q.5: What are the protocols supported by JMeter?

A: The protocols supported by JMeter are: 
1. Web: HTTP, HTTPS sites 'web 1.0' web 2.0 (ajax, flex and flex-ws-amf)
2. Web Services: SOAP / XML-RPC
3. Database via JDBC drivers
4. Directory: LDAP
5. Messaging Oriented service via JMS
6. Service: POP3, IMAP, SMTP
7. FTP Service

Q.6: List some of the features of JMeter.
 

A: Following are some of the features of JMeter:
1. Its free. Its an open source software.It has simple and intuitive GUI.
2. JMeter can load and performance test many different server types: Web - HTTP, HTTPS, SOAP, Database via JDBC, LDAP, JMS, Mail - POP3
3. It is platform-independent tool. On Linux/Unix, JMeter can be invoked by clicking on JMeter shell script. On Windows it can be invoked by starting the jmeter.bat file.
4. It has full Swing and lightweight component support (precompiled JAR uses packages javax.swing.* ).
5. JMeter store its test plans in XML format. This means you can generate a test plan using a text editor.
6. It's full multi-threading framework allows concurrent sampling by many threads and simultaneous sampling of different functions by separate thread groups.
7. It is highly Extensible.
8. Can also be used to perform automated and functional testing of your application. 

Q.7: What is a Test Plan in JMeter?


A: A Test Plan defines and provides a layout of how and what to test. For example the web application as well as the client server application. It can be viewed as a container for running tests. A complete test plan will consist of one or more elements such as thread groups, logic controllers, sample-generating controllers, listeners, timers, assertions, and configuration elements. A test plan must have at least one thread group.
Q.8: List some of the test plan elements in JMeter.
A: Following is a list of some of the test plan elements:

1. ThreadGroup
2. Controllers
3. Listeners
4. Timers
5. Assertions
6. Configuration Elements
7. Pre-Processor Elements
8. Post-Processor Elements

Q.9: What is Thread Group?

A: Thread Group elements are the beginning points of your test plan. As the name suggests, the thread group elements control the number of threads JMeter will use during the test.

Q.10: What are Controllers and its types?

A: JMeter has two types of Controllers:

Samplers Controllers : Samplers allow JMeter to send specific types of requests to a server. They simulate a user's request for a page from the target server. For example, you can add a HTTP Request sampler if you need to perform a POST, GET, DELETE on a HTTP service

Logical Controllers : Logic Controllers let you control order of processing of Samplers in a Thread. Logic Controllers can change the order of request coming from any of their child elements. Some examples are: ForEach Controller, While Controller, Loop Controller, IF Controller, Run Time Controller, Interleave Controller, Throughput Controller, Run Once Controller. 

Q.11: What is Configuration element?

A: Configuration Elements allow you to create defaults and variables to be used by Samplers. They are used to add or modify requests made by Samplers.

They are executed at the start of the scope of which they are part, before any Samplers that are located in the same scope. Therefore, a Configuration Element is accessed only from inside the branch where it is placed.
Q12: What are Listeners?
A: Listeners let you view the results of Samplers in the form of tables, graphs, trees or simple text in some log files. They provide visual access to the data gathered by JMeter about the test cases as a Sampler component of JMeter is executed.

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.

Q.13: What are Pre-Processor and Post-Processor elements?

A: A Pre-Procesor is something that will happen before a sampler executes. They are often used to modify the settings of a Sample Request just before it runs, or to update variables that are not extracted from response text.

A Post Processor executes after a sampler finishes its execution. This element is most often used to process the response data, for example, to retrieve particular value for later use.

Q.14: What is the execution order of Test Elements

A: Following is the execution order of the test plan elements:

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)
 
Q.15: How do you ensure re-usability in your JMeter scripts?
A: 
1. Using config elements like "CSV Data Set Config", "User Defined Variables", etc for greater data reuse.
2. Modularizing shared tasks and invoking them via a "Module Controller".
3.Writing your own BeanShell functions, and reusing them.
 
Q.16: Are the test plans built using JMeter OS dependant?
A: Test plans are usually saved in thr XML format, hence they have nothing to do with any particular OS. You can run those test plans on any OS where JMeter can run. 

Q.17: What are the monitor tests?

A: Uses of monitor tests are:

1. Monitors are useful for a stress testing and system management.
2. Used with stress testing, the monitor provides additional information about server performance.
3. Monitors makes it easier to see the relationship between server performance and response time on the client side.
4. As a system administration tool, the monitor provides an easy way to monitor multiple servers from one console.

Q.18: What are JMeter Functions?

A: JMeter functions are special values that can populate fields of any Sampler or other element in a test tree. A function call looks like this: 

${__functionName(var1,var2,var3)}


Q.19: Where can functions and variables be used?

A: Functions and variables can be written into any field of any test component.
 
Q.20: What are regular expressions in JMeter?
A: Regular expressions are used to search and manipulate text, based on patterns. JMeter interprets forms of regular expressions or patterns being used throughout a JMeter test plan, by including the pattern matching software Apache Jakarta ORO.
Q.21: How can you reduce resource requirements in JMeter?

A: Below are some suggestion to reduce resource requirements:

1. Use non-GUI mode: jmeter -n -t test.jmx -l test.jtl.
2. Use as few Listeners as possible; if using the -l flag as above they can all be deleted or disabled.
3. Disable the “View Result Tree” listener as it consumes a lot of memory and can result in the console freezing or JMeter running out of memory. It is, however, safe to use the “View Result Tree” listener with only “Errors” checked.
4. Rather than using lots of similar samplers, use the same sampler in a loop, and use variables (CSV Data Set) to vary the sample. Or perhaps use the Access Log Sampler.
5. Don't use functional mode.
6. Use CSV output rather than XML.
7. Only save the data that you need.
8. Use as few Assertions as possible.
9. Disable all JMeter graphs as they consume a lot of memory. You can view all of the real time graphs using the JTLs tab in your web interface.
10. Do not forget to erase the local path from CSV Data Set Config if used.
11. Clean the Files tab prior to every test run.