Introduction of JMeter

JMeter features:

  • Functional and performance testing for multiple protocols

    • Web-http, HTTPS (Java, NodeJS, PHP, ASP.NET,…)
    • SOAP / REST Webservices
    • FTP
    • Database via JDBC
    • LDAP
    • Message-oriented middleware (MOM) via JMS
    • Mail – SMTP(S), POP3(S) and IMAP(S)
    • Native commands or shell scripts
    • TCP
    • Java Objects
  • Test recordings are provided

  • CLI mode

  • Providing HTML reports

  • Full portability and 100% pure Java

  • Provides multithreading support to simulate multiple users

  • High scalability > this section content translation JMeter home page: jmeter.apache.org/index.html

    Author’s experimental environment

JMeter is an implementation of the Java language, that is, a pure Java application, so JMeter can theoretically run on any environment where the corresponding Java environment is available. | type value | | | — – | : – | | | Java version of Java version “1.8.0 comes with _181” (Java8 and above) | | | JMeter version 5.4.1 | | | operating system Ubuntu 20.04 (GNOME 3.36.5) | | | kernel version Linux version 5.8.0-43 – generic |

download

This article provides a quick introduction to JMeter, so the rest of the environment is up to the reader

  1. Enter the website pages (jmeter.apache.org/download_jm…). Select the appropriate image source, download the binary distribution file;
  2. After decompressing the compressed file to a local directory,JMeterThe path of the decompressed directory is calledJMETER_HOME;

A brief introduction to the JMeter file

File path (relative to the JMETER_HOME directory) The file
bin Folder where executable files are stored
docs Help Document Directory
extras The extension plug-ins directory, where files provide support for Ant
lib Jar package used by JMeter
bin/jmeter Start the JMeter client executable (itself a shell script) on Linux and Unix systems
bin/jmeter-server Start the JMeter server process executable (itself a shell script) on Linux and Unix systems
bin/jmeter.properties JMeter configuration file
bin/jmeter.bat Start the JMeter client executable file in Windows
bin/jmeter-server.bat Start the executable for the JMeter server process in Windows

Launch the JMeter user interface

Go to the bin directory in the JMETER_HOME directory and run the following command to start JMeter GUI mode:

./jmeter
Copy the code

After a few seconds, the screen opens as follows:

Introduction to the main concepts of JMeter

concept Introduction to the
The test plan The test plan describes a series of steps that JMeter needs to perform
Thread group Thread groups define a pool of users to execute (to emulate multiple users concurrently)
JMX file A file ending in (.jmx) corresponding to a test plan that contains JMeter application-specific data structures organized in XML format
Sampler (sample) Samplers can sample execution targets (HTTP, JDBC, etc.)
Pre-processor Preprocessing the sampler (providing user parameters, waiting for the specified time, etc.)
Post-processor Post-processing of samplers (result extractors, etc.)
Assertions (assertions) Assertions on the results of the sampler (response time assertions, response data assertions, response structure assertions, etc.)
Logic Controller Logically control the test plan execution steps (If, While, etc.)
Listener (the listener) Listeners are notified when the sampler has finished executing and are typically used to process the resulting data (show the resulting data, aggregate charts, etc.)
Config Element Be able to do some configuration for the test plan (request header configuration, common request configuration, authentication configuration, variable configuration, etc.)

An introduction to the main functional elements of JMeter (related to Http tests)

The JMeter interface is mostly operated by clicking the right mouse button to bring up the drop-down menu for adding elements

Thread group

Right-click test plan add

Component screenshots:

Component parameter description:

The parameter name Value and Meaning
Action after a mistake Continue (continue the next step), start the next loop, stop the thread (only the thread), stop the test (stop the test after the execution of the sampler), stop the test immediately
The number of threads The number of users to emulate
Ramp-up time (s) Preheating time. The number of threads used to start all configurations complete at the time of execution. For example, if the number of threads is 10, a thread is started every 1s. (The first thread is always started immediately. If the total number of threads is 1, no matter what the preheating time is, it is equivalent to 0.)
cycles Forever (loop does not stop), specify a number (stop execution after specifying the number of loops)
Do you loop the same user each time? Yes/no

HTTP request default value

Right-click the thread group element and add it from the Config Element drop-down

This component is used for scopedHTTPThe sampler provides default values.

Component parameter description:

The parameter name Value and Meaning
agreement The protocol is HTTP or HTTPS
Server name or IP address Domain name or IP address
The port number Port number of the server
The path The PATH part of the URL
Content encoding The character set encoding used in HTTP requests
parameter HTTP request parameters
Message body data Default request body data

User-defined variables

Right-click the thread group element and select Add from the Configuration Element

This component is used to define variables in the thread and can be referenced elsewhere using the syntax ${variableName}.

Click on the following screenaddButton to add a line of variable name and value to define a variable.

HTTP samplers

Right-click on the thread group element and select from the sampler entry

HTTP sampler can be usedHTTPSampling of the system under test in the form of a request (initiating a request). Much of the data in this component is similar to that mentioned aboveHTTP request default valueMany properties in the component are the same if the sampler is inHTTP request default valueComponent scope, empty properties in the sampler are filled with default values, and non-empty properties are overriddenHTTP request default valueThe same property in the component (nearby).

Component parameter description:

The parameter name Value and Meaning
agreement The protocol is HTTP or HTTPS
Server name or IP address Domain name or IP address
The port number Port number of the server
The path The PATH part of the URL
Content encoding The character set encoding used in HTTP requests
parameter HTTP request parameters (query parameters in the URL)
Message body data Request body data
Request method HTTP methods such as GET, POST, PUT, and DELETE
File upload Used to upload files
Automatic redirection If selected, automatic redirection is enabled. Indicates that the downstream HTTP handler automatically redirects, so the intermediate redirection process is invisible to JMeter. And can only be used for GET and HEAD methods. The POST and PUT methods are rejected.
Follow redirection If this parameter is selected, redirection follows (this parameter is valid only when automatic redirection is not selected). If set, JMeter’s sampler processes the response and tracks each redirection in the process, with the last unredirected request and response as the outermost sample data, and data from other requests as additional information to the sample data. (Maximum redirection count is 20)
Use the KeepAlive When checked, JMeter sets the request header Connection: keep-alive. But whether this option takes effect depends on JMeter’s HTTP implementation

In response to assert

Right-click the sampler and click add – Assert – Response Assert option add

Response assertion can assert the results obtained by the sampler, and assert the contents including the response header, response code, and response body with logic (equal, include, and regular match, etc.) to verify whether the output of the sampler meets the expectations of the tester.

Component parameter description:

The parameter name Value and Meaning
Name Name of assertion
Apply to The Main sample only option is used to select the scope of the assertion. Main sample refers to the sampler to which the assertion belongs, and sub samples refer to the subsampler generated by the sampler. For example, the advanced option of the HTTP sampler – fetching a built-in resource – generates sub-samplers.
Field to Test Refers to the target for which the assertion needs to be made. Text Response refers to the entire Response retrieved from the server as Text (Response body). Response Code is the Response Code (for example, 200). A Response Message is a Response Message (for example, OK). Response Headers is the Response head. Request Headers refers to the Request Headers. Request Data refers to all Data requested as text (Request body). The URL sampled is the URL of the sample. Document refers to the Document extracted by a specific rule like the Document part of the View Results Tree component.
Ignore status Ignores the status code of the response. Generally 4xx and 5XX are considered failures by default. If not set, the total sample result is a combination of status code successes and failures and assertion results. If so, the status is set to success before assertion (note: if this parameter is set, the assertion must be first, otherwise the failure result of the previous assertion will be cleared)
Pattern Matching Rules What rules to use for a given pattern string. Contains Contains pattern strings (which are considered regular expressions). Matches represents a match for regular Matches (pattern strings are considered regular expressions). Equals means equal (case sensitive, pattern strings are treated as plain text). Substring indicates that the text under test contains the given pattern string (which is considered plain text). Two logical symbols: Not and Or. Not means the result is reversed. Or means that the matching rule is true for a given pattern string and the assertion is OK.
Patterns to Test A list of pattern strings to test. Multiple pattern strings can be added by clicking the Add button. If it is a regular expression, it is a perl5-style regular expression without enclosed parentheses.

JSON variable extraction

Right-click on the request and Add — Post Processors — Json Extractor to Add the Json Extractor element

JSONExtractors can be used to extract data from the response bodyJSONStructure to extract the properties of the specified position as variables.

Component parameter description:

The parameter name Value and Meaning
Name Use descriptive names for presentations
Names of created variables One or more names (comma separated) of the variables to be created, and the number of names must correspond to the JSON Path expression
JSON Path Expressions One or more JSON path expressions, separated by commas, match the number of variables. (See below for expressions)
Default Values One and more defaults, separated by commas. If a JSON path expression does not return a value, use the default value for the location. The quantity is consistent with the number of variables
Match No. (0 for Random) If the JSON path expression can take multiple values, which value should be taken? 0 means random; -1 means to extract all the results (multiple variables named _N will be generated, with N starting from 1); X means to extract the result at the specified location, starting at 1, and returning null if X is greater than the number of results (defaults will be tried)
Compute concatenation var If checked, it means that if more than one result is obtained, they are separated by commas and put into a variable named _ALL

JSON path: $is the root element,. Is the attribute, and [0] is the first element of the array object ([1] is the second).

Such as:

{
    "user": {
        "names": ["Jack", "Jacky"],
        "age": 10
    }
}
Copy the code

For the above JSON, the value Jack is extracted using the expression $.user.names[0]. For more information, see jsonpath.com/

View the result tree element

This element is used to view the execution status of a request using the JMeter interface. It can view information about request messages, response messages, and assertions. See below for details.

JMeter example

Next, we use what we have learned above to implement a scenario where we query the contributors under the Tootfish repository on Gitee, extract a designated contributor name, and use a second request to get information about the contributors.

The two Gitee interfaces used are

  • GET https://gitee.com/open-hand/choerodon/contributors_count?ref=0.23.0

The response body structure is (the JSON path of the contributor names we need to obtain is $.cnol3 [0].username) :

{
    "status": 1,
    "contributors_count": "16",
    "contributors": [
        {
            "username": "example1"
        }
    ]
}
Copy the code

  • GET https://gitee.com/{contributor name}

Create a test plan

When you open Jmeter, there is a default test plan

Creating a thread group

Right-click the test plan and clickAdd > Threads (Users) > Thread GroupAdd a thread group element. Fill in the following values:

createHTTPThe default configures the element

Right click on the thread group,Add > Config Element > Http Request DefaultsAdd configuration elements. Fill in the values as follows:

createHTTPThe sampler gets the list of contributors

Right-click on the thread group,Add > Sampler > HTTP RequestAdd the sampler (fill path and add a parameter) :

Add a response extractor

Right-click the request sampler,Add > Post Processors > JSON extractorAdd the element below because some user is ingiteeIt doesn’t exist. Here we’re going to extractJSON path 为 $.contributors[4].usernameThe username value of the fifth contributor is put into the variablecontributorName:

Add the view result tree element to the request

Right click on the samplerAdd > Listener > View Results TreeAdd view result tree as follows:

addHTTPThe sampler requests specific contributor information

Right-click on the thread group,Add > Sampler > HTTP RequestAdd the sampler (here inpathAttribute to fill in${contributorName}Used to refer to a variable extracted from the previous request) :

Add the view result tree element to the request

Right click on the samplerAdd > Listener > View Results TreeAdd view result tree as follows:

Click on the green triangle to execute

After clicking the execute button, we will be prompted to save the file before execution. After selecting an appropriate directory to save the file, we will click the Execute button. At this time, the green execute button will turn gray and the timing will start in the upper right corner, indicating that the request is being executed.

Viewing the Execution Result

Click on the first requestView the result treeThe element can view the result of the request:

You can see that the response body is as follows:

Looking at the request data for the second request, you can see that the request was successful, and you can see the path of the request${contributorName}Already rendered as the value extracted from the first requesthandchoerodon:

conclusion

JMeter provides HTTP samplers, various assertion mechanisms, configuration mechanisms, and variable extraction mechanisms to help with our modular interface testing and make the system more robust.

Extended information

  • A random variable

  • Boundary extractor

  • Regular expression extractor

  • Response time assertion

  • Response body JSON assertion

    Reference documentation

  1. JMeter Get Started
  2. JMeter component description

This article was originally written by the toothfish technical team.The official website of toothfish

About the toothfish

Choerodon’s full-scene performance platform provides systematic methodology and collaboration, testing, DevOps and container tools to help enterprises streamline their requirements, design, development, deployment, testing and operations processes to improve management efficiency and quality in a one-stop shop. From team collaboration to DevOps tool chain, from platform tools to systematic methodology, toothfish fully meets the needs of collaborative management and engineering efficiency, running through the whole end-to-end process, helping team efficiency become faster, stronger and more stable. Poke here to try the toothfish