Performance Testing using Apache JMeter – Part 2

Introduction

In previous post about performance testing, we learned the basics of performance testing and JMeter tool. We learned that “performance testing tests how an application or resource performs under a given load“.

We also learned about performance matrices (we can measure performance in terms of):

  • Response time
  • Throughput (nos of requests kb/sec)
  • Reliability (percentage of errors)
  • Scalability (how previous matrics vary when changing the hardware or software configs of application)

We learned that we can generate load in terms of:

  • Number of users
  • Number of requests

Depending on how we apply the load there are different types of tests:

  • Load Test (validates the performance with specific load).
  • Stress Test (to know the performance limits of app).
  • Spike Test (to monitor the app behavior during change of load).
  • Endurance Test (to detect memory or connection leaks of app)

We also had a brief introduction of JMeter which is a multi-threaded, multi-protocol client that can execute complex test scenarios for performance testing. We also learned in previous post, how to install JMeter on windows machine.

Today we will put those learning in practice and write our first simple performance test for a REST API.

Getting Started with JMeter

When we first start the JMeter tool, we’ll be presented with a screen which looks like the following:

There are different termonologies and options which we can use in JMeter. We’ll focus on the basic items in this post which shall allow us to get started quickly.

Test Plan

This is the root element of a test, where its overall settings are specified and all other elements are controlled.

A Test Plan can be viewed as a container for running tests.

Some elements of a proper test plans are :

  • Thread groups
  • controllers
  • Timers
  • …others

In a test plan, it is a must to have at least one thread group.

Thread Group

Thread Groups are entry point of test. It must be a child of a test-plan.

It controls number of threads (users) JMeter will use to execute your test. If you are aware of Threading in C# or Java, that’s the same concept, here each thread represent a single user.

We can have more than one thread group each representing a test case.

Don’t worry if you do not get all of this just yet, it will become clearer as we move forward.

There many more items and configurations in JMeter, e.g. you can define variables, user data, logic controllers, view results etc., but we will not go into those details just yet and will cover those as we move forward.

Target Sample Application (REST API)

You can use JMeter to test variety of applications, written in many programming languages. For today’s demo, I will be using a very simple .NET Core Web API which has a very basic JWT Authentication mechanism in place.

If you are interested you can read more about the application on this post.

If you have your own API, I will suggest to use that one instead.

Here is the controller code for your reference:

This controller has two methods which we can target for performance testing.

also, the API is setup with two users as shown below (we will deal with those in next post, here it is for information only):

here is the Postman request for one of the API endpoint (this endpoint doesn’t require authentication):

Creating First Performance Test

We’ll start simple, we have an endpoint in our API which returns weather data (shown above). We will setup a simple thread group to test this endpoint.

We will be setting up following items:

  • Thread Group
    • HTTP Request Sampler
  • View Result in Table
  • Summary Report
  • View Result in Tree

Create a Thread Group

Right-click on the test plan and add a Thread Group as shown below:

Give it a name and keep the other properties without any changes:

Add an HTTP Request Sample

Next, right-click on newly created Thread-Group and add an HTTP Request as shown below

Now, configure this request with information from you API. This typically involves, URL, port number, HTTP Request and endpoint (path) info. see example below:

Add a Listener (View Result)

To see the output of our test, we can add a listener as shown below:

(Right-click the Thread Group and add a View Results in Table Listener)

You can add more listener if you want, e.g. I added two more listeners as show below:

Performance Test Execution

We have setup now a simple test, which make HTTP call to an API Endpoint and we also added few View Result items to see the output.

With all this setup we can now execute the test by clicking green button as shown below:

and here is the output of the test (Select View Result listener to see the results):

Increasing the Load (Number of Users)

Ok, we run the previous test which is configured for 1 user load. One way to increase the load is to increase number of users. We can do that easily in JMeter by going to Thread Group properties and update the following:

Run the test again and following is the result of execution:

This time, performance test executed with load of 10 users and you can see some information about the response time, status of the request etc.

Summary

In this post, we started setting up our tests using JMeter. We used an existing .NET Core API application (you can use your own API e.g. written in NodeJS e.g.) for testing purposes. We target one endpoint of the API and executed the test and saw the report.

You can download the sample JMeter file from this git repo.

Now you can add more tests in similar way e.g. add a new thread-group for another API endpoint. There are many other options in JMeter which we will explore in later posts. Let me know if you have some questions or comments. Till next time, Happy Coding.

My Recent Books

1 thought on “Performance Testing using Apache JMeter – Part 2”

Comments are closed.