Jmeter (ten) - from entry to proficient - JMeter logic controller - medium (detailed tutorial)

content

1 Introduction

2. Classification of logic controllers

3. Preview the logic controller

4. Detailed explanation of common logic controllers


1 Introduction

The explanation of the logic controller on Jmeter's official website is: "Logic Controllers determine the order in which Samplers are processed.".

Meaning, the logic controller can control the execution order of the samplers. It can be seen that the controller needs to be used with the sampler, otherwise the controller is meaningless. All samplers placed under the controller will be treated as a whole and will be executed together when executed.

The JMeter logic controller can control the execution logic of the element. In addition to the one-time controller, other types of logic controllers can be nested.

2. Classification of logic controllers

The logic controllers in JMeter are divided into two categories: (1) Control the logical execution sequence of nodes during the execution of the test plan, such as: Loop Controller, If Controller, etc.; (2) Group the scripts in the test plan to facilitate JMeter statistical execution Results and runtime control of scripts, such as: Throughput Controller, Transaction Controller.

3. Preview the logic controller

First, let's take a look at the logic controller of JMeter, the path: thread group (user) -> add -> logic controller (Logic Controller); we can clearly see that there are 17 logic controllers in JMeter5, as shown in the following figure :

 

If you can't see the above picture clearly, Brother Hong summed up a mind map, about the type of logic controller of JMeter5, as shown in the following figure:

 

Through the above understanding, we have a general understanding and understanding of the logic controller. Below, Brother Hong will share and explain some logic controllers that are usually used in work to small partners or children's shoes.

4. Detailed explanation of common logic controllers

  In this section, Brother Hong will explain the commonly used logic controllers in detail from top to bottom .

4.1Critical Section Controller

Let's take a look at the original official explanation: The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will be executed by only one thread as a named lock will be taken before executing children of controller.

Brother Hong's translation of the two knives, let's translate this bird language for you to see what it means. The general meaning is: Critical Section Controller (critical section controller), make sure its sub-elements (samplers/controllers, etc.) Executes only one thread as the specified lock before executing the controller's subroutine. Ha ha! Do you feel confused when you see this sentence? I touched my few remaining hair and fell into contemplation...... It doesn't matter, don't worry about it, Brother Hong will explain it with specific examples later, after reading it After the example, go back and read this sentence again, and you will suddenly realize it.

1. Let's take a look at what this Critical Section Controller looks like. Path: Thread Group > Add > Logic Controller > Critical Section Controller , as shown in the following figure:

 

2. The key parameters are described as follows:

Name: name, can be set at will, or even empty;

Comments: Comments, can be set at will, can be empty;

Lock name: lock name, here you can fill in the name of the thread executed under its child node, this thread is stored as a global lock

4.1.1 Example explanation

This part is mainly to understand what the sentence at the beginning means by cooperating with examples.

1. Let's talk about a test scenario for visiting the JMeter series of articles in Hongge Blog Park: the first step is to visit the home page of the blog park, the second step is to find the home page of Hongge's visit to the Hongge Blog Park, and the third step is to click JMeter category. Complete three steps in sequence to complete this test scenario. Then we use JMeter to add samplers for multiple requests according to this scenario. As shown below:

2. The script debugging is successful, run JMeter and view the result tree, as shown in the following figure:

 

3. From the previous figure, viewing the result tree shows that the request result data is not requested in order and does not meet expectations. At this time, add a critical section controller (critical section controller) and add a lock to control the execution order. As shown below:

4. The script debugging is successful. Run JMeter and view the result tree. It can be clearly seen that the requests are executed in the expected order. However, the response time will be too long, which will be explained later. As shown below:

 

4.1.2 Classification of lock names

(1) The lock name is empty, and each lock is considered to be a different lock

1. Let’s take a look at a specific example to create a critical part controller with an empty lock name, as shown in the following figure:

2. Run JMeter and view the result tree, as shown in the following figure:

 

(2) The lock names are the same, multiple locks are considered to be the same lock, and only one operation can exist at the same time point

1. Let's take a look at a specific example to create a critical part controller with the same lock name, as shown in the following figure:

2. Run JMeter and view the result tree, as shown in the following figure:

 

(3) The lock name is a variable, and it is judged whether it belongs to the same lock according to the variable value. When the variable value is the same, it is considered to be the same lock

1. Let’s take a look at a specific example, and create a critical part controller with a lock named variable, as shown in the following figure:

 

2. Run JMeter and view the result tree, as shown in the following figure:

 

4.2ForEach Controller

ForEach controller: generally used with user variables. The user-defined variables are called in sequence until the last, ending the loop. In order to meet the requirements of ForEach Controller to extract data, the format of variable naming is generally "variable name_number", where the number starts from 1. 1. Traversing the loop controller, as the name suggests, defines a loop rule. 2. All executable scenarios used to traverse the current element. 3. Read a series of related variables in the user-defined variables, the sampler or controller under the controller will be executed one or more times, and different variable values ​​will be read each time. 4. This controller is generally used in conjunction with the configuration element → regular expression extractor, which can repeatedly process certain elements on the page.

1. Let's take a look at what this ForEach Controller looks like. Path: Thread Group > Add > Logic Controller > ForEach Controller , as shown in the following figure:

 

2. The key parameters are described as follows:

Name: name, can be set at will, or even empty;

Comments: Comments, can be set at will, can be empty;

Input variable prefix: input variable prefix, you can define a set of variables in "user-defined variable", the loop controller can get the value corresponding to the variable from it, and then use it as the loop condition of the loop controller, you can also output the variable as a sampler parameter.

Start index for loop: The starting point of the subscript of the loop variable. Loop index start (unique) → traverse the variable range of the query, the starting value (if not filled here, the default starts from 1, if there is no variable starting with 1, an error will be reported during execution)

End index for loop: The end point of the subscript of the loop variable. loop index end (inclusive) → traverse the variable range of the query, the value of the end

Output variable name: The output variable name, the variable name generated by the loop controller. Subsequent references can be made through ${}

Add "_" before number ?: Whether to add "_" as a separator after the variable prefix. If there is an underscore in the defined variable name, you should check this item, otherwise it will not be found; otherwise, do not check it if there is no, otherwise the variable will also not be found.

4.2.1 Example explanation

1. First, customize 5 variables prefixed with Beijing Hongge, and the values ​​are abcde. And the numbers after Beijing Hongge are continuous. If they are not continuous, they will not be cycled to, as shown in the following figure:

 

2. Configure the ForEach controller as shown below:

 

3. Add a request to visit the blog garden home page + output value: ${Hongge}, the output value is the output variable Hongge of the controller, and the value of the output variable is obtained through ${Hongge}, as shown in the following figure:

 

4. Add the view result tree, run JMeter, and view the result tree, as shown in the following figure:

 

Note: Knock on the blackboard, knock on the head! ! !

1. The suffix values ​​of the input variables must be continuous, such as Beijing Hongge_1, Beijing Hongge_2, Beijing Hongge_3 ... In this way, if there is discontinuity in the middle, the loop will be interrupted

2. The setting of the loop start: if the variables are Beijing Hongge_1, Beijing Hongge_2, Beijing Hongge_3, and the set start is 1, the loop will start from Beijing Hongge_2

3. The setting of the end of the loop: If the variable has 3 Beijing Hongge_1, Beijing Hongge_2, Beijing Hongge_3, and the set end is 5, it will only loop Beijing Hongge_1, Beijing Hongge_2, Beijing Hongge_3, if the set end is 2, it will loop Beijing Hongge_1, Beijing Hongge_2.

4.3Include Controller

The Include controller is used to import external test fragments (non-complete test plans). The imported test plan will be executed during execution, but the imported test plan has special requirements. It cannot have thread groups and can only contain simple controllers. and components under the controller. In other words, it is equivalent to adding an execution unit, an encapsulated business operation unit, similar to the function (method) in our program development. For example, we use a sampler to simulate a business operation of querying student information, and then put it into a simple controller as an execution unit. When it is used elsewhere, we can directly refer to it without reinventing the wheel.

Generally speaking, it is more common to use the Include controller with the test fragment (Test Fragment).

1. Let's take a look at what this include Controller looks like. Path: Thread Group > Add > Logic Controller > Include Controller , as shown in the following figure:

 

2. The key parameters are described as follows:

Name: name, can be set at will, or even empty;

Comments: Comments, can be set at will, can be empty;

Filename: File name, a required field, if not, an error will be reported. Refer to the external jmx file by the path and filename of Filename.

Brother Hong recommends that friends or children's shoes can compare the Include controller and the Module controller (portal) to learn. The Include controller is referenced from external files and can only reference the content of the entire test segment. The Module controller is from the inside. The reference in the file is relatively flexible, and only part of the test fragment or module content can be referenced. This is not only easy to understand but also easy to remember and learn.

4.3.1 Examples

(1) When the value of the Filename path is empty, the program execution will report an error, the script execution will be terminated, and the following script content will not continue to be executed.

1. First create a test plan with an empty Filename path, as shown in the following figure:

 

2. Run JMeter and view the result tree (the program execution reports an error, the script execution is aborted, and the sampler below Visit Blog Park Homepage will not continue to be executed), as shown in the following figure:

 

(2) When the file in the Filename path does not exist, the program directly pops up an error and stops execution.

1. First, create a test plan in which the file in the Filename path does not exist. When you click the "Save" button, an error will pop up directly. As shown below:

 

(3) When the file in the Filename path does not contain the test fragment, skip the controller and continue to execute downward.

1. First create a test plan with no external references to test fragments, as shown in the following figure:

 2. Create a test plan that does not contain test fragments in the file with the Filename path, and add the external reference-no test fragment file above to the Include controller, as shown in the following figure:

 

3. Run JMeter and view the result tree (skip the controller and continue to execute the sampler on the home page of the blog garden), as shown in the following figure:

 

(3) When the file in the Filename path contains the test fragment, after executing the controller, continue to execute downward.

1. First create a test plan with external references to test fragments, as shown in the following figure:

 

2. Create a test plan that contains test fragments in the file with the Filename path, and add the external reference-with test fragment file above to the Include controller, as shown in the following figure:

 

3. Run JMeter and view the result tree (after executing the test fragment in the controller, continue to execute the sampler on the home page of the blog garden), as shown in the following figure:

At this point, you should understand the Include Controller and Test Fragment . Brother Hong’s understanding is that Test Fragment is equivalent to an independent part, which can be referenced by other test plans to achieve fragmentation and modularization of samples. When encountering repeated needs, such as login and registration, Test Fragment can be used . Fragment and Include Controller too. This avoids reinventing the wheel and doing a lot of useless work.

Guess you like

Origin blog.csdn.net/ZangKang1/article/details/124170565