Apache JMeter Getting Started Tutorial

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.batdouble-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.propertiesthe file and open it with an editor, #language=eninsert a line below language=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.propertiesthe 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 belowThe 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:

  1. 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.

  2. 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 is 5, then it takes 1 thread 5per 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.1002020

  3. Number of loops: If the number of threads is 100, the number of loops is 100. Then the total number of requests is 100*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 HTTPor HTTPS, the default is HTTP.

  • Server name or IP: HTTPThe target server name or IP.

  • Port number: The port number of the target server, the default value is 80

  • Method: HTTPthe method of sending the request, the available methods include GET, POST, HEAD, PUT, OPTIONS, TRACE, DELETEetc.

  • Path: target URLpath ( URLthe remaining part after removing the server address, port and parameters).

  • Content encoding: Encoding method, the default is ISO-8859-1encoding, and it is configured here utf-8.

  • Parameters: Send the parameters together with the request. For the parameters sent in the request URL, the user can URLset all the parameters in this table, and each row in the table is a parameter (corresponding to the one URLin the  key=valuetable). 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:

  1. Label: Each  JMeter (  elementfor example, I have only one here  Spring WebFlux) has an  Name attribute, and the value of the attribute is displayed here  Name .

  2. Samples: the number of requests—indicates how many requests were sent in this test, and I simulated 100a user cycle 6here 100*6=600.

  3. Average: average response time (unit: ms). The default is a single Requestaverage response time, when used Transaction Controller, can also Transactiondisplay the average response time in units.

  4. Median: That is,  50% the user's response time.

  5. 90% percentile (Line): 90% The user's response time. The neighbors *%agreed.

  6. Min (Min): Minimum response time.

  7. Maximum value (Max): The maximum response time.

  8. Abnormal (Error) %: Error rate - number of wrong requests/total number of requests.

  9. Throughput (Throughput): Throughput - By default, it indicates the number of requests completed per second ( Request per Second), when used  Transaction Controller , it can also indicate a similar  LoadRunner number  Transaction per Second .

  10. Receive KB/Sec: The amount of data received from the server per second, which is equivalent to KB/ LoadRunnerSec Throughput/Sec.

  11. Send KB/Sec: The amount of data sent to the server per second, which is equivalent to KB/ LoadRunnerSec Throughput/Sec.

Guess you like

Origin blog.csdn.net/DY_CSDN/article/details/130016640