How to use Response Assertion in JMeter

 HOME

What is Response Assertion

The response assertion control panel lets you add pattern strings to be compared against various fields of the request or response. The pattern strings are:

Contains, Matches: Perl5-style regular expressions

Equals, Substring: plain text, case-sensitive

You can also choose whether the strings will be expected to match the entire response, or if the response is only expected to contain the pattern. You can attach multiple assertions to any controller for additional flexibility.

What is Assertion Results

The Assertion Results visualizer shows the Label of each sample taken. It also reports failures of any Assertions that are part of the test plan.

Note – Assertion Results MUST NOT BE USED during load test as it consumes a lot of resources (memory and CPU). Use it only for either functional testing or during Test Plan debugging and Validation.

Create a Test Plan in JMeter by following below mentioned steps
Step 1 –  Add Thread Group

Select Test Plan on the tree

Add Thread Group                           

To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group

In the Thread Group control panel, enter Thread Properties as follows: 

Number of Threads: 1 – Number of users connects to the target website
Loop Count: 1 – Number of time to execute testing
Ramp-Up Period: 1 

Step 2 –  Adding JMeter elements  

The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send

Add HTTP Request Sampler

               To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request

Below mentioned are the values used in HTTP Request to perform the test

Name – HTTP Request 
Server Name or IP – localhost
Port – 8000
Method – GET
Path – /example

Step 3 – Add Assertions

Add Response Assertion

The response assertion control panel lets you add pattern strings to be compared against various fields of the request or response.

       To add: Right-click on HTTP Request and select: Add -> Assertions-> Response Assertions

Here, I have selected below options:-

Apply to : Main Sample only
Field to Test: Text Response
Pattern Matching Rules: Contains
Pattern To Test: Linda

Step 4 – Add Listeners
Add Assertion Results

The Assertion Results visualizer shows the Label of each sample taken. It also reports failures of any Assertions that are part of the test plan.

              To add: Right-click on Thread Group and select: Add -> Listener-> Assertion Results

View Result Tree 

View Result Tree show results of the user request in basic HTML format

         To add: Right click Test Plan, Add -> Listener -> View Result Tree

Step 5 – Save the Test Plan

           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be saved as .jmx format

Step 6  – Run the Test Plan

Click on Green Triangle as shown at the top to run the test

Step 7 – View the Execution Status in Assertion Results

Click on Assertion Results to see the status of Run. Successful request will like shown below

If the test fails, we will see message mentioning the error in the Assertion Results 

Step 8  – View the Execution Status in View Result Tree

  Click on View Result Tree to see the status of Run. Successful request will like shown below

View Result Tree with failed request looks like below

How to Download & Install Apache JMeter

HOME

In the previous tutorial, I have explained about Apache JMeter.  This tutorial explains how to download and install JMeter. 

JMeter is a 100% pure Java application, it requires Java installed on your machine.

I’m going to download JMeter Version 5.3 which needs Java 8 and above.

1) Install Java

Verify Java is installed on your machine or not.

Open a Command-Line console by clicking on the start menu then type cmd,  and type command java -version

If you don’t see java version , then you need to download and install the latest version of Java SE Development Kit. Download Java Platform (JDK) from here

2) Download Apache JMeter

The latest version of JMeter is Apache JMeter 5.3 now. You can download it here. Choose the Binaries file (either zip or tgz) to download as shown in the figure below

3) Install JMeter

  We don’t need to install JMeter, just unzip the JMeter downloaded folder. Go to the path where JMeter was downloaded and unzip the folder

 You should see the following files within the apache-jmeter-XX folder:

  • backups: contains the backups of JMX files (also known as JMeter projects) you edited within JMeter,
  • bin: contains the executable files to launch JMeter, and configuration files to tweak JMeter settings,
  • docs: JMeter’s documentation in HTML format,
  • extras: some useful side tools like excel sheets to compute graphs from JTL result files,
  • lib: JMeter’s libraries which are mostly Java JAR files,
  • printable_docs: documentation in printable format.

4) Launch JMeter

Double click on ApacheJMeter.jar 

The following figure shows the various components in the JMeter GUI

5) Upgrading JMeter

When a new JMeter version is available, the new JMeter version must be installed in a fresh new folder.

Please do not unzip a new JMeter version over a previous one.

Simply repeat the steps above to install a new version.

JMeter Tutorials

 HOME

 
Apache JMeter is a popular open source performance testing tool. It is 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
 
Chapter 1 Introduction of JMeter
Chapter 2 How to download and install Apache JMeter
Chapter 3 How to send GET Requests in JMeter
Chapter 4 How to send POST requests in JMeter
Chapter 5 JMeter Authorization with access token
Chapter 6  Install Apache JMeter in Ubuntu
Chapter 7  Constant Throughput Timer in JMeter
Chapter 8 How to generate Random Variables in JMeter
Chapter 9 How to use Response Assertion in JMeter

Introduction of JMeter

 HOME

What is Apache JMeter?

The Apache JMeter™ application is open source software, a 100% pure Java application designed to load test functional behavior and measure performance. JMeter is mainly used for testing Web application or FTP application but currently, it is applicable in functional testing, JDBC database connections, Web services, generic TCP connections and OS native processes. You can perform various testing activities like Performance, Load, Stress, Regression and Functional testing, in order to get accurate performance metrics against your web server.

JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it’s possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time). More detail about JMETER can be found here.

Apache JMeter features include:

  1. Open Source – JMeter is a 100% pure Java desktop application
  2. Platform independent –JMeter is written and developed using java, so it can run on any environment / workstation that accepts a Java virtual machine, for example – Windows, Linux, Mac, etc.
  3. Supports cross platform JMeter has unlimited testing capabilities allowing you to work both with local and server-based instances. The only thing you need to get started is Java (JRE) installed on the host.
  4. Simulation – JMeter can simulate multiple users by using virtual users or unique users in order to generate heavy load against web application under test.
  5. Supports Concurrency – Full multi-threading framework allows concurrent sampling by many threads and simultaneous sampling of different functions by separate thread groups.
  6. Easy to Use – 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.
  7. Test Result Visualization – JMeter has a set of tools for collecting and displaying results, like table, graph, tree, reports. The  The results can be saved in CSV, XML, HTML etc.
  8. Support various server typesJMeter is highly extensible and capable to load the performance test in different server types: HTTP, HTTPS, SOAP, JDBC, LDAP, JMS, POP3
  9. Works great even without UI. Test cases are executable in the command line which allows for their full integration into CI tools’ cases.

JMeter Workflow

JMeter sends requests to a target server by simulating a group of users. Server process the requests. Subsequently, data is collected to calculate statistics and display performance metrics of the target server through various formats.                                                                         

How to generate Random Variables in JMeter

HOME

This tutorial tells how  random variable can generate and pass as a part of a request in JMeter. Suppose, in the load test, there is a requirement to pass a random or different values to a specific parameter in the requests, how this can be achieve. One of the way is to pass different values from .csv file which contain different values and use them in the requests. Second way is to add Random Variable in JMeter, which generates random value at every run for requests. 

 

 Setting used in Random Variable are as follows:

Variable name: The name we are going to use to invoke the variable. “ReqCacheKey” in this example.

Output Format: The format for the variable. You can set the desired length of the number. I have set 0000000000 in order to work with a five-digit number. You can also use USER_000.

Minimum and Maximum: The range we want to set for the variable.

Seed: The seed for the random number generator. A seed is the first input that the number generation function receives to start the random generation. Here, it is ${__time()}. This will randomly generate the value like 1256078934, 9863457201

Per Thread: Is important to consider this option. If you set it as True, the threads will share the value. This means that there will be threads with the same value. If you require the variable to be different each time, this could cause problems for us. If you want to always generate a different value, you have to set it as False.

Create a Test Plan in JMeter by following below mentioned steps

Step 1 –  Add Thread Group

Select Test Plan on the tree

Add Thread Group                     

      To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group

In the Thread Group control panel, enter Thread Properties as follows: 

Number of Threads: 1 – Number of users connects to the target website
Loop Count: Infinite  – Number of time to execute testing
Ramp-Up Period: 1 
Duration: 5 sec

Step 2 –  Adding JMeter elements  

The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send

Add HTTP Request Sampler

               To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request

Below mentioned are the values used in HTTP Request to perform the test

Name – HTTP Request 
Server Name or IP – localhost
Port – 8010
Method – POST
Path – /demo/helloworld

Step 3 –  Add Random Variable

             To add: Right-click on Thread Group and select: Add -> Config Element-> Random Variable

Sample Request shown below. cacheKey variable is parameterized. ReqCacheKey in Random Variable will generate the random values, which will passed to parameter cacheKey present in the request body

Seed is ${__time()}. This generates the value randomly like 1256078934, 9863457201. If I keep Seed as blank, then when the tests run multiple time, it has the same set of random values (repetitive values), we don’t want same repetitive values, so seed is not blank

Step 4 – Adding Listeners to Test Plan

Listeners – They shows the results of the test execution. They can show results in a different format such as a tree, table, graph or log file

We are adding  View Result Tree listener

View Result Tree – View Result Tree show results of the user request in basic HTML format

         To add: Right click Test Plan, Add -> Listener -> View Result Tree

Step 5 – Save the Test Plan

           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be saved as .jmx format

Step 6  – Run the Test Plan

       Click on Green Triangle as shown at the top to run the test

Step 7 – View the Execution Status

             Click on View Result Tree to see the status of Run. Successful request will be of Green color in the Text Section

In the below image, we can see that cacheKey value is 1917449705 which is generated by Random Variable

Constant Throughput Timer in JMeter

What is Throughput?
Throughput is calculated as requests/unit of time. The time is calculated from the start of the first sample to the end of the last sample. This includes any intervals between samples, as it is supposed to represent the load on the server.
The formula is: Throughput = (number of requests) / (total time)
 
Suppose we want to run the load test with constant throughput in JMeter, then JMeter has a group of elements, which are called “Timers”.  And one of them has obvious title – “Constant Throughput Timer”. That is what we need. 
 
What is Constant Throughput Timer?
This timer allows us to keep total throughput constant. Constant Throughput Timer is only capable of pausing JMeter threads in order to slow them down to reach the target throughput so make sure you have enough threads in order to guarantee desired amount of requests per second. Also be aware that Constant Throughput Timer is precise enough only on minute level so you need to properly calculate ramp-up period and let your test run long enough. Of course, if the server is not able to handle such a load, the throughput will be lower. Throughput may decrease if other timers contradict the Constant Throughput timer.
Constant Throughput Timer will introduce random delays between requests in such a way that a load/stress of required throughput is sent to the application.
 
Screenshot of Constant Throughput Timer

                               Screenshot of Control Panel of Constant Throughput Timer

 
Suppose, we want to run 10 request per second for 5 sec. Then this can be achieved by 
Create a Test Plan in JMeter by following below mentioned steps
 
Step 1 –  Add Thread Group
Select Test Plan on the tree
Add Thread Group                     
      To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group
In the Thread Group control panel, enter Thread Properties as follows: 
Number of Threads: 1 – Number of users connects to the target website
Loop Count: Infinite  – Number of time to execute testing
Ramp-Up Period: 1 
Duration: 5 sec
 
Step 2 –  Adding JMeter elements  
The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send
Add HTTP Request Sampler
               To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request
Below mentioned are the values used in HTTP Request to perform the test
Name – HTTP Request 
Server Name or IP – localhost
Port – 8010
Method – POST
Path – /demo/helloworld
Step 3 –  Add Constant Throughput Timer
             To add: Right-click on Thread Group and select: Add -> Timer-> Constant Throughput Timer

Add Target Throughput: 600 (means 600 request in 60 sec, so 10 requests per sec)
Select Calculate Throughput based on – this thread only (More details about the option is present

Step 4 – Adding Listeners to Test Plan
Listeners – They shows the results of the test execution. They can show results in a different format such as a tree, table, graph or log file
We are adding  View Result Tree listener
View Result Tree – View Result Tree show results of the user request in basic HTML format
         To add: Right click Test Plan, Add -> Listener -> View Result Tree
We are adding  Summary Report listener
Summary Report – The summary report creates a table row for each differently named request in your test. This is similar to the Aggregate Report , except that it uses less memory.
         To add: Right click Test Plan, Add -> Listener -> Summary Report
Step 5 – Save the Test Plan
           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be saved as .jmx format
Step 6  – Run the Test Plan
       Click on Green Triangle as shown at the top to run the test
Step 7 – View the Execution Status
             Click on Summary Report to see the status of Run. As we can see in the summary report, 50 requests are executed as 10 requests per sec (10*5=50 requests).
 
 
 
 

Install Apache JMeter in Ubuntu

HOME

 

What is 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 can used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types.

In this tutorial, will discuss how to install JMeter in Ubuntu. For an example, I’ll use Ubuntu Desktop 18.04.

We need to install Java, before installing Apache JMeter. 

How to Install Java in Ubuntu Desktop 18.04

The command below will display the version of java installed on the system

java --version

The command below will download java on the system in headless mode

sudo apt-get install openjdk-11-jre-headless

How to Install Apache JMeter in Ubuntu Desktop 18.04

After Java is installed, copy the link to the archive with the latest version of Apache JMeter from the official site http://jmeter.apache.org/download_jmeter.cgi and download it:

wget http://apache.volia.net//jmeter/binaries/apache-jmeter-5.2.1.tgz

Extract the archive:

tar -xf apache-jmeter-5.2.1.tgz

How to run Apache JMeter in Ubuntu?

The command below will run the test named – PerfTest.jmx in Apache Ubuntu

./apache-jmeter-5.2.1/bin/jmeter.sh -n -t ./PerfTest.jmx

The output of JMeter Test will look like as below. 

We can see the summary of the test which include Total number of Requests processed, Total Execution Time, Throughput, Average time taken to process a request, Minimum time taken to process a request, maximum time taken to process a request and error percentage.

JMeter Authorization with access token

 
Authorization with dynamic access token is used to pass dynamic response content to subsequent requests. This is used to validate API authorization.
 
In this post, we will discuss about fetching access token (dynamic response) with the help of JSON Extractor and pass it as a parameter in subsequent request using BeanShell Assertion.
 
To achieve this, we need to create 2 Thread Groups- 
Thread Group 1 – To generate Access Token  
Thread Group 2 – To pass Access Token to Request 
 

How to setup JMeter to perform above test

Step 1 – Thread Group 1 – Thread Group – Authorization Token Generation
1) Add HTTP Request Sampler – In HTTP Request Control Panel, the Path field indicates which URL request you want to send
           To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request
Add valid credentials in the parameters section.
 
2) Add HTTP Head Manager – The Header Manager lets you add or override HTTP request headers, like can add Accept-Encoding,Accept,Cache-Control
             To add: Right-click on Thread Group and select: Add -> Config Element -> HTTP Read Manager
 
Add Authorization as Headers in Head Manager 
3) Add JSON Extractor – To extract the authentication token from the request , we are going to use JMeter JSON Extractor. The process of extracting a variable from a response works as mentioned below:
First, the server sends back a response ,then a post-processor, like the JSON Extractor is executed which extracts part of the response and put it into a variable like ${token}.
JMeter Json Extractor
               To add: Right-click on Thread Group and select: Add -> Post Processors -> JSON Extractor
 
The JSON extractor requires us to follow a few steps so we can process the JSON correctly
1) Name – JSON Exractor
2) Apply to – we will use the defaulted Main Sample Only. The option is:
               Main sample only – the assertion only applies to the main sample
3) Name of created variables – BEARER
4) JSON Path Expressions – access_token

4) Add BeanShell Assertion -An advanced assertion with full access to JMeter API. Java conditional logic can be used to set the assertion result.

                   To add: Right-click on Thread Group and select: Add -> Assertions -> BeanShell Assertions

                                           

Add below mentioned script in the Script section of BeanShell Assertion

Script – ${__setProperty(BEARER, ${BEARER})};

Step 2 – Thread Group 2 – Thread Group – Main Request

1) Add HTTP Request Sampler – Below mentioned are the values used in HTTP Request to perform the test
Add valid credentials in the parameters section.
2) Add HTTP Head Manager – We have previously extracted the token from Token Generation request. Now, it’s time to reuse it in headers section of HTTP Head Manager.
Below mentioned are the values used in Http Request to perform the test
Authorization = Bearer ${__property(BEARER)}
 
Step 3 – Adding Listeners to Test Plan
Listeners – They shows the results of the test execution. They can show results in a different format such as a tree, table, graph or log file
We have added  listeners – View Result Tree 
View Result Tree – View Result Tree show results of the user request in basic HTML format
                To add: Right click Test Plan, Add -> Listener -> View Result Tree

Step 4 – Save the Test Plan

           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be saved as .jmx format

Step 5  – Run the Test Plan

          Click on Green Triangle as shown at the top to run the test

Step 6 – View the Execution Status

             Click on View Result Tree to see the status of Run. Successful request will be of Green color in the Text Section
Here, we can see that Token Generation request is successfully processed.
This image shows that the Main Request is successfully executed too.
 

How to send POST requests in JMeter

HOME

 

 

How to send HTTP Request in JMeter?


We can perform GET as well as POST operation in JMeter. In this tutorial, we will only explain how we send POST HTTP request in JMeter. In the previous tutorial, I have explained how we can send GET request in JMeter.

Create a Test Plan in JMeter by following below mentioned steps


Step 1 –  Add Thread Group

  • Select Test Plan on the tree
  • Add Thread Group                                                                                                                               To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group
  • In the Thread Group control panel, enter Thread Properties as follows: We will take an example of row no 5.
    • Number of Threads: 5 – Number of users connects to the target website
    • Loop Count: 5  – Number of time to execute testing
    • Ramp-Up Period: 5 – It tells JMeter how long to delay before starting the next user. For example, if we have 5 users and a 5 -second Ramp-Up period, then the delay between starting users would be 1 second (5 seconds /5 users)
  • Step 2 –  Adding JMeter elements  

The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send
Add HTTP Request Sampler
               To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request

Below mentioned are the values use in HTTP Request to perform the test

  • Name – HTTP Request 
  • Server Name or IP – localhost
  • Port – 8000
  • Method – POST
  • Path – /demo/helloworld

Add HTTP Head Manager – The Header Manager lets you add or override HTTP request headers, like can add Accept-Encoding,Accept,Cache-Control

To add: Right-click on Thread Group and select: Add -> Config Element -> HTTP Read Manager
Below mentioned are the values use in Http Request to perform the test
Content-type = application/json
accept – application/json

Step 3 – Adding Listeners to Test Plan


Listeners – They shows the results of the test execution. They can show results in a different format such as a tree, table, graph or log file
We are adding View Result Tree listener
View Result Tree – View Result Tree show results of the user request in basic HTML format
To add: Right click Test Plan, Add -> Listener -> View Result Tree

Aggregate Report – It is almost same as Summary Report except Aggregate Report gives few more parameters like, “Median”, “90% Line”, “95% Line” and “99% Line”.

                To add: Right Click on Thread Group > Add > Listener > Aggregate Report

Step 4 – Save the Test Plan

           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be save as .jmx format.

Step 5  – Run the Test Plan

             Click on Green Triangle as shown at the top to run the test.

Step 6 – View the Execution Status

             Click on View Result Tree to see the status of Run. Successful request will be of Green color in the Text Section.

Click on Response data and Response Header to view other information about Response.

 Click on Aggregate Report Result to see the aggregated status of Run.

How to send GET Request in JMeter

HOME
 
 
What is 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 design for testing Web Applications but has since expanded to other test functions.
It can used to simulate a heavy load on a server, group of servers, network or object to test its strength or to analyze overall performance under different load types.
 
How to send GET HTTP Request in JMeter?
We can perform GET as well as POST operation in JMeter. In this tutorial, we will only explain how we can perform GET operation.
 
Create a Test Plan in JMeter by following below mentioned steps
Step 1 – Add Thread Group
 
  • Select Test Plan on the tree
  • Add Thread Group                                                                                           
  •           To add Thread Group: Right click on the “Test Plan” and add a new thread group: Add -> Threads (Users) -> Thread Group

In the Thread Group control panel, enter Thread Properties as follows: We will take an example of row no 5.

  • Number of Threads: 5 – Number of users connects to the target website
  • Loop Count: 5  – Number of time to execute testing
  • Ramp-Up Period: 5 – It tells JMeter how long to delay before starting the next user. For example, if we have 5 users and a 5 -second Ramp-Up period, then the delay between starting users would be 1 second (5 seconds /5 users)

Step 2 –  Adding JMeter elements

The JMeter element used here is HTTP Request Sampler. In HTTP Request Control Panel, the Path field indicates which URL request you want to send

Add HTTP Request Sampler

      To add: Right-click on Thread Group and select: Add -> Sampler -> HTTP Request.

Below mentioned are the values use in HTTP Request to perform the test

  • Name – HTTP Request 
  • Server Name or IP – localhost
  • Port – 8010
  • Method – GET
  • Path – /demo/helloworld/demo

Step 3 – Adding Listeners to Test Plan

Listeners – They shows the results of the test execution. They can show results in a different format such as a tree, table, graph or log file

We are adding  View Result Tree listener

View Result Tree – View Result Tree show results of the user request in basic HTML format

         To add: Right click Test Plan, Add -> Listener -> View Result Tree

Complete Test Plan will look like as shown below

Step 4 – Save the Test Plan

           To Save: Click File Select -> Save Test Plan as ->Give name of the Test Plan. It will be save as .jmx format

Sample .jmx File

Step 5  – Run the Test Plan

             Click on Green Triangle as shown below to run the test

Step 6 – View the Execution Status

             Click on View Result Tree to see the status of Run. Successful request will be of Green color in the Text Section

Sample of Failed Request. Failed request will be of Red color in View Result Tree under Text option. This screen sows the reason for the failure of the request like Connection refused here.