Abstract
This article focuses on the status of JMeter tools in the test and some of them are difficult to understand or unclear in the handbook, readers can understand these concepts through this article, and then according to their own needs to consult the specific use of JMeter components to complete the test work .
1. Introduction
JMeter is a software for testing the C/S application of the desktop test (and is not suitable for B/s structure, because it is difficult to simulate the user's action on the browser, if you need to test B/s structure of the application, you can choose Selenium such tools), Mainly used for functional testing , stress testing , performance testing and other work.
The advantages of JMeter compared to other test software are as follows:
- It can help testers to easily simulate the environment in which multiple users access the server simultaneously (via thread Group), which can detect many problems that cannot be exposed in a single-threaded environment.
- A wide range of applications, almost all you can think of C/s application it can provide the corresponding support. JMeter has customized some of the application-specific test scenarios, such as testing the HTTP server, testing the database , testing the Java program, and so on. In addition, even if the jmeter does not provide a test environment for the current application, the user can customize it in the same way as yesterday BeanShell.
- Provides a rich logic controller that allows testers to easily write some relatively complex test logic.
- Provides a comprehensive set of variable mechanisms and configuration mechanisms to help testers reduce the burden of writing use cases and reduce duplication of effort.
- Provides a powerful monitoring build to help testers easily get test results statistics.
Disadvantages of JMeter:
- It is difficult to test for "correctness". Although JMeter provides an assertion mechanism, it is common for our tests to simulate multi-user operations, so that a user's response from a request is unpredictable (such as reading and writing a database table at the same time, although we can have each impersonated user store the information written in a common area, However, there may still be problems, because the time of the database writes and the time to write to the public area are not guaranteed to be synchronized, so it would be cumbersome to verify the correctness of the application by JMeter. Often we just use assertions to check for simpler and more important information, such as return codes.
- There is no good BeanShell testing mechanism. In JMeter, BeanShell is a very important part, because JMeter custom test scenarios are often somewhat out of the way with our applications, and you need to use BeanShell to accomplish some of the things JMeter can't do. However, BeanShell is also needed to ensure correctness, and jmeter does not provide good support for BeanShell testing.
- Through the above analysis, it can be found that jmeter is more suitable to find out the problems of the tested system in the concurrency environment.
2. Basic structure of the JMeter test case
The basic structure of the JMeter test case is a tree structure similar to that of Windows Explorer, where each node is represented by an element, so a complete jmeter test case is actually made up of one element, The execution of the test is actually the process of executing these elements. In general, JMeter will traverse these elements in a depth-first way, while for elements of the same layer, JMeter will execute from top to bottom.
There are many kinds of elements in the jmeter, and each element has a specific meaning in the tree structure, in order to facilitate understanding, these elements from the structural nature of the three main categories:
The first class is the control element, which usually appears at the side of the tree structure, which is used to control the execution of the first layer of the element (note, not all of its elements), such as controlling their sequencing, or executing or not executing which. With this type of element we can easily dynamically control the execution of test Cases , which are similar to the if-else, switch, while and other logical control statements that become languages.
The second class is an action-based element, which is the element that actually initiates the test request, usually at the bottom of the tree structure, where each action element represents a request-response process, and their order of execution is usually managed by the controlled element.
The third category is the configuration element, which can only be used as a leaf node in a tree structure, and is applied to its scope (the rule of scope is as follows: If the configuration element is under a control element, it is scoped to all descendants under the control element; If the configuration element is under an action element, It has a certain effect on all action elements that have a scope of only this action element. These effects vary depending on the type of element, such as changing the parameters of the element, delaying the request time, adding some action before and after the request, and listening to the request and its corresponding. In addition, if an action element is affected by more than one of the same configuration elements, the effect of these configuration elements will be merge,merge depending on the function type of the element (see section 3rd).
3. Elements in the JMeter
Functionally speaking, the elements of JMeter are divided into eight categories and two special elements. These elements are described here from the use of the context, as to what the specific function of each element will need to see the help of JMeter (the method is to click on the unknown element, and then select the Assistance menu under the "Assist" option).
- The Test plan element: A control-type element. Can exist only at the root node of a tree structure. It represents the entire test scenario, where testers can set up some global content, such as globals (note that global variables are thread local, see section 4th), classpath configuration (if you want to call your own Java class in JMeter), and so on.
- The Thread group element: A control-type element. Can exist only under the test plan element. It represents a group of users who behave similarly, and usually we put the action of a class of users under the same thread group, so that we can simulate many of these users. Here you can configure the number of impersonated users (number of threads), number of cycles, execution time, and so on.
- Logic controller: A control element. Can exist in any seat under the thread group. It is used to control the execution of its underlying elements, and JMeter provides many elements of the logic controller type that allow us to implement basic logic in testing.
- Config element: the configuration elements. These elements are used to change the configuration of all action elements within their scope, and the use of such elements can reduce duplication of effort in the writing of test cases, such as allowing an HTTP request defaults element to configure the host address and port number of HTTP requests in all use cases. This eliminates the need to make such a configuration in each action element. This value is used when the merge occurs, if only one of the config element elements in a domain has a value, and if a field has multiple config element elements that have values, the config closest to the action element is used The value of element elements (closest to the configuration element under the Action element node).
- Timer: a configuration element. If you want to control how often requests are issued, you should defer these requests using a timer. When the merge occurs, the time of all the timer is added.
- Pre Processor: Configuration element. Used to modify all affected action elements according to certain conditions. When the merge occurs, the pre processor will be executed sequentially, farthest from the action element.
- Post Processor: Same as pre Processor, just after the action element is used to extract the required information from the response.
- Assertions: configuration element. Used to assert the result of an action element within its affected range, such as the ability to assert a return code for some HTTP requests, and the system will log the error if it does not pass. When the merge occurs, all assertion will be judged.
- Listener: Configuration element. Used to listen for all the action elements in its affected range, the test result data is mainly generated by such elements, so they are very useful. When the merge occurs, all listener monitoring actions are performed.
- Sampler: Action-type element. Represents a request-response process, which is the initiator of the action in the test case and the primary element of the test case. JMeter many kinds of action elements according to different applications, if the user feels still not enough to use even BeanShell sampler to write their own actions.
4. Variables in the JMeter
Sometimes we want to send thousands of random requests, or want the content of this request to depend on the previous requests, then we need to use the variables in JMeter (note that variable is not the property), so that you can use variables to configure each request, This makes it possible for the same sampler to make different requests each time.
When using variables, it is important to note that the variables in JMeter are thread independent (thread Local), that is, although we have defined n variables, there is a mirror of the n variables in each thread, and they are independent and non-interfering with each other.
When we want to use variables, we first need to create the required variables. There are many ways to create variables in JMeter, one way is to define global variables through test plan (for all thread Group, which is thread independent), or through the user Defined in config element element to define local variables for different thread groups (note that the variables defined in the User Defined element are used for the entire thread group, regardless of where the element is placed, it will be applied to the entire thread group, which is a special configuration element). This variable is also created when we assign a value to a variable that is not created in the app, which will be referred to as an assignment later. In fact, JMeter's source code uses map to implement the variables, so these properties are not difficult to understand.
When we want to refer to a variable somewhere, we can get the value of the variable by ${the syntax of the variable name}. Note that if the variable is not defined, then the equation is treated as a normal string.
There are many ways to modify a variable's value, either by BeanShell or by some specific element in JMeter (for example, CSV Date Config Element), or by using the JMeter function to define a variable (how to do that, See the following section).
Normally if we want to make a different request in each loop, we can put the possible request content in a file and have the CSV Date Config element get the corresponding value from it and give it to the variable, or through the BeanShell Sampler uses the script to customize the value of the variable (note that you cannot use the BeanShell preprocessor in the pre processor to customize the variable, and the pre processor is used to modify the domain in the request, which occurs after the request is created. That is, if we define a variable in BeanShell preprocessor and then write it in the request domain, the result is that JMeter first sees a variable that is not assigned, and then treats the ${variable name} as a string and then executes BeanShell Preprocessor. This is where many people are prone to make mistakes, or you can use the pre processor to directly modify the domain in the request, and you can write a jmeter function in the request domain to generate the desired value directly.
In some applications, we want the content of the next request to depend on the previous request. Then we can extract the useful information from the response via post processor and assign it to a variable for the next use.
5. Properties in JMeter
There are property concepts in jmeter, and they usually have two uses: First they represent the configuration information for JMeter (there are bin/in the JMeter directory. Jmeter.properties); second, they can be used as memcache to enable communication between threads (the get and put methods of properties are thread-safe). Property acquisition, definition, modification is generally done through the JMeter function, of course, can also be through the powerful BeanShell.
Often when we want to have some kind of dependency between multiple threads, we can use the property, but that's not much, because most of the users we simulate are independent of each other, and they shouldn't know what others are doing.
In addition, we can configure some constants in the Jmeter.properties file to be used at any time in the test
6. Functions in the JMeter
Sometimes we need to perform some simple operations (such as generating a random number) and do not want to write BeanShell code, so consider using some of the functions built into JMeter. These functions are typically formatted with the ${__ function name (parameter 1, Parameter 2 ...)}, and they have two ways to return their execution results: one is to return directly, that is to say JMeter will replace the function's string with the return result of the functions after executing the function (for example, assuming ${__ Method ()} returns to world, so if we write Hello ${__method} in the field, the actual result is Hello world. The other is when a variable is specified by a parameter (usually the last one), jmeter the execution result into that variable. Depending on the function, the JMeter provides a powerful function builder (the Function Helper dialog option in the Options menu), which lists all the methods and possible functions, and has handy help documentation, and The generator can be used by the user to generate the required functions.
It is necessary to emphasize again jmeter the process of reading a field (any space that can fill in the content): when JMeter reads a string in a field, it first checks to see if there is a match ${...} The string, if any, to parse the ${recursively ...} Until the string in parentheses no longer contains parentheses. And then parse this ${...} Whether it is a function or a variable, and if it overwrites the original ${with its result ...} string until the entire string has been parsed. For example, when you enter Hello ${__method (${num},2)} In the input field, and the value of the NUM variable is 1, the string is first converted to Hello ${__method}, and then because the value of ${__method is world, The content in this field will eventually be Hello world.
After mastering the above content, we can basically apply the functions and variables in JMeter, and what the reader needs to do is to look at the manuals to see what functions the functions provide. (Note that some functions cannot be placed in special domains, such as ${__threadnum} can not be placed in the variable definition of test plan or in the variable definition field in user Defined element, the reason is very clear in the manual)
7. The BeanShell in JMeter
In most cases, the functionality provided by JMeter is not enough for us to use, and there may be some more complex logic in our test cases that are not implemented by simple functions, Then we have to use the powerful BeanShell (JMeter has some elements specifically for adding BeanShell scripts, such as BeanShell Sampler, in different places in the test). Unfortunately, JMeter's manual does not describe how BeanShell is used, but instead pushes all the responsibilities to BeanShell's website, and BeanShell's website does have a complete BeanShell user manual, But we don't want to learn a scripting language for testing, so here are some simple applications for BeanShell, and if you don't feel enough, then look at more complex applications.
7.1 BeanShell Get started quickly
BeanShell is a Java-oriented scripting language, so if you want to use him without BeanShell at all, write Java code directly. The only thing to note is that BeanShell supports variables of a type, that is, without specifying a variable type, as long as he is assigned a value, BeanShell knows the type of the variable. Regardless of where the type variable is used, the subsequent code can access the variable, which is different from the strongly typed Russian variable. For example:
View Plaincopy to Clipboardprint? Arbitrary code block { y = 2; Untyped variable assigned int x = 1; Typed Variable assigned } print (y); 2 print (x); error! X is undefined. Arbitrary code block { y = 2; Untyped variable assigned int x = 1; Typed Variable assigned } print (y); 2 print (x); error! X is undefined. |
7.2 JMeter built-in variables
JMeter has built-in variables in its BeanShell that allow users to interact with JMeter, the main variables and how to use them as follows (JMeter document does not explain this part in detail, This will also indicate which of the classes they correspond to in Javadoc):
- VARs: This variable actually refers to the local variable container in the JMeter thread (which is essentially map), so you can access the variables in the jmeter through the put and get methods. This variable is the most useful of all built-in variables, and it is a bridge between test cases and BeanShell interactions. Corresponds to Org.apache.jmeter.threads.JMeterVariables
- Props: This variable refers to the configuration information for JMeter, which is used in a similar way to VARs, but can only be put in a string type value, not an object. Corresponds to Java.util.Properties.
- CTX: This variable refers to the context of the current thread, and in theory we can control almost everything related to the current thread, but this requires the user to be very familiar with JMeter's source code. Corresponds to Org.apache.jmeter.threads.JMeterContext.
7.3 Using Java classes in BeanShell
Sometimes we want to send a few requests, and the content of these requests is implemented by a Java class, then we need to let JMeter call this Java class, and the only way is through BeanShell.
In order to be able to load the Java class, we first need to add the class Path of the custom Java class in the test plan, then we can import the Java class in BeanShell and finally put the obtained content into the VARs variable. This way you can pass the ${in other places ...} To get the information for that variable.
For example, a COM.LINHAO.A class was created under the/root/java folder, and the class has a method hello () returns a "Hello World" string, so if you want to use the string returned by this method in the request, do the following:
First add classpath to/root/java under test plan
Then add a BeanShell sampler before the request and write the following code:
View Plaincopy to Clipboardprint? Import COM.LINHAO.A; Vars.put ("word", A.hello ()); Import COM.LINHAO.A; Vars.put ("word", A.hello ()); |
Finally, write ${word} where you want to use the string, so that eventually the field will be replaced with the Hello World
Commissioning of BeanShell under 7.4 jmeter
BeanShell is also code, there may be errors, and JMeter is also a graphical interface of the program (there are command-line mode, but not the same as in the design use case phase), so many times the use Case designer is not able to determine whether certain variables in BeanShell are correct. An easy way to do this is to use BeanShell to create a dialog box and then display the value of the variable you want to detect in the dialog box. The code is as follows:
View Plaincopy to Clipboardprint? JFrame frame = new JFrame (a); A is a variable to being checked Frame.setvisible (TRUE); JFrame frame = new JFrame (a); A is a variable to being checked Frame.setvisible (TRUE); |
This allows you to know the value of variable a when the test is executed here.
Although BeanShell is very powerful, it is a scripting language after all, so if the generation logic of test parameters is complex, then the main logic should be placed in the Java class, and then let BeanShell to invoke, BeanShell only to handle some relatively simple operations.
"Go" JMeter User Guide