Jmeter is an open source stress test tool developed by Apache company based on Java. It is small in size, full of functions and easy to use. It is a relatively lightweight test tool and very simple to use. Since JMeter is developed in Java, you must first install the JDK to run it. Jmeter is free of installation, and can be used on Linux/Windows/MacOS. Jmeter can do interface testing and stress testing. The simple operation of interface test includes doing HTTP script (send GET/POST request, add cookie, add header, add permission authentication, upload file), doing WebService script, parameterization, assertion, association (regular expression extractor and processing json-JSON path) Extractor) and JMeter operate databases, and so on.

The interface test

Jmeter- HTTP interface script

Generally, there are five steps :(1) add thread group (2) add HTTP request (3) write access url, path, request mode and parameters in HTTP request (4) add view result tree (5) call interface, view return value

  • Jmeter sends a GET request

  • Jmeter sends a POST request

  • Add the cookie jmeter

    You need to add a configuration component to the thread group – the HTTP Cookie manager

  • Add the header jmeter

    You need to add a configuration component to the thread group – the HTTP header manager

  • Jmeter uploads a file

  • If the JMeter parameterized input parameter changes frequently, it can be set as a variable to facilitate unified modification and management. If the input parameters are required to be random or multiple, a variable is formed either by a function generator or by reading a file. So there are three ways to parameterize: user-defined variables, function generators, and reading files.

    (1) User-defined variables need to add configuration components – user-defined variables.

(2) Function generator needs to use function helper function, can call the function to generate some regular data. Several commonly used functions are _uuid, _random, and _time. _uUID generates a random unique ID. For example, to avoid Java request retransmission causing too much unprocessed data, the interface request can be added with a unique request ID and a unique response ID for one-to-one mapping; Random number _random, can be in a range of random values you specify; Take the current time _time, Some time class input arguments can be used, such as {__time(,)} to generate a timestamp accurate to milliseconds, {__time(/1000,)} to generate a timestamp accurate to seconds, and ${__time(YYYy-mm-dd HH: MM :ss,)} to generate the current time accurate to seconds.

(3) Read from files

You need to add a configuration component to the thread group -CSV Data Set Config

Recycle on EOF: After True is set, the value of Recycle is allowed



A specific example is shown below:

  • Jmeter Assertion JMeter assertion is used to check whether the response returns the result we expect. For the entire thread group, add an average-response assertion under the thread group. Add an average-response assertion under the request if it is only for a request.

  • Jmeter associated

    There is a parameter call between the interface requests. In order to save this parameter, the JMeter association is established. For example, the login interface and the commodity purchase interface need the login information such as the token returned by the login interface. Jmeter association can save the token information, which is convenient for the commodity purchase interface. Jmeter association can be accomplished in two ways, getting the value specified in the return result. They are the regular expression extractor and json Path Extractor respectively. (1) If the return value obtained by the regular expression extractor does not match, it can expand the data matching on both sides of the regular expression.

    A. About regular expressions

    () : The part in parentheses is what to extract. . : Matches any string except the line swap. + : Indicates that the character preceding the + sign must occur at least once (one or more times). ? : representative? The preceding character can occur at most once, stopping when the first match is found (0 or 1 times). : Indicates that the characters before the symbol may not occur, or may occur once or more times (0, 1, or more times). (.*) : Indicates the greedy mode, which matches as many characters as possible (.*?). Or (. +?) : Matches as few characters as possible. Once the first character is matched, no further action is taken.

    B. About templates

If you want to extract multiple values, such as a and b, you can write $1? 2 $. ${id_go}; ${id_g1}; ${id_g2}; ${id_g2}; ${id_g2}; Here is a concrete example, as shown in the figure below:

(2) JSON Path Extractor JMeter processes JSON strings by installing JSON Path Extractor plug-in and extracts the field values in JSON strings. The plug-in can be downloaded from jmeter-plugins.org/?search=jpg… After the download is complete, decompress it and directly put the lib folder into the corresponding directory of JMeter. Special note: This plug-in cannot be used for jMeter 2. Xx or so, but can be used normally after installing the plug-in for JMeter 3. Xx or so. Post processor -jp@gc-JSON Path Extractor needs to be created under the request, the specific instance is shown below:

About json path related plug-in method and use, recommend we can see this blog post: www.jianshu.com/p/56a607fc0…

  • There are basically four steps for jMeter to operate a database: (1) Import mysql JDBC JAR package (2) Create database Connection Configuration (3) add JDBC request to thread group, write SQL statement (4) Add view result tree, Click the start button to see the SQL executed. The concrete example is shown in the screenshot below:

Special note: Jmeter can also operate Oracle, postgreSQL, msSQL, mongodb and other databases, as well as different databases, JDBC Connection Configuration The Database URL format is different from the JDBC Driver Driver name. The list of JMeter database drivers is shown in the following table:

The database drive Database url
mysql com.mysql.jdbc.Driver jdbc:mysql://host:port/{dbname}? allowMultiQueries=true
oracle org.postgresql.Driver dbc:postgresql:{dbname}

Jmeter-webservice interface script

It is divided into five steps :(1) obtain the request address, request packet, and soapaction of the webservice interface through the soapui tool. (2) JMeter creates a thread group. (3) SOAP/XML-RPC Request is established under the thread group, and Request URL, Request packet, and soapAction are written. (3) Start JMeter, call the interface, and view the returned value by viewing the result tree. An example of SOAPUI obtaining information is shown below:

After submitting the SOAPUI, click RAW to view the SOAPATION. If soapATION is not returned for some interfaces, you do not need to fill it in jMeter.



An example of the Jmeter-webService script is shown in the figure below:

Pressure test

There are two types of pressure test scenarios: one is a single scenario, pressure on one interface; The second is a hybrid scenario, with multiple associated interfaces. Pressure measurement time, the general scenario is running 10-15 minutes. If it is a fatigue test, it can be pressed for a day or a week, depending on the actual situation.

Confirmation of pressure task requirements

Before pressure testing, it is necessary to make clear the pressure testing function and pressure testing index. Generally, several problems need to be determined:

  1. Should the pressure test be carried out with fixed interface parameters or with random interface parameters?
  2. How many concurrent requests are required?
  3. What is the TPS (Transactions per second) target? What is the response time?
  4. Pressure server name or pressure server IP, generally is the pressure test specified server

Pressure test setting

  1. Thread count: The number of concurrent threads and how many threads can run. Specifically, how many users at a time to access
  2. Rame -up Period(in seconds): indicates the number of seconds for JMeter to initiate concurrency. Prepare duration: Specifies the time required for all virtual users to start up. If the number of threads is 20 and the preparation time is 10, then it takes 10 seconds to start 20 numbers, or 2 threads per second.
  3. Number of loops: This setting does not change the number of concurrency, but can extend the concurrency. Total requests = Number of threads * number of loops
  4. Scheduler: Set the start time, end time, duration and start delay time of the pressure test.

Check the pressure test results

After running, the aggregate report will show the results of the pressure test. Mainly observe Samples, Average, error, Throughput.

  1. Samples: indicates the total number of requests made
  2. Average: Average response time, default is the Average response time of a single Request (ms)
  3. Error%: Indicates the percentage of the number of Error requests that occurred during the test. If there is an error, it is necessary to look at the log of the server side, and cooperate with the development to locate the cause
  4. Throughput: Throughput for short. By default, Throughput indicates the number of requests processed per second, which is the processing capacity of the server. The higher the TPS, the better the processing capacity of the server.

Analysis of pressure measurement results

  1. The error rate and development confirmation, determine whether the error rate is allowed to occur or in what range;

  2. Throughput If the number of requests per second is greater than the number of concurrent requests, the Throughput can gradually increase. If the throughput is less than the number of concurrent in the case of good performance of the machine under pressure test, it means that the number of concurrent can not be increased, and can be slowly reduced to find the best number of concurrent;

  3. · Log in to the corresponding Web server to check CPU and other performance indicators and analyze the data;

  4. Maximum TPS: Increase the number of concurrent requests until the TPS reaches a point where it starts to decline, and that value is the maximum TPS.

  5. Maximum number of concurrent requests: The maximum number of concurrent requests and the maximum TPS are different probabilities. Generally, the number of concurrent requests is increased continuously. When the server reaches a value, the request timeout occurs, and the value can be considered as the maximum number of concurrent requests.
  6. During the pressure test, a performance bottleneck occurs. If the CPU, network, and CPU displayed in the press task manager are normal but not more than 90%, it indicates that the server is faulty and the press is not faulty.
  7. Performance considerations include databases, applications, middleware (TomACT, Nginx), networks, and operating systems.

Jmeter stress tests under Linux

  1. To install JMeter on Linux, you need to install JDK first, configure environment variables, and upload the compressed installation package of JMeter. Then you can decompress the package in Linux. Recommend a blog: blog.csdn.net/zhemeteor/a…

  2. Run jMeter in Linux. Go to the bin directory of jMeter and run the script. If jMeter environment variables are not configured, run the following command:

./jmeter -n -t a.jmx -l res.jtl

JMX is the prepared JMeter script, and res. JTL is the test result file. The test result file can be imported to the JMeter result tree for viewing.

Jmeter Adds a load machine