Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use the Parallel Controller in JMeter

DZone's Guide to

How to Use the Parallel Controller in JMeter

In this article we analyze one of Apache JMeter's extensions, the Parallel Controller, developed as a contribution to the open source community.

· Performance Zone ·
Free Resource

Sensu is an open source monitoring event pipeline. Try it today.

In this article we are going to analyze one of Apache JMeter's extensions, the Parallel Controller. This controller was developed by BlazeMeter as a contribution to the open source community, by Andrey Pokhilko and Artem Fedorov.

The Parallel Controller can be used to create parallel requests. Parallel requests are requests that are not executed one after the other, but simultaneously. For example, to handle AJAX requests or perform other simultaneous actions in a load script.

You can install the Parallel Controller by using the JMeter Plugins Manager. It is called the Parallel Controller & Sampler. We will also need a Dummy Sampler and a WebSocket plugin for this demonstration, which you can find in the JMeter Plugins Manager as well. We will use Websocket Samplers by Peter Doornbosch.

Now we will look at the Parallel Controller's features through several examples. Let's start with a simple example.

1. First, let's add the controller to the Test Plan. After adding a thread group:

Right click on Thread Group -> Add -> Logic Controller -> bzm - Parallel Controller

2. Also add two Dummy Samplers inside the controller and apply identical characteristics to both of them.

Right-click on the Parallel Controller -> Add -> Samplers -> jp@gc Dummy Sampler

The samplers are needed to emulate sending HTTP requests. You can add any Samplers according to your need. Now the script looks like this:

3. Run the test.

As you can see from the screenshot above, the Start Time for both requests is identical. This means the requests were sent simultaneously.

First important note: If you open the console, you will see that the number of running threads has become equal to two. That is, this controller creates two threads and runs them simultaneously.

If you have several samplers that need to work in one thread, you need to logically combine them. Otherwise, the parallel controller creates new threads for each of its child elements.

4. For example, you can use a Simple Controller. To add it:

Right click on Thread Group -> Add -> Logic Controller -> Simple Controller

Now, put the elements that should be combined under this controller.

Now the Parallel Controller will create 2 threads: the first for Dummy 1 and Dummy 2 (under the first Simple Controller) and the second for Dummy 3 and Dummy 4 (under the second Simple Controller). Besides, we added Dummy 5 after the Parallel Controller and it will be executed after these two groups finished executing.

As you can see in the GIF above, Dummy 1 and 2 have different starting times, and Dummy 5 was only executed after Dummy 4 execution.

Second important note: The next part of the test will not start until all the parallel threads have completed their work.

Now let's take a look at the two notes I highlighted, their downsides and possible solutions.

The first note is that the controller creates separate threads and executes them in parallel. Because of this, there might be a problem with samplers that interact with each other inside the thread.

For demonstration, we will use the WebSocket plugin. This plugin has a group of samplers that can be connected by the WebSocket session inside the thread.

If the session is created before a Parallel controller, and the work of sampler's group is performed inside the controller, we will get an error. The error states that the connection does not exist from the WS Sampler (that is located inside the controller). You can see this in the screenshot below.

If you have related elements in the test plan, they should all be placed inside the Parallel Controller.

The second note is that the next part of the test will not start until all parallel threads have completed their work.

I once had the following scenario: a user got a large amount of data via an HTTP request and received information via a web socket for a certain time. When the time ran out, the scenario stated that he should go to the next page.

This part of the test was as follows. In the test, the page to go to after time runs out is called "Last request."

The problem was that when time ran out, the thread that received the messages did quit, but the thread that received the data didn't finish running until it received all the data.

So, the thread will not go on to the next step until the data finishes loading. But this behavior is wrong. That is, it is necessary to somehow interrupt the execution of the second thread after the parallel thread completes.

Some samplers have a Timeout field. In my case, I used the HTTP request sampler and it has a Connection Timeout Field. This means that after the specified time the sampler will finish its work. But this solution is too narrow.

Another solution could be as follows. Let's say you have a different case, and in both threads, you have a group of requests that are executed in a loop. One thread finishes after receiving a certain message, and the second does not know about this message. The second should finish after the last iteration, and you can use the Groovy or BeanShell samplers to do that by setting the necessary value of the variable. For instance, using the vars.put("isEnd","true") command variable and 'isEnd' gets a 'true' value.

This situation will look like this:

Here we have two parallel threads: the first thread with the While controller and the second thread with the Loop controller (this controller replaces the message receiving). Inside the second thread, after the Loop controller, you set the value using the Groovy or BeanShell sampler. In the first thread, you check the value of this variable in the While controller's condition, for example, using the following condition:  ${__jexl3("${isEnd}"=="true")} .

There is also a problem that cannot be solved: you cannot add a parallel controller inside the parallel controller. An error will be received when executing. Just don't do that.

The Parallel Sampler is a light version of the controller, and is used to create Ajax requests or others parallel HTTP requests. The sampler has a simple interface and it is presented in the screenshot below. You just need to put in all parallel URLs.

The sampler will not finish execution until all references are processed. But if you want to use it for paralleling site pages, remember that the sampler executes all the links that are on the website page as well.

To sum up, the Parallel Controller is a good controller for requests paralleling, which is very useful for testing sites with parallel actions. For instance, when processing WebSocket requests, or for testing mobile applications, where there can be a huge number of parallel requests like heartbeat requests. In addition to the controller, we also have the Parallel Sampler that can used for creating parallel HTTP requests, which greatly facilitates the structure of the script and is well suited for paralleling Ajax requests.

That's it! If you want to learn more advanced JMeter check out our free JMeter academy.

You can also try out BlazeMeter, which enhances JMeter features. Just request a demo or put your URL in the box below and your test will start in minutes.

Sensu: workflow automation for monitoring. Learn more—download the whitepaper.

Topics:
dummy sampler ,parallel requests ,performance testing ,apache jmeter ,jmeter plugins ,parallel controller ,websocket plugin ,ajax requests ,jmeter ,load testing

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}