Introduction to load testing with Apache JMeter
Tags: java, tutorial, testing
Published: 2019-10-14 07:05:09

---
title: Introduction to load testing with Apache JMeter
published: true
description: 
tags: java, tutorial, testing
cover_image: https://images.pexels.com/photos/931321/pexels-photo-931321.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260
---

Hello! In this post, I'd like to address an issue of the load testing. One of the best tools available is Apache JMeter. It is free, open-source and, thanks to its Java nature, cross-platform. Although, it has a bit strange interface, so this article serves also as a quickstart to JMeter. In this post, we'd observe what is a load testing and how it is different from performance testing; how JMeter works and how to compose your tests; and finally - we would test a very simple server, written with plain Vert.x.

## What is a load testing?

Before we would start with load tests, let quickly point out what is a load testing itself. First thing first, we should remember, that load testing is a _non-functional testing_: it refers to aspects of the software that may not be related to a specific function or user action. We use _load testing_ in order to study app's behaviour under a specific load. We need to distinguish load testing and performance testing. We use _performance testing_ to analyze overall app's performance under a heavy load.

## Introduction to JMeter

In this section we would quickstart with Apache JMeter. I assume, that you already have it installed on your machine. If you don't have it, you can download it on [Apache Foundation website](https://jmeter.apache.org/download_jmeter.cgi). But keep in mind, that it requires at least Java 8 to run. 

### Getting started with JMeter

For start, let just quickly observe JMeter's interface. When you run it, you would see a similar window as on the screenshot below:

![JMeter UI](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-general-window.png)

I've divied the JMeter window on 3 areas:

1. __Test plan area__ = this is a tree-based structure, where you can manage your test's structure, add/remove items (see next about them)
2. __Toolbar__ = this bar contains most important actions, applied to the test - save/open a saved test, run, etc.
3. __Element area__ = in this part you can configure an individual item (thread group, elements etc.)

Now, let see what JMeter offers to us in terms of test plan structure.

### Test plan structure

JMeter operates its own terminology, that you should be familiar with. One of key concepts here is a _test plan_. Test plan is a root structure of load tests and represents what to test and how to go about it. It can include _thread groups_, that control the number of threads JMeter will use during the test. Thread groups include different _elements_, for example _samplers_ (used to send requests to server; for example HTTP or FTP requests), _logic controllers_ (to define logical conditions; if/for etc.), _listeners_ (used to visualize tests output in the form of tables, graphs, trees, or simple text in some log files). These elements are most used, but not only ones. There also others, like _assertions_, _configuration elements_, _timers_ etc., that you can use in your tests. Also, it is worth to remember an execution order of 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)

You can find a more detailed description of test plan elements in this [Tutorialspoint tutorial](https://www.tutorialspoint.com/jmeter/jmeter_test_plan_elements.htm). In my turn, I would not waste your time - so let move directly to the example API.

## Example API

Writing a REST API is not a subject of this post, so we will use a very simple API. For this example, we even would not use any external dependencies - Vert.x Core package is more than enough. We just need to have a dummy endpoint, nothing more. Add this code:

```java
public class DummyService extends AbstractVerticle {

    private long start;

    @Override
    public void start() throws Exception {
        HttpServer server = vertx.createHttpServer();
        start = System.currentTimeMillis();
        server.requestHandler(req->{
            long stop = System.currentTimeMillis();
            long duration = stop - start;
            req.response().end("Service is running "+duration+" ms");
        });
        server.listen(8800, "localhost");
    }

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new DummyService());
    }

}
```

Run this and access in your browser, to check that everything is up and running:

![](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-firefox.png)

Now, we have a dummy endpoint, let write a simple load test with Apache JMeter.

## How to do load testing with JMeter?

In this section we would create a simple load test with Apache JMeter. You should perform following steps:

### Step 1. Create new Thread Group

![Create new thread group in Apache JMeter](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-step-1.png)

To start load testing, we need create new thread group. Right-click on _Test Plan_ and select _Add > Threads (Users) > Thread Group_. Here we can specify following parameters:

* Number of threads = 10
* Ramp-Up period = 1
* Loop count = 100

### Step 2. Set up configuration

![Setup test configuration in Apache JMeter](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-step-2.png)

Next step is to set up config for our load test. Our dummy api is running as _localhost:8800_, therefore we should tell JMeter where to find it. Right click on __Thread Group__ and select _Add > Config Element > Http Request Default_. Here we add:

* Protocol = http
* Server name = localhost
* Port = 8800
* Path = /

### Step 3. Add HTTP Request

![Add HTTP Request in Apache JMeter](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-step-3.png)

We can now add a _sampler_ - in our case HTTP Request. We predefined everything in the previous step, so nothing to add here. Although, in more complex tests, of course you have to specify it. Leave everything blank for now.

### Step 4. Create a listener

![Create a listener in Apache JMeter](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-step-4.png)

Finally, add a _listener_ - an element to show results. Go and add _Add > Listener > Summary Report_. The summary report creates a table row for each differently named request in your test. With this component, you also can save data as CSV.

### Step 5. Run a test

![Run Apache JMeter test](https://www.mednikov.net/wp-content/uploads/2019/10/jmeter-step-5.png)

And now, you can run a load test (__NB__ don't forget to save the test). We received a data table, that we can interpret:

* Label = name of the HTTP Request sampler
* Samples = number of virtual users per request
* Average = average time taken by all the samples to execute specific label
* Min =  the shortest time taken by a sample for specific label.
* Max = the longest time taken by a sample for specific label
* St.Dev = a set of exceptional cases which were deviating from the average value of sample response time.
* Error = a percentage of Failed requests per Label.
* Throughtput = a number of request that are processed per time unit(seconds, minutes, hours) by the server

I recommend you to observe the documentation for [summary report](https://jmeter.apache.org/usermanual/component_reference.html#Summary_Report) or this post on [understanding a summary report](http://www.testingjournals.com/understand-summary-report-jmeter/). 

## Conclusion

In this post we observed load testing of [Java web applications](https://www.codesity.tech). One of the best tools available for this is Apache JMeter. It is free, open-source and, thanks to its Java nature, cross-platform. In this post, we have checked what is a load testing and how it is different from performance testing; how JMeter works and how to compose your tests; and finally - we tested a very simple server, written with plain Vert.x Core.

## References

* Bernhard Knasmüller. _How to Use JMeter to Performance Test a REST API_ (2019), [read here](https://knasmueller.net/how-to-use-jmeter-to-performance-test-a-rest-api)
* Mutiara Hana. _Performance Testing with JMeter for Dummies_ (2018), Medium, [read here](https://medium.com/skyshidigital/performance-testing-with-jmeter-for-dummies-541ea9171464)
* Ragnar Lonn. _When should I start load testing?_, TechBeacon, [read here](https://techbeacon.com/app-dev-testing/when-should-i-start-load-testing)
* Rishil Bhatt. _Understand and Analyze Summary Report in Jmeter_ (2017), [read here](http://www.testingjournals.com/understand-summary-report-jmeter/)