The latest and most complete jmeter interface test and stress test in the whole network

I. Introduction

Jmeter is an open source stress testing tool developed by Apache based on java. It is small in size, full of functions, and easy to use. It is a relatively lightweight testing tool and very easy to use. Because jmeter is developed in java, jdk must be installed before running. jmeter is free of installation. After getting the installation package, you can directly decompress it and use it. At the same time, it can be used on linux/windows/macos.

jmeter can do interface testing and stress testing. The simple operations of the interface test include doing http scripts (sending get/post requests, adding cookies, adding headers, adding authority authentication, uploading files), doing webservice scripts, parameterization, assertion, association (regular expression extractor and processing json -json path extractor) and jmeter to operate the database and so on.

2. Interface test


2.1 Jmeter-http interface script

Generally divided into five steps: (1) Add thread group (2) Add http request (3) Write access url, path, request method and parameters in http request (4) Add view result tree (5) Call interface, view return value

  • jmeter sends get request

  • Write picture description here

Write picture description here

  • jmeter sends a post request

    Write picture description here

 

  • jmeter add cookie

    You need to add a configuration element in the thread group—HTTP Cookie Manager

Write picture description here

Write picture description here

  • Adding a header in jmeter
    requires adding a configuration element in the thread group—HTTP header manager
    Write picture description here

Write picture description here

  • jmeter upload file

    Write picture description here

 

  • jmeter parameterization
    If the input parameter changes frequently, it can be set as a variable to facilitate unified modification management; if the input parameter requires random or multiple choices, a variable can be formed through the function generator or read the file. So there are three ways to parameterize: user-defined variables, function generators, and reading files.

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

Write picture description here

Write picture description here 

(2) The function generator
  needs to use the function assistant function, which can call the function to generate some regular data. Several commonly used functions are _uuid, _random, _time. _uuid will generate a random unique id. For example, to avoid too much unprocessed data caused by java request resending, the interface request can add a unique request id and a unique response id for one-to-one correspondence; the random number _random can be found in Take a random value in a range you specify; take the current time _time, some time-type input parameters can be used, such as {__time(,)} is to generate a timestamp accurate to milliseconds, {__time(/1000,)} is Generate a timestamp accurate to seconds, ${__time(yyyy-MM-dd HH:mm:ss,)} is to generate the current time accurate to seconds.

Write picture description here

(3) To read from the file,
  you need to add a configuration element in the thread group - CSV Data Set Config
  where Recycle on EOF: After setting True, allow the value to be cycled
Write picture description here

 

A specific example is as follows:

Write picture description here

Write picture description here

Write picture description here

  • jmeter assertion
      jmeter assertion is used to detect whether the result returned by the response is consistent with our expectations. If it is for the entire thread group, add an assertion-response assertion under the thread group; if it is only for a certain request, add an assertion-response assertion under the request.

Write picture description here

Write picture description here

 

jmeter association

There are parameter calls between interface requests. In order to save this parameter, a jmeter association is established. For example, the login interface and the purchase interface. The purchase interface needs the token and other login information returned by the login interface. The jmeter association can save this token information, which is convenient for the purchase interface.

The jmeter association can be completed in two ways, and the value specified in the returned result can be obtained. They are regular expression extractor and json path extractor respectively.
(1) Regular expression extractor
If the return value you want to obtain is not matched, you can expand the matched data on both sides of the regular expression.

Write picture description here

 

a. About regular expressions

(): The enclosed part is to be extracted.
.: matches any string except a newline.
+: The character in front of the + sign must appear at least once (one or more times).
?:represent? The preceding character can appear at most once, stopping after the first match is found (0 or 1).
: The character in front of the representative number may not appear, or may appear one or more times (0 times, 1 time or more times)
(.*): Greedy mode, matching as many characters as possible
(.*?) or (.+ ?): Match as few characters as possible, once the first one is matched, it will not go down.

b. About templates

  If you want to extract multiple values, such as a and b, you can write it as: $1$$2$. No matter how many values ​​to extract, the reference name is one. For example, the name is id, ${id_go}: get the whole string ab, ${id_g1}: get a, ${id_g2}: get b.
  Here is a specific example, as shown in the figure below:

The plug-in can be used normally after the plug-in is installed in the version around jmeter 3.xx.
  It is necessary to create a post-processor -jp@gc-JSON Path Extractor under the request, and the specific example is as follows:

Write picture description here

Write picture description here

Write picture description here

  For the method and use of json path related plug-ins, it is recommended to read this blog:

There are basically four steps for jmeter to operate
  the database: (1) Import the jdbc jar package of mysql (2) Create the connection configuration of the database, add the configuration element-JDBC Connection Configuration to the thread group (3) Add jdbc request to the thread group, Write sql statement (4) Add view result tree, click the start button, you can see the executed SQL. A specific example is shown in the screenshot below:

  • Write picture description here

Write picture description here

Write picture description here

Write picture description here

 Special note: jmeter can also operate databases such as oracle, postgreSQL, msSQL, mongodb, etc. At the same time, for different databases, the format of the Database url filled in JDBC Connection Configuration and the name of the JDBC Driver are also different. The jmeter database driver list is shown in the following table:

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

2.2 Jmeter-webservice interface script

It is basically divided into five steps: (1) First, you need to obtain the request address, request message and request soapaction of the webservice interface through the soapui tool. (2) jmeter creates a new thread group (3) creates a SOAP/XML-RPC Request under the thread group, and writes the request url, request message, and request soapaction. (3) Start jmeter, call the interface, and check the return value by looking at the result tree.

An example of soapui obtaining information is shown in the following figure:

Write picture description here

  After submitting soapui, click raw to see soapation. If some interfaces do not return soapation, there is no need to fill in jmeter.

Write picture description here
   The jmeter-webservice script example is shown in the following figure:

Write picture description here

3. Stress test

There are two scenarios for stress testing: one is a single scenario, where one interface is pressed; the second is a mixed scenario, where multiple associated interfaces are used. The stress test time generally runs for 10-15 minutes in a scenario. If it is a fatigue test, it can be pressed for one day or one week, depending on the actual situation.

3.1 Confirmation of stress testing task requirements
Before stress testing, stress testing functions and indicators must be clarified. Generally, several issues need to be determined:

  1. Should I perform stress testing with fixed interface parameters or randomized interface parameters?
  2. How many concurrency do you need to support?
  3. What is the TPS (transactions per second) target? What is the response time?
  4. The pressure server name or the pressure server IP is generally the server specified by the pressure test


3.2 Pressure measurement settings

  1. Number of threads: the number of concurrency, how much can run. Specifically, how many users are accessing at the same time
  2. Rame-Up Period(in seconds): Indicates how many seconds JMeter starts concurrently. Understand it as preparation time: How long does it take to set the number of virtual users to start all of them. If the number of threads is 20 and the preparation time is 10, then it takes 10 seconds to start 20 numbers, that is, start 2 threads per second.
  3. Number of cycles: This setting will not change the number of concurrency, but can extend the concurrency time. Total number of requests = number of threads * number of cycles
  4. Scheduler: Set the start time, end time, duration and start delay time of the pressure test.


3.3 Viewing the stress test results
  After running, the aggregation report will display the stress test results. Mainly observe Samples, Average, error, and Throughput.

  1. Samples: Indicates the total number of requests issued
  2. Average: Average response time, by default it is the average response time of a single Request (ms)
  3. Error%: The percentage of the number of error requests that occurred in the test. If there is an error, it is necessary to look at the log of the server and cooperate with the development to find the cause
  4. Throughput: tps for short, throughput. By default, it means the number of requests processed per second, that is, the server processing capacity. The higher the tps, the better the server processing capacity.


3.4 Analysis of pressure test results

  1. Confirm the error rate with the development to determine whether errors are allowed or within what range the error rate is allowed;
  2. If the number of throughput requests per second is greater than the number of concurrent requests, it can be increased slowly; if the throughput is less than the number of concurrent requests when the performance of the stress-tested machine is good, it means that the number of concurrent requests cannot be increased any more. Slowly decrease to find the optimal number of concurrency;
  3. After the pressure test is over, log in to the corresponding web server to check performance indicators such as CPU and analyze the data;
  4. Maximum tps: Continuously increase the number of concurrency until the tps reaches a certain value and starts to decline, then that value is the maximum tps.
  5. Maximum concurrency: The maximum concurrency and the maximum tps are different probabilities. Generally, the concurrency is continuously increased. After reaching a certain value, the server has a request timeout, and this value can be considered as the maximum concurrency.
  6. There is a performance bottleneck in the pressure test process. If the CPU, network, and CPU viewed by the task manager of the press machine are all normal and do not reach more than 90%, it can indicate that there is a problem with the server and the press machine has no problem.
  7. Considerations affecting performance include: database, application, middleware (tomact, Nginx), network, and operating system.


3.5 jmeter stress test under linux

  • jmeter installation in linux
    To put it simply, you need to install jdk first, configure environment variables at the same time, and finally upload the jmeter compressed installation package. After decompressing the installation package under linux, you can use it. Recommended blog: install Jmeter_zhemeteor column under Linux - CSDN blog
  • jmeter runs in linux |
    enter the bin directory under jmeter to run the script, and run the command without configuring the jmeter environment variable:

 

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

Among them, a.jmx is the prepared jmeter script, and res.jtl is the test result file. The test result file can be imported into jmeter to view the result tree.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

 

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive  

 

 

Guess you like

Origin blog.csdn.net/okcross0/article/details/132299279