Jmeter connects to the database and cooperates with combined applications such as BeanShell and http request

1. Preparation before connection: download the jar package for connecting to postgresql via jdbc

Download link: https://jdbc.postgresql.org/download.html

After the download is complete, place it under /Apach Jmeter4.0/lib/ under the Jmeter installation path, and then restart Jmeter

Note: If there is no jar package or the version of the jar package is incorrect, it will prompt "could not load'org.postgresql.Driver'".

 

Two, configure the JDBC connection

1. Right-click the thread group -> Add -> Configuration element -> JDBC Connection Configuration

 

  • Variable Name for created pool: The name of the database connection pool. We can have multiple jdbc connection configurations, each of which can have a different name. You can use this name in the jdbc request to select the appropriate connection pool for use.
  • Max Number of Connections: The maximum number of connections in the connection pool. In most cases, set it to zero (0). This means that each thread will get its own pool, which contains a single connection, that is, no connection is shared between threads. If you really want to use a shared pool), then set the maximum number of links to be the same as the number of threads to ensure that threads will not wait for each other.
  • Max Wait (ms): Maximum overtime
  • Time Between Eviction Runs (ms): The time interval for clearing the idle connection pool.
  • Auto Commit: Whether the transaction automatically raises the price option. For example, if Oracle performs an update operation, the data will not be updated to the database if it does not commit. This option helps us to submit it automatically.
  • Transaction Isolation: JDBC transaction control database transaction control ADIC portal
    • TRANSACTION_NONE: unsupported transaction, TRANSACTION-NONE=0
    • TRANSACTION_READ_UNCOMMITEED: Allow dirty read, non-repeatable read and phantom read
    • TRANSACTION_READ_COMMITEED: Dirty reads are prohibited, non-repeatable reads and phantom reads are allowed
    • TRANSACTION_REPEATABLE_READ: Dirty reads and non-repeatable reads are prohibited, and phantom reads are allowed
    • TRANSACTION_SERILIZABLE: Dirty reads, non-repeatable reads and phantom reads are prohibited
    • DEFAULT: Set by JMETER. The default value is 1, in fact, TRANSACTION_READ_COMMITEED prohibits dirty reads, allows non-repeatable reads and phantom reads
  • Test While Idle: Test the idle connection of the pool, please refer to BasicDataSource.html#getTestWhileIdle . Query verification.
  • Soft Min Evictable Idle Time(ms):
  • Validation Query: Verify the syntax of SQL
  • Database URL: database address, the following is the connection method of different databases:
MySQL

Driver class

com.mysql.jdbc.Driver

Database URL

jdbc:mysql://host[:port]/dbname



PostgreSQL

Driver class

org.postgresql.Driver

Database URL

jdbc:postgresql:{dbname}



Oracle

Driver class

oracle.jdbc.OracleDriver

Database URL

jdbc:oracle:thin:@//host:port/service OR jdbc:oracle:thin:@(description=(address=(host={mc-name})(protocol=tcp)(port={port-no}))(connect_data=(sid={sid})))



Ingress (2006)

Driver class

ingres.jdbc.IngresDriver

Database URL

jdbc:ingres://host:port/db[;attr=value]

Microsoft SQL Server (MS JDBC driver)

Driver class

com.microsoft.sqlserver.jdbc.SQLServerDriver

Database URL

jdbc:sqlserver://host:port;DatabaseName=dbname

Apache Derby

Driver class

org.apache.derby.jdbc.ClientDriver

Database URL

jdbc:derby://server[:port]/databaseName[;URLAttributes=value[;…]]
  • JDBC Driver class: database type, the driver package of the database needs to be placed in the /lib directory, and the use of mysql depends on the download address of mysql-connector-java-8.0.13.jar
  • Username: username
  • Password: Password

2. Right-click the thread group -> Add -> Sampler -> JDBC Request

 

  • Variable Name of Pool declared in JDBC Connection Configuration (required): The name of the JMeter variable bound to the connection pool. This must be consistent with the "Variable Name" field of the JDBC connection configuration.
  • Query Type: query type, set the query type according to the statement
    • Select Statement: Used when executing a query statement. You can use ${} to introduce parameters, not placeholders for parameterization
    • Update Statement: Used when performing update operations, including insertion and modification. You can use ${} to introduce parameters, but not placeholders for parameterization.
    • Callable Statement: Corresponding to the CallableStaenent object in the JDBC specification, it provides a way for all DBMSs to call a stored procedure in a standard form. In the vernacular, the stored procedure can be called, that is, it can have input parameters and placeholders. You can use ${} to introduce parameters, and you can also use placeholders to introduce parameters.
    • Prepared Select Statement: Precompiled query statement is more efficient than Select Statement. You can use ${} to introduce parameters and placeholders to introduce parameters.
    • Prepared Update Statement: Pre-compiled update statement, including query and insert, is more efficient with Update Statement, and supports placeholders to introduce parameters. You can use ${} to introduce parameters, and you can use placeholders to introduce parameters.
    • Commit: The content in the current connection state is submitted. The SQL in the query statement is submitted immediately after the execution is completed. When this item is used, the Auto commint option in the JDBC Connection Configuration is False. You can use ${} to introduce parameters, and you can use placeholders to introduce parameters.
    • Rollback: The content in the current connection state can be rolled back. You can use ${} to introduce parameters, and you can use placeholders to introduce parameters.
    • Autocommit(false): Specify not to submit automatically. If Auto commint in JDBC Connection Configuration is true, override this option. You can use ${} to introduce parameters, and you can use placeholders to introduce parameters.
    • Autocommit(true): Indicates that automatic price increases are allowed. You can use ${} to introduce parameters, and you can use placeholders to introduce parameters.
    • Edit: Variable application, which refers to any of the above variables
  • SQL Query SQL query statement, no need to enter the end of the line
  • Parameter values: parameter values, used for multiple values, split
  • Parameter types: parameter types, INTEGER, DATE, VARCHAR, DOUBLE
  • Variable Names: SQL execution statements often return a data set, we can save it in the variable, and let the subsequent components call.
  • Result variable name: The number of variables in the above Variable Names corresponds to the queue. Result variable name creates an array to save all the returned results.
  • Query timeout: define the query time
  • Handle result set: Define how to handle the results returned by the callable-statements statement

3. Simple query of instance operation

1. Connect to the postgresql database

  • Define the database connection pool name: MYSQL
  • URL: connect to postgresql configuration, format: jdbc:postgresql://ip:port/dbname, such as jdbc:postgresql://40.98.151.20:5432/away
  • JDBC Driver class:org.postgresql.Driver
  • Username: username
  • Password: login password

2. Create a new query for a field in the ad table

3. Query results:

 

4. Query parameterization of instance operation

1. Parameterization of a single query

Statement:

select * from booking_domain where id= ${id}

View query results:

2. Parameterization of multiple queries

Statement:

select * from booking_domain where id= ${id} and name= ${name}

search result:

5. Reference return result of instance operation

1. Save variable query statements by setting Variable Names

select id,name,type from booking_domain where id= ${id} and name= ${name}

There are 3 returned values ​​specified in our query, which means that 3 columns will be returned. Set A, B, C in Variable Names. A saves the data in the first column, B saves the data in the second column and so on. Then the following variables will be set to.

A_#=2 (总行数)

A_1=第1列, 第1行

A_2=第1列, 第2行

C_#=2 (总行数)

C_1=第3列, 第1行

C_2=第3列, 第2行

If the return result is 0, then A_# and C_# will be set to 0, and other variables will not be set. If 6 rows of data are returned for the first time and only 3 rows of data are returned for the second time, then the extra 3 rows of data variables will be cleared for the first time. You can use ${A_#}, ${A_1}... to get the corresponding value

Script code:

log.info("${A_#}"); //第一列有多少行
log.info("${B_#}"); //第二列有多少行
log.info("${C_#}"); //第三列有多少行
log.info("${A_1}"); //第一列第一行的值
log.info("${B_1}"); //第二列第一行的值
log.info("${C_1}"); //第三列第一行的值

Use Beanshell Sampler to output the results:

Log results:

There is indeed only this one in the actual database:

Request return result:

2. Save the result through Result variable name, Result variable name will save the returned result as an array, and get the value through vars.getObject("result").get(0).get("name")

Script code:

allvalue = vars.getObject("result"); //获取全部的值
value1 = vars.getObject("result").get(0); //获取列表你内下标为0的值
valuekey = vars.getObject("result").get(0).get("name"); //获取列表你内下标为0的值
log.info("全部的返回结果" + allvalue); //第一列有多少行
log.info("数组内下标为1的值" + value1);
log.info("数组内下标为1的值的域名" + valuekey);

Use Beanshell Sampler to output the results:

Results of the:

Additional: Add a combination operation here, which is to put the result of the database query in the request parameter:

 Use the result in the http component, execute vars.getObject("result").get(0).get("name") through the function __jexl3()

which is:

${__jexl3(vars.getObject("result").get(0).get("name"))}

Results of the:

Six, the combination of variables defined in the Test Plan and Bean Shell

1. Define the following three variables in the Test Plan:

2. Bean Shell script:

  a. Bean shell can accept incoming parameters, as shown in the figure below: ${u1} ${u2} ${u3}, with spaces in between

  b. Parameters can be extracted in order through bsh.args[]

  c. The bean shell provides a built-in variable Parameters to save a collection of parameters

Script code:

// 获取参数传递过来的值并存入变量
vars.put("v1",bsh.args[0]);
vars.put("v2",bsh.args[1]);
vars.put("v3",bsh.args[2]);
// 获取参数传递过来的变量集合
vars.put("v4",Parameters);
// 输出变量集合
log.info(Parameters);
// 输出该元件的name
log.info(Label);
// 设置响应代码
ResponseCode = 500;
// 设置响应信息
ResponseMessage = "This is a test";
// 设置是否成功
IsSucces = false;
// 设置响应数据
SampleResult.setResponseData("Hello lucas");

operation result:

The two sentence settings entered by 1 in the figure below:

ResponseCode = 500;

ResponseMessage = "This is a test";

The two sentence settings entered by 2 in the figure below:

log.info(Parameters);

log.info(Label);

 

 

 

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/108128295