Note: How to use JMeter to operate MySQL database? You’ll understand instantly after reading it!

Recently, I used JMeter for interface testing and encountered a scenario that required the use of a data database: a page about data reports that needed to sum or average the data in the database before displaying it.

If you want to assert, you need to connect to the database and compare the SQL query results with the page results by writing SQL statements.

Taking the MySQL database as an example, the specific implementation steps are as follows:

一、加载JDBC驱动

二、连接数据库

三、数据库的查询(单值/多值引用)

1. Load the JDBC driver

1) Prepare the driver jar package locally

注意:驱动包的版本一定要与数据库的版本匹配,驱动版本太低可能导致连接报错。

1. Network disk download

Prepare the MySQL driver. If you cannot find it, you can download it from the network disk: link: https://pan.quark.cn/s/b59620ad7f30, extraction code: 6WWw, put the file in the JMeter installation folder

picture

2. Download from the official website

Enter the official website https://dev.mysql.com/downloads/connector/j/ and select: Platform Independent. Click Download, unzip and find the jar file, and place the file in the JMeter installation folder.

picture

picture

2) Import the driver package

In JMeter's test plan, find Add directory or jar to classespath, click Browse next to it, select the package, and import it.

picture

2. Connect to the database

1) Add component JDBC Connection Configuration

Select the thread group, right-click, add – Configuration Element – ​​JDBC Connection Configuration

picture

2) Placement JDBC Connection Configuration

picture

1. Name: empty by default, you can customize the name or leave it empty

2. Comment: empty by default, can be customized, or empty

3. Variable Name for created pool: A connection name, for example named s_mysql1, needs to be uniquely identified and needs to be used in subsequent JDBC requests, so it needs to be consistent with the name in the JDBC sampler. A simple understanding is that it needs to be determined when making a jdbc request. Which database to request.

4. Max Number of Connections: The maximum number of database connections allowed in the connection pool. The default is 10. When doing performance testing, it is recommended to fill in 0.

5. Max Wait(ms): The maximum waiting time to retrieve a connection from the connection pool, in milliseconds, the default is 10000, the default is enough

6. Time Between Eviction Runs (ms): The idle time of the thread, in milliseconds, the default is 60000, the default is enough 7.
Auto Commit: Automatically submit the sql statement, there are three options: True, False, Edit (function settings provided by JMeter) , the default is true, the default is fine

8. Transaction Isolation: Transaction isolation level, the default is DEFAULT, the default is enough

9. Test While Idle: Whether to disconnect when the connection is idle. The default is True. The default is OK.

10. Soft Min Evictable Idle Time (ms): The minimum idle time of the connection in the connection pool, in milliseconds. When the connections in the connection pool are idle and exceed the set value of "Soft Min Evictable Idle Time", the connection pool may choose to recycle these idle connections. The default is 5000. The default is fine.

11. Validation Query: Verify sql syntax, the default is select1, the default is enough

12. Database URL: Database connection URL, you can bring the character set characterEncoding=utf-8, you can also allow multiple SQL statements to execute allowMultiQueries=true, and you can also specify the time zone serverTimezone=UTC, such as jdbc:mysql://10.0.41.104: 33066/u_backend?serverTimezone=UTC&characterEncoding=utf-8&allowMultiQueries=true

13. JDBC Driver class: JDBC class, empty by default, required. For mysql, select com.mysql.jdbc.Driver

14. Username: Username of database

15. Password: database password

3. Database query (single value/multi-value reference)

1) Add JDBC Request

1. Select the simple controller, right-click and add –Sample–JDBC Request

picture

After the addition is successful, fill in the corresponding parameters:

Name: Custom

Variable Name of Pool declared in JDBC Connection Configuration: To be consistent with the Variable Name for created pool value in JDBC Connection Configuration

Query Type: Based on the filled in sql selection, select Select Statement for query, and Update Statement for update.

Variable names: Field values ​​corresponding to SQL query results. As many field values ​​as there are, there are as many corresponding values. For example, if SQL finds a field value reg_count, then the following will correspond to one variable count.

Handle ResultSet: The default is Store as String. Just select the default value. When this option is selected, the query results will be stored in variables in the form of strings.

Insert image description here

2) Add HTTP request

Add an HTTP request, fill in the name, HTTP request method, path, etc.

picture

3) Add response assertion

1. Reference a variable

The form of quoting a variable is the number of rows of the variable name, such as {variable name_number of rows}, for example, the number of rows of the variable name
,
such
 as {count_1}, which means referencing the data in the first row of the count field in the query result, KaTeX parse error: Expected group after '_' at position 5: {Variable name_̲#} represents the number of rows returned, for example {count_#} represents how many rows have been returned.

picture

2. Reference multiple variables

If there are multiple values ​​returned by the SQL query, for example, 5 rows of records are queried, each row has three fields, pid_reg_count corresponds to the variable count, pid_fee corresponds to the variable fee, and income corresponds to the variable income.

picture

picture

You can add a Debug Sample to see how each value is obtained. KaTeX parse error: Expected group after '_' at position 7: {count_̲#} represents the number of rows returned, {count_1} represents the first number of the count field returned The value of the row, ${count_2} represents the value of the second row of the returned count field.

fee1​ represents the value of the first row of the returned fee field, and {income_1} represents the value of the first row of the returned income field. . . And so on.

picture

That’s it for today’s sharing, I hope it can inspire you all.

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

Insert image description here

This information 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. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/133078932