Practical explanation of JMeter distributed pressure testing based on Docker

        A JMeter instance may not generate enough load to stress test your application. As shown on this site, one JMeter instance will be able to control many other remote JMeter instances and create a greater load on your application. JMeter uses Java RMI [Remote Method Invocation] to interact with objects in a distributed network. The communication between the JMeter master station and the slave station is shown in the figure below:

We need to open 2 ports for each Slave/Server.

Server_port=1099
server.rmi.localport=50000

Open a port on the client machine and let the slave machine send the results to the master machine.

client.rmi.localport=60000

By running multiple instances of JMeter on multiple machines as servers, we can generate as much load as needed.

Docker
What is the use of docker here?

Docker is a bit like a virtual machine. But unlike a virtual machine, instead of creating a complete virtual operating system, Docker allows applications to use the same Linux kernel as the system they are running on, requiring only that the applications be shipped with something not already running on the host machine. This provides a huge performance boost and reduces application size

Docker is an infrastructure manager. It can package a software and all its dependencies into a container to run. You can deploy software packaged as a docker image to any machine with docker installed. It separates software from hardware, so developers can rest assured that applications will run on any machine, regardless of whether that machine has any customized settings that may be different from the machine used to write and test the code.

Role of Docker in JMeter distributed testing
If we look at the above setup - to do distributed load testing - we need 1 master and N slaves to generate huge load. Each JMeter slave machine requires a specific version of Java and JMeter to be installed. The specific port should be opened and the JMeter server should be running, ready and waiting for instructions from the master.

Setting up some machines manually may seem easy. What if we want to do this for 50, 100, 1,000 machines? Imagine what will happen if we need to upgrade the JMeter version on all machines in the future? This is why docker appears.

We basically set up the entire infrastructure for JMeter distributed testing in a file called a Dockerfile. Examine these dockerfiles and read the comments to understand what each step does.

Dockerfile is used for JMeter basics:
In distributed testing, all environments must have the same version of Java, JMeter, plug-ins, etc. The only difference between a master and a slave is the exposed ports and running processes. So, let us create a Docker file with all the common steps for master and slave. Let's call it the jmbase image. We need to do the following to build our base image.

We need Java8 - so let's open the jdk-8-jre slim version to keep the size as small as possible.
We may need some utilities such as wget, unzip, telnet, etc. So let's install them.
We need the latest version of JMeter. Create a variable for the version - this will make future maintenance easier.
Add a folder containing all plugins.
Add a folder containing sample tests.

  • <span style="color:#232323"><code class="language-dockerfile"><span style="color:#008000"># Use Java 8 slim JRE</span>
    <span style="color:#0000ff">FROM</span> openjdk:<span style="color:#880000">8</span>-jre-slim
    <span style="color:#0000ff">MAINTAINER</span> TestAutomationGuru
     
    <span style="color:#008000"># JMeter version</span>
    <span style="color:#0000ff">ARG</span> JMETER_VERSION=<span style="color:#880000">3.3</span>
     
    <span style="color:#008000"># Install few utilities</span>
    <span style="color:#0000ff">RUN</span> apt-get clean && \
        apt-get update && \
        apt-get -qy install \
                    wget \
                    telnet \
                    iputils-ping \
                    unzip
     
    <span style="color:#008000"># Install JMeter</span>
    <span style="color:#0000ff">RUN</span>   <span style="color:#0000ff">mkdir</span> /jmeter \
          && <span style="color:#0000ff">cd</span> /jmeter/ \
          && wget https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-<span style="color:#008000">$JMETER_VERSION</span>.tgz \
          && tar -xzf apache-jmeter-<span style="color:#008000">$JMETER_VERSION</span>.tgz \
          && <span style="color:#0000ff">rm</span> apache-jmeter-<span style="color:#008000">$JMETER_VERSION</span>.tgz
     
    <span style="color:#008000"># ADD all the plugins</span>
    <span style="color:#0000ff">ADD</span> jmeter-plugins/lib /jmeter/apache-jmeter-<span style="color:#008000">$JMETER_VERSION</span>/lib
     
    <span style="color:#008000"># ADD the sample test</span>
    <span style="color:#0000ff">ADD</span> sample-test sample-test
     
    <span style="color:#008000"># Set JMeter Home</span>
    <span style="color:#0000ff">ENV</span> JMETER_HOME /jmeter/apache-jmeter-$JMETER_VERSION/
     
    <span style="color:#008000"># Add JMeter to the Path</span>
    <span style="color:#0000ff">ENV</span> PATH $JMETER_HOME/bin:$PATH</code></span>

Dockerfile for JMeter client/master
Master dockerfile should inherit from the base image and should expose port 60000:

<span style="color:#232323"><code class="language-dockerfile"><span style="color:#008000"># Use vinsdocker base image</span>
<span style="color:#0000ff">FROM</span> vinsdocker/jmbase
<span style="color:#0000ff">MAINTAINER</span> TestAutomationGuru
 
<span style="color:#008000"># Ports to be exposed from the container for JMeter Master</span>
<span style="color:#0000ff">EXPOSE</span> <span style="color:#880000">60000</span></code></span>

 

Dockerfile for JMeter Server / Slave:

The server docker file should inherit from the base image and should expose ports 1099 and 50000. jmeter-server should be running

<span style="color:#232323"><code class="language-dockerfile"><span style="color:#008000"># Use vinsdocker base image</span>
<span style="color:#0000ff">FROM</span> vinsdocker/jmbase
<span style="color:#0000ff">MAINTAINER</span> TestAutomationGuru
 
<span style="color:#008000"># Ports to be exposed from the container for JMeter Slaves/Server</span>
<span style="color:#0000ff">EXPOSE</span> <span style="color:#880000">1099</span> <span style="color:#880000">50000</span>
 
<span style="color:#008000"># Application to run on starting the container</span>
<span style="color:#0000ff">ENTRYPOINT</span> <span style="color:#008000">$JMETER_HOME</span>/bin/jmeter-server \
                        -Dserver.rmi.localport=50000 \
                        -Dserver_port=1099</code></span>

 

As you can see in the Dockerfile above, if we need to change the version/port of Java/JMeter, I just need to update the dockerfile and Docker will take care of the rest.

I have pushed these Dockerfiles to docker hub under vinsdocker account. Therefore, anyone can extract these files and set up a JMeter distributed testing infrastructure.

Make sure docker is installed on your machine. Once installed, the rest is easy. You just need to follow the steps here.
Run the following commands one by one:

sudo docker run -dit --name slave01 vinsdocker/jmserver /bin/bash
sudo docker run -dit --name slave02 vinsdocker/jmserver /bin/bash
sudo docker run -dit --name slave03 vinsdocker/jmserver /bin/bash

Docker automatically extracts the docker image I uploaded and creates 3 containers for the JMeter server. If you need more containers, continue executing the above command and just change the container name.

Run the following command to create a container for the JMeter master server

sudo docker run -dit --name master vinsdocker/jmmaster /bin/bash

 Run the following command to see all running containers, open ports, etc.:

sudo docker ps –a

 Run the following command to get a list of the IP addresses of these containers:

sudo docker inspect --format '{ { .Name }} => { { .NetworkSettings.IPAddress }}' $(sudo docker ps -a -q)

I've included a sample test in the docker image that ran for 30 seconds with 5 concurrent users, which you can see in the container. path. /sample-test/sample-test.jmx

If - you need to copy any file from host to docker container - you can issue the following command. For example: I copied the tests into my main JMeter container. This command will copy my local jmeter test (docker-test.jmx) to this path in the main container:

/jmeter/apache-jmeter-3.3/bin/docker-test.jmx

sudo docker exec -i master sh -c 'cat > /jmeter/apache-jmeter-3.3/bin/docker-test.jmx' < docker-test.jmx

Use the following command to enter the container and we can see whether the file was successfully copied:

sudo docker exec -it master /bin/bash 

Let's run the test on the master server and see if it works fine [not in distributed mode]. The Docker container will be able to run JMeter tests because it has all the software and dependencies to run JMeter tests:

jmeter -n -t sample-test/sample-test.jmx
Creating summariser <summary>
Created the tree successfully using sample-test/sample-test.jmx
Starting the test @ Thu Dec 21 17:14:59 UTC 2017 (1513876499683)
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
summary +      1 in 00:00:01 =    1.5/s Avg:   265 Min:   265 Max:   265 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
summary +    336 in 00:00:29 =   11.4/s Avg:   112 Min:    87 Max:   325 Err:     0 (0.00%) Active: 5 Started: 5 Finished: 0
summary =    337 in 00:00:30 =   11.2/s Avg:   113 Min:    87 Max:   325 Err:     0 (0.00%)
summary +      4 in 00:00:00 =  210.5/s Avg:    97 Min:    93 Max:   109 Err:     0 (0.00%) Active: 0 Started: 5 Finished: 5
summary =    341 in 00:00:30 =   11.3/s Avg:   113 Min:    87 Max:   325 Err:     0 (0.00%)
Tidying up ...    @ Thu Dec 21 17:15:30 UTC 2017 (1513876530127)
... end of run

That's all. Now we are ready to run our tests in a distributed manner using docker containers. We just need to add -R[slave01,slave02,slave03]

jmeter -n -t sample-test/sample-test.jmx -R172.17.0.5,172.17.0.6,172.17.0.7
Creating summariser <summary>
Created the tree successfully using sample-test/sample-test.jmx
Configuring remote engine: 172.17.0.5
Configuring remote engine: 172.17.0.6
Configuring remote engine: 172.17.0.7
Starting remote engines
Starting the test @ Thu Dec 21 17:01:48 UTC 2017 (1513875708955)
Remote engines have been started
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4445
summary +      4 in 00:00:11 =    0.4/s Avg:   182 Min:    98 Max:   232 Err:     0 (0.00%) Active: 15 Started: 15 Finished: 0
summary +   1021 in 00:00:20 =   51.5/s Avg:   111 Min:    85 Max:   283 Err:     0 (0.00%) Active: 0 Started: 15 Finished: 15
summary =   1025 in 00:00:30 =   33.7/s Avg:   111 Min:    85 Max:   283 Err:     0 (0.00%)
Tidying up remote @ Thu Dec 21 17:02:20 UTC 2017 (1513875740196)
... end of run

If you have noticed, we created all the containers on the same host. That is, both JMeter and JMeter slaves are running on the same machine. Therefore, all system resources will be shared by these containers.

Summary
       In this article, our aim was to create a JMeter distributed testing infrastructure using Docker. If you follow the steps above, you will understand that creating testing infrastructure using docker is very easy and fast. We write the entire infrastructure in one file, which can be version controlled. Then we create an instance (container) from this file. Docker ensures that the container has all software and dependencies etc. You may ask, is it okay to run multiple jmeter server instances on one machine to generate more load? No, this is not allowed. This doesn't help at all. In fact, one JMeter instance can generate more load than multiple JMeter instances running on the same host.

So why do we use docker and do these things? As I said above, our purpose here is to understand the role of docker in JMeter testing. We can understand the real purpose of docker when we use cloud computing service providers such as AWS/Digitalocean, where you can create any number of virtual machines on demand

Finally: [Tutorials that may help you]

 These materials should be relatively complete for friends who do [software testing]. This kind of learning materials has accompanied me through the most difficult journey. I hope it can also help you! Everything must be done as early as possible, especially in the technical industry, and technical skills must be improved.

If you need the above software testing information, you can click on the small card below to get it yourself.

Guess you like

Origin blog.csdn.net/2301_77645573/article/details/132889576