1.1 Overview of JMeter
Introduction to JMeter
Apache JMeter™ is an open source software developed by the Apache organization. It is a typical application program developed in pure Java. It can be used for software testing on different platforms such as Windows, Linux or macOS systems. JMeter is mainly used for functional load testing of applications to measure software performance, and can also be used for other types of testing such as interface testing, API testing, etc.
JMeter version
Different versions of JMeter have different functions and different support for JDK. Some functions of the new JMeter version will be gradually improved, new functional components will be introduced, and the performance will be gradually improved. Therefore, in the best practice of the JMeter official website, it is recommended that users use the latest JMeter version anyway.
Here we introduce the latest JMeter5.2.1 version.
JMeter GUI interface
When JMeter is running in GUI mode, the window mainly consists of three parts:
1. Ribbon
The upper menu bar and the lower toolbar. The menu bar shows a menu of functions provided by JMeter, while the icons in the toolbar are shortcuts for common functions.
2. View area
JMeter elements are presented in a tree structure, where "Test Plane" is the root node of the tree, and each node is a JMeter element.
In this area, you can add and delete nodes, or adjust the position of nodes by dragging.
3. Content area
Select a JMeter element node in the view area, and the content of the element will be displayed in the content area accordingly. Its content can be viewed, set and other operations.
1.2 JMeter test composition
JMeter build tests are similar to assembling an intelligent electronic device, such as a smartphone. A smartphone is composed of a series of physical components such as CPU, memory, input and output devices (such as USB interface, camera, etc.), screen, and battery.
JMeter tests are also composed of a series of JMeter elements. JMeter provides all the elements for building tests. You can assemble these elements as you like to complete the test you want.
JMeter elements have the following four types of test elements:
1) Test plan
2) Thread group
3) Components
Contains configuration elements, timers, pre-processors, post-processors, assertions and listeners
4) Controller
Includes Sampler, Logic Controller and Test Snippets
1.2.1 Test plan
A test plan describes the sequence of steps that a JMeter test will perform at runtime. A complete test plan consists of one or more thread groups, logic controllers, samplers, listeners, timers, assertions, and configuration elements.
The test plan element is the root node of the JMeter test tree and is unique, and all test element nodes are located under the root node.
1.2.2 Thread Group
In manual testing, test cases are executed manually by test engineers. The test cases of performance testing and automated testing are executed by the selected testing tools instead of testing engineers. The task of JMeter to execute the test is done by the thread group. For example, thread groups are equivalent to test engineers who execute test cases in manual testing.
The Thread Group controls the number of threads JMeter uses to execute tests. How many users to simulate (called virtual users) to execute the test can be achieved by modifying the number of threads in the thread group. For example, setting the number of threads to 10 means simulating 10 users to execute the test.
All samplers and logic controllers must be located under the thread group. From this perspective, the JMeter test plan really starts from the thread group. Other elements, such as listeners, can be placed directly under the test plan, in which case they will act on all thread groups.
There can be multiple thread groups under a test plan, and multiple thread groups can be configured to start in parallel or sequentially in the test plan.
1.2.3 Components
The most basic element in JMeter is component, which is the smallest functional unit in JMeter test, and each component has a specific function. For example, the "Response Assertion" assertion element can realize the verification of whether the request or response is expected.
JMeter provides many components. In order to facilitate users to use and manage many components, JMeter groups multiple functionally similar or logically related components into one category, called components. JMeter consists of six major components: configuration elements, timers, pre-processors, post-processors, assertions, and listeners.
- configuration element
Configuration elements are closely related to samplers. Similar to configuration files to software, software configuration files can affect the behavior of software; also through configuration components, request content can be added or modified to realize customization of requests.
- timer
By default, JMeter threads execute samplers sequentially without pauses. The delay is specified by adding a timer to the thread group. If no delay is added, JMeter may send too many requests to the server in a short period of time, causing the server to be overloaded and crash.
A timer can delay the execution of each sampler within its scope for a period of time.
- Preprocessor
A preprocessor performs some operations before making a sampler request.
If a preprocessor is attached to a sampler element, it will execute before that sampler element runs.
Preprocessors are often used to modify the settings of a sampler request before running, or to update variables not extracted from the response text.
- post processor
A postprocessor performs some action after a sampler request is complete.
If a postprocessor is attached to a sampler element, it will execute after that sampler element has run.
Post-processors are usually used to process the response data and extract the required values from it.
- Affirmation
Assertions are used to verify that a sampler request or corresponding response returns the expected result.
Whether the JMeter test is executed successfully and whether the result is expected can be verified by adding assertions.
- listener
The listener can collect relevant data during the execution of the test by JMeter, and present the data in different forms, such as trees, graphs, reports, etc.
For example, the "Graph Results" listener draws a graph of response times, the "View Results Tree" listener shows details of sampler requests and responses, etc.
In addition, some listeners can also save the collected test data to a file for later use.
1.2.4 Controller
- sampler
The sampler is used to construct the request sent to the server for processing, that is, to tell JMeter how to send the request to the server. For example, to send an HTTP request, you can select the "HTTP Request" sampler, and you can also customize the request by adding configuration elements.
- logic controller
Sampler requests are executed sequentially by default, which meets complex business/scenario requirements in some cases. Through the logic controller, the logic of JMeter sending requests can be controlled to realize complex business/scenario. For example, selectively execute certain requests, execute requests cyclically, execute logically dependent requests as a whole, execute requests alternately, etc.
1.3 JMeter execution order and scope
1.3.1 Execution order
Similar to the priority of operators or operators, when a JMeter test contains multiple different elements, which elements are executed first and which elements are executed later are not executed in strict order according to the order in which they appear, but It will follow certain internal rules, which we call the execution order of JMeter elements. Generally, JMeter elements are executed in the following order:
0. 配置元件
1. 前置处理器
2. 定时器
3. 取样器
4. 后置处理器
5. 断言
6. 监听器
A simple example:
Among them, ① is a sampler, ② is a configuration component, ③ is a timer, ④ is a monitor, ⑤ is a pre-processor, ⑥ is a post-processor, and ⑦ is an assertion.
The execution sequence is: ②-->⑤-->③-->①-->⑥-->⑦-->④
1.3.2 Scope
The elements in the JMeter test tree can be divided into two categories: hierarchical structure and sequential structure
The elements of the hierarchy include: configuration elements, timers, pre-processors, post-processors, assertions and listeners;
Elements of the sequence structure include: samplers and logic controllers.
Generally speaking, the elements of the sequence structure do not have the concept of scope, and the scope is only for components with a hierarchical structure.
The JMeter scope is the area in which the component is valid for the sampler, and within this area the component is active.
Element scoping for hierarchies
The scoping rules for elements with a hierarchy are as follows:
(1)若其父节点为取样器,则其只对该取样器起作用;
(2)若其父节点为逻辑控制器,则会影响逻辑控制器下的所有取样器;
(3)若其父节点为线程组,则会影响线程组下的所有取样器;
(4)若其父节点为测试计划,则会影响测试计划下的所有取样器。
Some examples:
1. The parent node is a sampler
-Test Plan
-Thread Group
HTTP Request1
Response Assertion
View Results Treel
HTTP Request2
View Results Tree2
Parse:
If the parent node of the "Response Assertion" assertion is the "HTTP Request1" sampler, only "HTTP Request1" will be asserted, and the "HTTP Request2" sampler will not be asserted.
2. The parent node is the logic controller
-Test Plan
-Thread Group
-Loop Controller
Response Assertion
-HTTP Request1
View Results Treel
-HTTP Request2
View Results Tree2
Parse:
The parent node of the "Response Assertion" assertion is the "Loop Controller" logic controller, and the "Response Assertion" will assert the "HTTP Request1" sampler and "HTTP Request2" sampler under the logic control.
3. The parent node is a thread group
-Test Plan
-Thread Group
Response Assertion
-HTTP Request1
View Results Treel
-Loop Controller
-HTTP Request2
View Results Tree2
-HTTP Request3
View Results Tree3
Parse:
The parent node of the "Response Assertion" assertion is the "Thread Group" thread group, and the "Response Assertion" will assert the "HTTP Request1" sampler, "HTTP Request2" sampler, and "HTTP Request3" sampler under the thread group.
4. The parent node is the test plan
-Test Plan
Response Assertion
-Thread Groupl
-HTTP Request1
View Results Treel
-Loop Controller
-HTTP Request2
View Results Tree2
-Thread Group2
-HTTP Request3
View Results Tree3
Parse:
The parent node of the "Response Assertion" assertion is the "Test Plan" test plan, and the "Response Assertion" will assert the "HTTP Request1" sampler, "HTTP Request2" sampler and "HTTP Request3" under the test plan.
1.4 A simple JMeter test plan
Take visiting Baidu as an example ( www.baidu.com ).
按照如下步骤完成测试计划:
1.在Test Plan节点上右键,选择Add->Treads(users)->Thread Group
在Tread Properties项下,设置“Number of Threads(users)”为1
2.在Thread Group节点上右键,选择Add->Sampler->HTTP Request
在Web Server项下,设置“Server Name or IP”为www.baidu.com
在HTTP Request项下,设置“Path”为/
3.在HTTP Request节点上右键,选择Add->Assertions->Response Assertion
在Field to Test项下,选择“Response Code”
在“Patterns to Test”项下,点击“Add”,输入200
4.继续在HTTP Request节点上右键,选择Add->Listener->View Results Tree
5.点击“Save”快捷按钮,然后输入测试计划文件名,保存
6.点击“Start”快捷按钮执行测试,可以在“View Results Tree”中查看测试结果。
JMeter测试查看结果树显示如下:
If the article is helpful to you, remember to like, bookmark, and add attention. I will share some dry goods from time to time...
END Supporting Learning Resources Sharing
Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】
加入我的软件测试交流qq群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.
How to obtain the full set of information: