Implementing JMeter distributed stress testing based on docker

Why is distribution needed?

At work, we often need to perform high- QPS stress testing on some key interfaces. JMeter is developed in the Java language and does not create a thread (virtual user). The JVM allocates 1M of stack memory space to each thread by default. Limited by the configuration of a single pressure testing machine, it is difficult to achieve too high concurrency. Therefore, by realizing distribution through JMeter, the hardware resources of multiple hosts can be integrated to achieve stress testing of the tested interface at the same time.

picture

There are two roles in the Jmeter distributed testing environment: Master and Slaves

Master node: Send test scripts to participating Slaves nodes and aggregate the execution results of Agent nodes.

Slaves node: Receive and execute the test script sent by the Master node, and return the execution results to the Master.

Why use docker?

If you want to link each host in a distributed manner, each host must have a JMeter environment (JDK + JMeter). If you use docker, you can manage the JMeter environment through docker, simply pull the image and start the container. Furthermore, the JMeter pressure testing machine can be infinitely expanded using k8s and cloud services. In theory, any number of concurrent users can be simulated.

Preparation

JDK: Required to start the JMeter tool

$ brew install openjdk@11

JMeter: Writing JMeter scripts

https://archive.apache.org/dist/jmeter/binaries/

picture

Write a simple script.

docker: Create containers through docker.

$ docker pull runcare/jmeter-master
$ docker pull runcare/jmeter-slave

View docker image

$ docker images

REPOSITORY              TAG        IMAGE ID       CREATED         SIZE
runcare/jmeter-master   latest     e052a8cd8680   3 years ago     326MB
runcare/jmeter-slave    latest     05c7ba96d97d   3 years ago     326MB

Please remember the image ID e052a8cd8680 of jmeter-master, which will be used later.

Use of distributed pressure testing

Start the slave node.

Assuming there are two hosts, two slaves can be started.

$ docker run -it -d --name slave01 runcare/jmeter-slave
$ docker run -it -d --name slave02 runcare/jmeter-slave

View started containers

$ docker ps

CONTAINER ID   IMAGE                  COMMAND                   CREATED             STATUS             PORTS                 NAMES
b270636a7741   runcare/jmeter-slave   "/bin/sh -c 'jmeter-…"   43 seconds ago      Up 42 seconds      1099/tcp, 60001/tcp   slave01
2584c7fef5f8   runcare/jmeter-slave   "/bin/sh -c 'jmeter-…"   52 seconds ago      Up 50 seconds      1099/tcp, 60001/tcp   slave02

View the IP addresses of two slaves

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

/slave01 => 172.17.0.3
/slave02 => 172.17.0.2
/k8s_nginx_nginx-deployment1-fc7586d97-jvjvk_nginx_4379ed7f-b0be-4f9f-a0a9-4a5fd7b45b38_1 => 
/k8s_nginx_nginx-deployment1-fc7586d97-bztbh_nginx_6968e6b1-6689-4f1a-a9ea-04532577841c_1 => 

Send stress test script to slave

$ result=`date +"%Y%m%d%H%M%S"` && docker run --rm -v /Users/fnngj/zhpro/script:/data e052a8cd8680 jmeter -n -t /data/baidu_script.jmx -l /data/$result.jtl -j /data/$result.log -e -o /data/$result -R 172.17.0.2,172.17.0.3

Parameter Description

result=date +“%Y%m%d%H%M%S”`: Specify the name of the test result, named after the current date and time.

/Users/fnngj/zhpro/script: The directory of the stress test script. The test results will also be stored in this directory.

baidu_script.jmx: The name of the stress test script, stored in the /Users/fnngj/zhpro/script directory.

e052a8cd8680: Image ID of jmeter-master.

172.17.0.2,172.17.0.3: The IP addresses of the two slaves.

Test Results

Executed directory

$ pwd
/Users/fnngj/zhpro/script

$ ls
20230707005328     20230707005328.jtl 20230707005328.log baidu_script.jmx

View report

Enter the 20230707005328 directory and click on the index.html file to see the stress test results.

picture

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!   

Guess you like

Origin blog.csdn.net/2301_78843735/article/details/133030527