Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

One, foreword

JMeter acts as a proxy between the browser and the Web server, capturing the requests of the browser and the responses of the Web server, simulating the pressure of real users on the Web server through threads. The basic principle is to set up a thread pool, multithreading running sampler to generate a lot of load, during the run through assertions to verify the correctness of the results, can be monitored to record the test results.

Download and install

Download address: jmeter.apache.org/download_jm…

Iii. Introduction of catalog

Four, quick use

GUI launch click:

~/bin/jmeter.bat
Copy the code

The process is started in the background and the command execution mode is displayed:

jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder]
Copy the code

GUI display:

Create a thread group

Right click on TestPlan and select New Thread Group as shown:

A thread group is a collection of threads, each representing a user who is using the application. In JMeter, each thread is meant to simulate a real user making a request to the server. In JMeter, the thread group component runs configuration where the user sets the number of threads, how they are initialized, and so on.

Success display:

Explanation:

Purpose: Is the starting point for the execution of a test plan, including sampler, Controller, and so on. Parameters:

  • Name: indicates the Name of the thread group
  • Comments: Notes the information and records the reasons and policies for setting some parameters
  • Action to be taken after a Sampler error:

The next action to take after an in-thread sampler fails:

  • Continue: Continue to perform subsequent operations

  • Start Next Thread Loop: Executes the Next Thread Loop

  • Stop Thread: Exits the Thread without performing any operations on the Thread

  • Stop Test: stops the Test after the sampling is complete

  • Stop Test Now: Stops the Test immediately

Thread Properites: Thread properties

  • Number of Threads(Users) : The Number of Threads to start for concurrent execution

  • Ramp-uo Period(in seconds) : specifies the Period in which the number of threads set in the previous step is started. Setting different values validates the performance of the system under different concurrency scenarios

  • Loop Count: Sets the number of times the Sampler Loop will be executed indefinitely after Forever is checked

  • Scheduler: It’s a Scheduler. The execution time can be set only after this parameter is selected

  • The Number of Threads * Loop Count is the Number of times the Sampler executes, as seen in the View Results Tree.

Scheduler Configuration:

  • Duration(seconnds) : request execution time (Loop Count Forever must be checked to be valid)

  • Startup delay(seconds) : specifies the delay time. However, you can use this configuration if you want to run tests at specified times.

  • You can set setUp Thread Group, Thread Group, and tearDown Thread Group.

Execution rules in Thread Groups

  • SetUp, Thread Group, and tearDown are executed sequentially

Write an HTTP request

Success display:Used to send HTTP(S) requests to the server

Parameter Description:

  • Name: component Name, which can be modified
  • Comment: Comments information, which can be written or not

Web Server:

  • Protocol[HTTP] : indicates the default HTTP. You can specify the value as required
  • Server Name or IP: IP address or domain Name, for examplecn.bing.com(Do not add http://, the default is; Cannot have path)

The HTTP Request:

  • Port Number: the default value is 80 for HTTP and 443 for HTTPS

  • Path:/ indicates the root directory

  • Content Encoding: The Content encoding can be set to UTF-8 or ANSI if the request has garbled characters

  • Configuration item: Default value

  • Paramters: Configures request parameters, such as adding key– avalue, url encode, file type

  • Body Data: The Parameters option cannot be clicked when selecting Body Data and entering the Json request Body. Parameters and Body Data and Files Upload cannot be selected at the same time.

  • Files upload: Upload a file.

Add view result tree

Click Run

9. Display results

Description:

Common ways to view results

  • Text: The default display mode, raw HTML Text
  • HTML: View the response data in a browser-like format
  • RegExp Tester: Supports regex for checking and correlation. (When doing the association can be done here to debug whether it is written correctly)
  • XPath Tester: Extract content using XPath; (When doing the association can be done here to debug whether it is written correctly)
  • Scroll Automatically: Will automatically Scroll when many requests are made

Display information:

  • Sampler Result: Summary information for this request
  • Request information: divided into request data and request header
  • Response information: divided into response data and response header
  • Filename: You can save the execution result to a file specified by Filename. Note: The execution result is saved in the bin directory of JMeter by default. If Errors is checked, only error messages are saved; If Success is checked, only successful execution information is saved; Save all information if not checked
  • Browser: You can open previously saved test files and display test information in the result column below. If Errors is checked, only error messages are displayed; If Success is checked, only successful execution information is displayed. All information is displayed if not checked

Sampler result

X. Summary RePort presentation

Display result:Explain the meaning of the aggregate report tag:

  • Label: Each JMeter element has a so attribute, which displays the value of the name attribute
  • #sampler: indicates the total number of requests made in this test, 11 users, 1 for each loop
  • Average: indicates the Average response time of a single request. By default, this parameter is the Average response time of a single requesttransaction controllerYou can also display the average response time in terms of transaction
  • Median: 50% of users have access time less than this time
  • 90% line: The access time of 90% users is below this time
  • Min: indicates the minimum response time
  • Max: indicates the maximum response time
  • Error % : the number of errors/total requests in this test
  • Throughput: Throughput. By default, requests completed per second.
  • KB/ SEC: indicates the Throughput received from the server per second, which is equivalent to the Throughput/ SEC unit of LR in milliseconds.

If you want to generate an HTML report, you can refer to the following command to generate:

jmeter -n -t [jmx file] -l [results file] -e -o [Path to web report folder
Copy the code

Parameterization test

Jmeter parameters CSV Data Set Config Select the CSV Data Set Config in the original configuration file

Display:

Parameter Description:

  • Filename: indicates the file path. It can be a relative path or an absolute path. It can be a.txt file or a.csv file
  • File encoding: File code. The default value is ANSI. Other codes can be configured based on actual conditions
  • Variables Names(comma-delimited) : Variable name, indicating that each column is assigned to a variable, separated by commas if there are multiple columns, and referenced by ${}
  • Ignore first line: Ignore the first line
  • Delimiter(use "\t" for tab): delimiter, comma by default
  • Allow quoted data?The default value is false. If there are quotation marks in the data, the data is assigned to the variable with quotation marks. If it’s Flase, the quotes are removed
  • Recycle on EOF?: Indicates whether to loop again when an end-of-file character is encountered. The default value is True, that is, continue from the beginning of the file
  • Stop thread on EOF?: Terminates the thread if it encounters an end-of-file character. The default value is False, that is, does not stop. When 【Recycle on EOF?】 When this parameter is set to True, this parameter is invalid. When theRecycle on EOF?< EOF > < EOF > < EOF > < EOF >
  • Sharing mode: Shared mode. The default isall threadsAnd also supportscurrent thread group / current thread / edit

Summary of sharing mode:

  • All treatds: The data in the CSV file is shared, regardless of the number of threads or cycles, will trigger the interface request parameters to use the variable data in different lines of the CSV file

  • Current Thread group: In a thread group, the number of threads and the number of loops trigger changes in interface request parameters

  • Current thread: The number of threads does not determine the number of cycles required to update the data in the CSV file

Parameter files use:

  1. Write the content

  1. To get the results

3. Display results:

Twelve, BeanShell

When creating data, you need to obtain the results and save them locally. There are two ways:

  • The first method saves the results locally directly by retrieving them
  • The second is exported through the database

Today, we’ll introduce the first one, which is obtained from regular expressions in JMeter:

Step one:The second step:Step 3:

Reference code:

FileOutputStream fps=new FileOutputStream("${outfile_online}".true);
OutputStreamWriter osw=new OutputStreamWriter(fps);
BufferedWriter bw=new BufferedWriter(osw);

bw.append("${mobile}\t${User}\t${User}\t${fd}\n");

if(bw! =null){bw.close(); }if(osw! =null){osw.close(); }if(fps! =null){fps.close(); }Copy the code

Step 4:

Get results:

Regular Expression Extractor

Written simply:Regular expression extractor:

  • Apply to: Application scope
  • Response field to examine: Sample data source.
  • Reference name: variable name used for reference elsewhere. I wrote phone here, customizable setting, reference method:${reference name}
  • Regular expression: data extractor, () parentheses are the values you want to get. Phone “:”(equivalent to LR left boundary,)”,”name” equivalent to LR right boundary. And in brackets\d+Is a regular expression, used to match the data needed to obtain, a regular expression will be explained at the end of the article
  • Template:$$Corresponds to the regular expression extractor type. -1 all, 0 random, 1 the first, 2 the second, and so on, if there is only one general fill inThe $1 $
  • Match numbers: All the results of the regular expression match data can be regarded as an array, and the match number can be regarded as the number of elements in the array. Minus 1 is all, 0 is random, 1 is the first, 2 is the second, and so on. If only the first matching value is obtained, enter 1
  • Default value: the default value when a match fails. I don’t have to write it. To use this parameter for subsequent logical judgment, simply write it as ERROR.

JMeter common shortcut key reference

shortcuts function note
Ctrl + C copy Replicable component
Ctrl + V paste Stickable component
Ctrl + Shift + C Copy and paste the current component onto the next line
Ctrl + R Run the test plan
Ctrl + E Clear running results
Ctrl + T Enable/disable the current component You can choose more
Ctrl + F Full-text search Generally used to Search components, after the Search is complete, click Search -> Reset Search to resume
Ctrl + – Pack up all “-” the minus sign on the main keyboard
Ctrl + Shift + – Expand all “-” the minus sign on the main keyboard
Ctrl + 0 Creating a thread group
Ctrl + 1 Adding HTTP Requests
Ctrl + 2 Regular expression extractor
Ctrl + 3 In response to assert
Ctrl + 4 Stationary timer
Ctrl + 5 Change Test Action 5.0 to Flow Control Action JMeter flow control actions
Ctrl + 6 JSR223 PostProcessor
Ctrl + 7 JSR223 PreProcessor (PreProcessor) vars.put(“key”, value) : Stores the value in the Jmeter variable key and references it with ${“key”} in subsequent requests.
Ctrl + 8 Debug Sampler
Ctrl + 9 View the result tree

Linux Quick start Reference

Reference code:

#! /bin/bash

#jmeter kill and start

echo -e '\033[32m--------Jmeter---------------\033[0m'
echo "Kill the jmeter-server process................... \n"
kill -9 `ps aux | grep jmeter | grep -v grep | awk '{print $2}'`
sleep 3
cd/ root/tools/apache - jmeter - 3.3 / bin /echo "Start jmeter - server"
nohup ./jmeter-server &
echo -e '\ [32 033 m -- -- -- -- -- -- -- -- -- -- -- -- welcome to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - \ [0 033 m'
Copy the code