Powered By Blogger

Thursday, April 14, 2016

Jmeter Basics

  • Introduction:


WHY JMETER?


  • Open source license: JMeter is totally free,  allows developer use the source code for the development
  • Friendly GUI: JMeter is extremely easy to use and doesn't take time to get familiar with it
  • Platform independent: JMeter is 100% pure Java desktop application. So it can run on multiple platforms
  • Full multi-threading framework. JMeter allows concurrent and simultaneous sampling of different functions by a separate thread group
  • Visualize Test Result: Test result can be display in a different format such as chart, table, tree and log file
  • Easy installation: You just copy and run the *.bat file to run JMeter. No installation needed.
  • Highly Extensible: You can write your own tests. JMeter also supports visualization plugins allow you extend your testing
  • Multiple testing strategy: JMeter supports many testing strategies such as Load Testing, Distributed Testing, and Functional Testing.
  • Simulation: JMeter can simulate multiple users with concurrent threads, create a heavy load against web application under test
  • Support multi-protocol: JMeter does not only support web application testing, but also evaluate database server performance. All basic protocols such as HTTP, JDBC, LDAP, SOAP, JMS, and FTP are supported by JMeter
  • Record & Playback Record the user activity on the browser and simulate them in  web application using JMeter
  • Script Test: Jmeter can be integrated with Bean Shell & Selenium for automated testing.

Jmeter Installation:

Operating system Support for JMeter

JMeter is a pure Java application and should run correctly on any system that has a compatible Java implementation.
Here is the list of operating system compatible with JMeter.
Operating system
Java Virtual Machine
Architecture
Free BSD 9.0
Open JDK 6
amd64
Linux 2.4, 2.6, 3.1
Sun JDK 5, 6, 7 and Open JDK 6
i386, amd64
Mac OS
JDK6
Window XP
Sun JDK6, Sun JDK 7
32/64 bits
Window 7
Sun JDK 5, 6, 7.
32 bits

In this tutorial, we run JMeter on Window operating system

Steps to Install JMeter

Step 1) Install Java

Because JMeter is pure Java desktop application, it requires a fully compliant JVM 6 or higher. You can download and install the latest version of Java SE Development Kit.
After installation is finished, you can use the following procedure to check whether Java JDK is installed successfully in your system
  • In Window/Linux, go to Terminal
  • Enter command java -version
If Java runtime environment is installed successfully, you will see the output as figure below
If nothing displays, please re-install Java SE runtime environment

Step 2) Download Jmeter

As of this writing, the latest version of JMeter is Apache JMeter 2.9.You can download ithere
Choose the Binaries file (either zip or tgz) to download as shown in figure below

Step 3) Installation

Installation of JMeter is extremely easy and simple. You simply unzip the zip/tar file into the directory where you want JMeter to be installed. There is no tedious installation screen to deal with! Simple unzip and you are done!
Once the unzipping is done installation directory structure should look like as figure below
Given below is the description of the JMeter directories and its importance JMeter directory contains many files and directory
  • /bin: Contains JMeter script file for starting JMeter
  • /docs: JMeter documentation files
  • /extras: ant related extra files
  • /lib/: Contains the required Java library for JMeter
  • /lib/ext: contains the core jar files for JMeter and the protocols
  • /lib/junit: JUnit library used for JMeter
  • /printable_docs:

Step 4) Launch JMeter

You can start JMeter in 3 modes
  • GUI Mode
  • Server Mode
  • Command Line Mode

Start JMeter in GUI Mode

If you are using Window, just run the file /bin/jmeter.bat to start JMeter in GUI mode 
Following figure annotates the various components in the JMeter GUI

Start JMeter in Server Mode

Server mode is used for distributed testing. This testing works as client-server model. In this model, JMeter runs on server computer in server mode. On client computer, JMeter runs in GUI mode.
To start the server mode, you run the bat file bin\jmeter-server.bat as below figure

Start JMeter in command line mode

JMeter in GUI mode consumes much computer memory. For saving resource, you may choose to run JMeter without the GUI. To do so, use the following command options
This is a command line example
$jmeter -n -t testPlan.jmx - l log.jtl -H 127.0.0.1 -P 8000

Additional Packages

Based on your requirement, you will need one or more optional packages listed below.
  • Java Compiler
         Java Compiler allows developers to build JMeter source code and other JMeter plugins
  • SAX XML parser
         SAX is the Simple API for XML, originally a Java-only API. You can use SAX XML parser as an alternative to XML parser in JMeter
  • Email Support
         JMeter has extensive Email capabilities. It can send email based on test results and has a POP3(S)/IMAP(S) sampler. It also has an SMTP sampler.
  • JDBC driver
          If you want to test database server, you have to install JDBC driver

Use JMeter in Linux

  • Using JMeter in Linux is the same as in Window; you simply run the following shell script.
  • Run the script file jmeter (This file has no extension)- run JMeter (in GUI mode by default).
  • Run the script file jmeter-server - start JMeter in server mode (calls JMeter script with appropriate parameters)
  • jmeter.sh - very basic JMeter script with no JVM options specified.
  • mirror-server.sh - runs the JMeter Mirror Server in non-GUI mode
  • shutdown.sh - Run the Shutdown client to stop a non-GUI instance gracefully
  • stoptest.sh - Run the Shutdown client to stop a non-GUI instance abruptly

How Apache JMeter works

For using JMeter, you need to define your test plan. Which basically means, you have to specify
  • http or other urls to hit or sql queries to run
  • how to store/display the results
  • how many threads to run to create desired load/stress
In JMeter terms, these are called Threads, Samplers, Listeners etc.

So a complete test plan contains samplers, listeners etc. To create it, you right click on "Test Plan" on the left-hand pane of JMeter GUI and add required elements.

These terms are explained below briefly: 





JMeter's Test elements explained briefly :
Test ElementsDescription
Test PlanContainer of complete test.
Thread GroupA test thread (its name thread-group is misleading). You tell it what urls to hit or queries to run.
SamplerThe http urls/ sql queries to be stress tested
ListenerThey log the test urls and response and displays them graphically, or in tables etc
Logic ControllerThey customize how samplers (basically urls/queries) are run i.e. in sequential order, randomly, in loop etc
TimerThey help introduce fixed/random delay between urls/queries to better mimic real/practical scenarios
Config ElementConvenience objects to configure http/jdbc parameters

Usage of HTTP Sampler for a basic http test:

Let's write our first web test plan for testing an http url. So here are the steps. The screenshots are provided below.

The steps are as follows:

  1. Right click on Test Plan in left-hand side pane. And add a thread group. In the right-hand side pane, set a name for thread-group (eg. First Thread). Keep default values in other fields. There is no save or update button. Any changes you make are instantly applied when you move away from the eleent.
    [Image 1 and Image 2]

  2. Now, right-click on the thread-group created above, and add Sampler HTTP Request. In the the right-hand pane, add a name for the sampler (eg. Home Request), in the Server name or IP field add your webserver's hostname or IP Address. In the Path field, add the path of your servlet, jsp or html file that you want to test. Let other fields keep the default values.
    [Image 3 and Image 4]

  3. Now in order to view the test results, you need a Listener. We will use a tabular listener. Right click on Test Plan and add Listener View Results in Table. Give it a name and let other fields keep default value.
    [Image 5 and Image 6]

  4. You are done now. Just hit the start button (green arrow head) from the menu bar. In the View Results in Table page, the result will be displayed. It will contain timestamp, response time, response size in bytes etc.
    [Image 7]
Image 1 : Add a thread-group.Top of page


Image 2 : Give a name to thread-group.Top of page


Image 3 : Add an HTTP Request SamplerTop of page



Image 4 : Give a name to HTTP Sampler, add hostname and webpage path.Top of page


Image 5 : Add the listener "View Results in Table"Top of page


Image 6 : Give a name to listener and run the test.Top of page


Image 7 : Check result of test.

Introduction to Config Element

Config Element is a convenience element where you can store the common configuration values of all samplers like webserver's hostname or database url etc.

Example of HTTP Request Defaults Config Element (sample usage)

If you need to test dozens of http urls, then instead of adding the webserver's hostname or proxy details etc in all HTTP Request Samplers, you can put these in The Config Element HTTP Request Defaults. This will avoid repetition of data and also make the config manageable. Let's see an HTTP Config Element in action

Image 1 shows how to add an HTTP Config Element and Image 2 show how to configure 1.

Image 1: Adding HTTP Config Element


Image 2: Configuring HTTP Config element







CSV Data Set Config

You can parameterize the script to enter different login
credentials. This login information (e.g. Username, password) could be stored
in a text file, JMeter has an element that allows you to read different
parameters from that text file. It is "CSV Data Set Config", which is used to
read lines from a file, and split them into variables.


HTTP Cookie Manager: 

In Browser, your username and password will
be stored in your computer as cookies. Next time, when you visit 
same website, you don't need to do login again because your browser will
use your cookies as user data to login.


Http Request defaults: 

You are sending 100 HTTP requests to the server
google.com. You would have to manually enter server name = google.com
for all these 100 requests.
Instead, you could add a single HTTP request defaults with the
"Server Name or IP" field = google.com, No need to type 100 times!


Login Config Element vs CSV Data Config:

In Login Config only one users credentialscan be simulated where as in CSV Data set config multiple users credentials (values) can be read from a file.



Steps to Record a Script using Jmeter:


  1. Add a Thread group.
  2. Add an HTTP Request Default.
  3. Add HTTP(S) Test Script Recorder:
Select Work Bench, Add >Non-Test Elements >HTTP(S) Test Script Recorder

Settings to be done in Test Script Recorder:

  Port Setting-8080, Including or Excluding URL Patterns (Optional)
  (e.g. *.html, *.php, etc.), Then press Start Recording.

Configure Firefox To Use JMeter Proxy:

Open Firefox > Preferences > Advanced tab > Network > Connection
Setting > Select the "Manual proxy configuration > Set HTTP Proxy to
"localhost" and Port to "8080” > Check "Use this proxy server for all
 protocols“> OK.


CREDITS: As i learnt on my own from various tutorials, i would like to thank GURU99 and some other tutorials for the Knowledge.