1. Brief introduction
Apache JMeter is a Java-based stress testing tool developed by the Apache organization. Used for stress testing software, it was originally designed for web application testing, but later expanded to other testing areas. It can be used to test static and dynamic resources such as static files, Java servlets, CGI scripts, Java objects, databases, FTP servers, and more. JMeter can be used to simulate huge loads on servers, networks or objects, to test their strength and analyze overall performance under different stress categories. Additionally, JMeter enables functional/regression testing of applications by creating scripts with assertions to verify that your program returns the results you expect. For maximum flexibility, JMeter allows the use of regular expressions to create assertions.
2. Download and install
Since Jmeter is developed based on java, you first need to download and install JDK and configure environment variables. The minimum requirement of JMeter is Java 8, which is compatible with higher versions. The official website recommends using the minimum required version.
Jmeter official website download address: https://jmeter.apache.org/download_jmeter.cgi
Download Jmeter
After the download is complete, unzip the zip package, find the bin directory and apache-jmeter-5.2.1\bin\jmeter.bat
double-click to start Jmeter
Start Jmeter
- Note: There will be two windows when it is opened, the command window of Jmeter and the graphical operation interface of Jmeter, and the command window cannot be closed.
Approximate interface
3. Basic settings
Jmeter language switching
- Temporary method : click
Options
→Choose Language
→Chinese(Simplified)
to switch to Simplified Chinese, only for this time, after closing Jmeter, the next startup will restore the default language.
switch language
- Permanent method : Open the bin directory under the decompressed file of Jmeter, find
jmeter.properties
the file and open it with an editor,#language=en
insert a line belowlanguage=zh_CN
, save after modification, and restart Jmeter The default interface is displayed in Simplified Chinese.
Modify default language
Modify Jmeter's default encoding to utf-8 to solve console garbled characters
-
Open the bin directory under the decompressed file of Jmeter, find
jmeter.properties
the file and open it with an editor -
exist
#The encoding to be used if none is provided (default ISO-8859-1)、#sampleresult.default.encoding=ISO-8859-1
-
Insert a line below
The encoding to be used if none is provided (default utf-8)、sampleresult.default.encoding=utf-8
-
Save and restart Jmeter after modification
modify code
4. Write project test scripts
4.1. Add thread group
- Right click on "Test Plan" → "Add" → "Thread (User)" → "Thread Group"
new thread group
- Configure thread group parameters
configuration
Detailed explanation of the main parameters of the thread group:
-
Number of threads: number of virtual users. A virtual user occupies a process or thread. How many simulated user visits will fill in the number of threads.
-
Ramp-Up time (seconds): How long does it take for the set virtual users to start up. If the number of threads is
100
, and the preparation time is5
, then it takes 1 thread5
per second to start , that is, 1 thread per second. It is equivalent to simulating 1 user access per second, and setting it to zero is what I understand as concurrent access.100
20
20
-
Number of loops: If the number of threads is
100
, the number of loops is100
. Then the total number of requests is100*100=10000
. If "Forever" is checked, all threads will keep sending requests until you choose to stop running the script.
4.2. Add test interface
- Right click on "Your Thread Group" → "Add" → "Samplers" → "HTTP Requests"
add request
Spring-boot
Fill in the interface request parameters, I will test the local service here (the demo source code used in this tutorial is at the end of the article), you can refer to the following figure to fill in:
Fill in the interface
Detailed explanation of the main parameters of the Http request (those who have done interface testing should get started quickly):
-
Protocol: send HTTP request protocol to the target server, can be
HTTP
orHTTPS
, the default isHTTP
. -
Server name or IP:
HTTP
The target server name orIP
. -
Port number: The port number of the target server, the default value is 80
-
Method:
HTTP
the method of sending the request, the available methods includeGET
,POST
,HEAD
,PUT
,OPTIONS
,TRACE
,DELETE
etc. -
Path: target
URL
path (URL
the remaining part after removing the server address, port and parameters). -
Content encoding: Encoding method, the default is
ISO-8859-1
encoding, and it is configured hereutf-8
. -
Parameters: Send the parameters together with the request. For the parameters sent in the request
URL
, the user canURL
set all the parameters in this table, and each row in the table is a parameter (corresponding to the oneURL
in thekey=value
table). Note that you need to check "encoding" when passing parameters into Chinese ".
4.3. Add view result tree
- Right click on "Your Thread Group" → "Add" → "Listener" → "View Results Tree"
add result
- Here, we modify the response data format (choose whatever format you return, I return json here), run the Http request, and you can see the response data returned by this request.
View Results
4.4. Add user-defined variables
- Add user-defined variables for Http request parameterization, right-click on "Your Thread Group" → "Add" → "Configuration Components" → "User-Defined Variables":
custom variable
- Add a username parameter (corresponding to your actual request parameter key, those who have done interface testing should understand it)
add variable
- Use this parameter in the Http request, the format is: ${key}, for example:
use parameters
- Here I have a method to query users by username, so after changing the parameters, the result of running again is zero user data
View the results tree again
4.5, json assertion (because I return json here, other requirements should be selected according to the actual situation)
- Add an assertion: Right click on "Your HTTP Request" → "Add" → "Assertion" → "json Assertion"
image.png
- Configure the specific content of the json assertion
Assertion content
- Assertion result: Right-click "Your HTTP Request" → "Add" → "Listener" → "Assertion Result"
Assertion result
Assertion success
In order to demonstrate the failure, I modified the assertion content to zer
assertion failed
4.6. Add aggregation report
- Right-click "Your Thread Group" → "Add" → "Listener" → "Aggregate Report" to store the performance test report
add report
So far we have completed a basic interface test script
5. Performance test
- In order to test the effect, I simulate 100 users to access and obtain data concurrently, loop 6 times, and modify the thread group data as follows:
Modify thread group
- Go back to the aggregated report and run this stress test
pressure test
- Analysis test report (first let this stress test run to completion)
Test Data
Detailed explanation of aggregation report parameters:
-
Label: Each
JMeter
(element
for example, I have only one hereSpring WebFlux
) has anName
attribute, and the value of the attribute is displayed hereName
. -
Samples: the number of requests—indicates how many requests were sent in this test, and I simulated
100
a user cycle6
here100*6=600
. -
Average: average response time (unit:
ms
). The default is a singleRequest
average response time, when usedTransaction Controller
, can alsoTransaction
display the average response time in units. -
Median: That is,
50%
the user's response time. -
90% percentile (Line):
90%
The user's response time. The neighbors*%
agreed. -
Min (Min): Minimum response time.
-
Maximum value (Max): The maximum response time.
-
Abnormal (Error) %: Error rate - number of wrong requests/total number of requests.
-
Throughput (Throughput): Throughput - By default, it indicates the number of requests completed per second (
Request per Second
), when usedTransaction Controller
, it can also indicate a similarLoadRunner
numberTransaction per Second
. -
Receive KB/Sec: The amount of data received from the server per second, which is equivalent to KB/
LoadRunner
SecThroughput/Sec
. -
Send KB/Sec: The amount of data sent to the server per second, which is equivalent to KB/
LoadRunner
SecThroughput/Sec
.