Article Directory
foreword
Apache JMeter is an open source Java application for performance testing of software. It was originally designed for web application testing, but has since expanded to other testing areas. It can be used to test static and dynamic resources such as static files, Java servlets, CGI scripts, Java objects, databases, FTP servers, and more.
JMeter provides a graphical user interface that allows testers to easily create and run test plans and view test results. JMeter can also simulate a variety of load types, including static and dynamic resources such as files, images, videos, databases, etc.
Official document address: https://jmeter.apache.org/usermanual/
github address: https://github.com/apache/jmeter
1. Concept principle
1.1 Principle of JMeter
The basic principle of Jmeter is to establish a thread pool, run the sampler in multiple threads to simulate user requests, verify the correctness of the results through assertions, and finally record the test results through the listener.
- X1-X5 These components are used to simulate user actions
- Y1 is responsible for simulating user requests, and Y2 is responsible for verifying that the results are correct
- Z is responsible for the collection of results
The overall sequence of execution is:
- configuration element
- Preprocessor
- timer
- sampler
- post processor
- Affirmation
- listener
If a controller is added, the execution order is also related to the controller and scope
1.2 Performance Test Terminology
the term | meaning | Remark |
---|---|---|
pressure test | Is a test method used to test the performance of the system under high load | By simulating a large number of users accessing the system, you can test the system's stability, reliability, and response time under high load. |
concurrent number | Generally speaking, it refers to the number of concurrent users. The number of users whose requests are being processed by the system at the same time. | In addition to the number of concurrent connections, each user may open multiple connections, so the number of concurrent connections may be greater than the number of concurrent users. |
throughput | The number of requests processed by the system per unit time | |
SWC | Queries Per Second, the number of queries per second | |
TPS | Transactions Per Second, the number of transactions processed per second | For a single interface, TPS can be considered equivalent to QPS. For example, accessing a page /index.html is a TPS, while accessing the /index.html page may request 3 times for servers such as css, js, and index interfaces. 3 QPS were generated. |
RT | Response Time, which represents the time difference from when the client initiates a request to when the server receives the request and responds to all data | Generally take the average response time. |
PV | Page View, that is, the number of page views, refers to the number of visits to a website or application within a certain period of time |
1.3 web project performance standards
The following are general Internet server performance criteria
dimension | Fail | pass |
---|---|---|
timeout probability | > 0.5‰ | <0.5‰ |
probability of error | > 0.5‰ | <0.5‰ |
TPS | < expected high peak | > expected high peak |
CPU utilization | > 75% | <75% |
response event | > greater than expected time | < expected time |
Load | Average CPU load per core > 1 | Average CPU load per core < 1 |
JVM memory usage | > 75% | < 75% |
Full GC frequency | Average < 0.5h1 times | Average > 0.5h1 times |
2. Common components
Common components of JMeter are as follows:
- Thread group: It is used to simulate the situation that users access the system concurrently, and parameters such as the number of threads and the number of cycles can be set.
- Sampler: The component that actually accesses the system in Jmeter, which belongs to the thread group
- Controller: used to control the execution sequence and the number of iterations of each element in the test plan.
- Timer: used to simulate the user's behavior when executing the test plan, for example, waiting for a period of time before executing the next request.
- Pre/Post Processors: Simulate user behavior while executing the test plan. A pre-processor can perform actions before issuing a Sampler Request, such as modifying the settings of a Sampler Request or updating variables not extracted from the response text. Post-processors can perform actions after a sampler request, such as extracting variables in the response text or writing response data to a file.
- Configuration elements: closely related to samplers, requests can be added or modified
- Listener: used to collect and display test results, such as response time, response code, response message, etc.
For the unknown options in the component, you can access the Internet for document query through Help in the menu bar
2.1 Thread Group
The following are some parameter configuration items explained:
-
Ramp-Up time
How long does JMeter take to start all threads. If there are 10 threads with a Ramp-Up time of 100 seconds, then each thread will start within 10 seconds of the previous thread starting, for a total time of 100 seconds (the first thread is always started directly) to make the test fully up to speed.
-
Defer creating threads until needed
If ticked, threads will only be created when an appropriate proportion of the boost time has elapsed. This is best suited for tests that have boost times that are much longer than the execution time of a single thread. That is, earlier threads end before later threads start. If not selected, all threads will be created at the start of the test (then they will be paused for the appropriate proportion of boost time).
-
Scheduler (Duration)
If checked, you can configure and choose a relative end time. JMeter will use this to calculate the end time.
2.2 Sampler
Each sampler produces one or more sample results with various attributes (success/failure, elapsed time, data size, etc.) and can be watched in different Listeners. Jmeter provides many samplers: as follows
The most common is the Http request
-
protocol
OptionalHTTP, HTTPS, FILE
-
content encoding
The content-encoding to use (for POST, PUT, PATCH and FILE). This is the character encoding to use and is independent of the Content-Encoding HTTP header.
-
browser-compatible headers
When using multipart/form-data, this will suppress the Content-Type and Content-Transfer-Encoding of the HTTP header, and only send Content-Disposition.
-
coding?
Encode special characters in url
-
contains equal to?
When passing a key-value, some applications do not expect an equals sign when the value is an empty string
2.3 Controller
When dealing with more complex test processes, it is necessary to control the overall test process similar to the if/for control sequence and number of cycles in programming languages. jmeter provides many controllers to facilitate testing of various scenarios, as shown below:
Commonly used controllers are:
- IF controller
- Only one controller login scenario is commonly used
- cycle controller
- random controller
2.4 Timers
There is no delay between Jmeter requests, but the actual work sometimes needs to increase the fixed time to test, and sometimes it is necessary to let the threads execute at the same time at a certain point in time, then the timer function can be used
2.5 Pre/Post Processor
-
A pre-processor performs some operations before a sampler request, a post-sampler does something after.
-
Common scenarios for pre-processors in HTTP requests include: assigning a different user id to each thread.
-
Post-processors are most commonly used to process response data.
2.6 Configuration elements
Config Element provides support for static data configuration, which can set default values and variables for samplers.
Store and send cookies like a web browser. If there is an HTTP request and the response contains a cookie, the cookie manager will automatically store the cookie, and then use the cookie in all subsequent requests for a particular web site. Can be viewed in the results tree.
2.7 Listeners
The listener is used to monitor the results, and can also view, save and read the saved test results.
The following takes the aggregation report as an example to display the attributes in the table
attribute name | meaning |
---|---|
Label | The sample tag name, the sampler is the Http request is the Http request name |
Samples | The number of samples with the same label, the sampler is the http request is the number of sent requests |
Average | Request response time, average |
Median | Response time from small to large, at the 50% position, 50% of the samples took no more than this time; the rest took at least the same amount of time. |
90% Line | at 90% position |
95% Line | at 95% position |
99% Line | At 99% position |
Min | minimum value |
Max | maximum value |
Error % | request error rate |
Throught | throughput |
Received KB/sec | The data rate received from the server KB/s |
Sent KB/sec | Data rate sent to the server, KB/s |
3. Plug-ins
3.1 Third-party plug-ins
Jmeter 作为一个开源插件,在某些功能上比不过商业软件LoadRunner,但是有增加插件功能,可以根据自己所需安装想要的插件。
3.2 录制插件
badboy 是一款用C++开发的测试工具,被设计用于测试和开发复杂的动态应用, 但是已经很久未更新了。
用过jmeter的人都知道,jmeter测试简单点的静态页面还成,脚本制作也就三两步就搞定了。但是要是制作复杂点的测试脚步就非常困难了,比如登录系统输入用户名和密码,什么函数、参数配置之类的,肯定会把你搞晕。而且网上 jmeter 相关复杂点的案例也非常少,它本身提供的帮助文档也只有一个很简单的例子,用处不大。有了badboy就不一样了,它可以提供像 loadrouner 一样的录屏功能,不需要你自己去配置什么协议、参数、cookie manager之类的,只要你把你的测试过程录制出来,然后save as jmeter 脚本格式就ok了
官网不能访问了,网上找到一个 badboy安装包:链接: https://pan.baidu.com/s/1Ae3tLuP3q7301SzyXDGw2A 提取码: jcg5
四、JSR223 脚本及函数
4.1 JSR223 脚本
jsr233 是 java 对脚本语言的支持规范,jmeter 支持脚本语言
-
BeanShell
-
一个小型的、免费的、可嵌入使用Java编写的具有对象脚本语言特性的Java源码解释器。
-
-
groovy
- 一种基于JVM的敏捷开发语言,它结合了Python、Ruby和Smalltalk]的许多强大的特性
-
javascript
4.2 Function variables
A JMeter function is a special value that can populate the field of any sampler or other element in the test, the syntax is as follows:
${__functionName(var1,var2,var3)}
The JMeter variable reference syntax is as follows:
${VARIABLE}
Documentation: https://jmeter.apache.org/usermanual/functions.html
5. Actual cases
5.1 Login pressure test
Prepare 100 users to log in, and perform pressure test on the login interface. The test project uses an open source project: Wei HR .
Here is a stored procedure that generates users:
DELIMITER $$
CREATE PROCEDURE generate_sql()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= 99 DO
SET @sql = CONCAT("INSERT INTO `vhr`.`hr` (`name`, `phone`, `telephone`, `address`, `enabled`, `username`, `password`, `userface`, `remark`) VALUES ('系统管理员', '18568887789', '029-82881234', '深圳南山', '1', 'admin", LPAD(i, 3, '0'), "', '$2a$10$ySG2lkvjFHY5O0./CPIE1OI8VJsuKYEzOYzqIa7AJR6sEgSzUFOAm', 'http://bpic.588ku.com/element_pic/01/40/00/64573ce2edc0728.jpg', NULL);");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
call generate_sql
Prepare user.csv, modify the code to remove the verification code part, and the final test throughput is 50 per second
5.2 Database pressure test
To perform pressure testing on the database, first configure the JDBC connection pool, add the driver jar package to the lib directory, restart Jmeter, and perform pressure testing
reference
- https://github.com/aliesbelik/awesome-jmeter
- Full-stack performance testing practice book Jmeter actual combat
- Two very useful testing tools jmeter and badboy