jmeter压力测试值之配置JDBC Connection Configuration(一)

一、下载mysql jar包

下载mysql jar包

http://dev.mysql.com/downloads/connector/j/

网盘下载地址:mysql-connector-java-commercial-5.1.25-bin.jar

查看jar包支持的mysql版本:http://dev.mysql.com/doc/connector-j/en/connector-j-versions.html

查看mysql数据库版本:SELECT VERSION();

注:如果下载的jar包版本不支持mysql数据库,运行Jmter时可能会报错:no suitable driver found for jdbc:mysql//……..

为Jmeter添加jar文件

下载后解压.zip文件,把mysql-connector-java-x.x.x-bin.jar(例中:mysql-connector-java-commercial-5.1.25-bin.jar)放到Jmeter安装目录下的lib目录下(例中:D:Program Filesapache-jmeter-2.13lib)

2.   启动Jmeter,添加需要的原件,Sampler

二、配置JDBC Connection Configuration

重要参数说明:

  • Variable Name:数据库连接池的名称,我们可以有多个jdbc connection configuration,每个可以起个不同的名称,在jdbc request中可以通过这个名称选择合适的连接池进行使用。
  • Max Number of Connection: 数据库最大链接数
  • PoolTimeout: 数据库链接超时,单位ms
  • Idle Cleanup Interval (ms): 数据库空闲清理的间隔时间,单位ms
  • Auto Commit:自动提交。有三个选项,true、false、编辑(自己通过jmeter提供的函数设置)
  •   Transaction Isolation
    事务间隔级别设置,主要有如下几个选项:(对JMX加解密) 
    【TRANSACTION_NODE   事务节点 、
    TRANSACTION_READ_UNCOMMITTED  事务未提交读、
    TRANSACTION_READ_COMMITTED   事务已提交读 、
    TRANSACTION_SERIALIZABLE   事务序列化 、
    DEFAULT  默认、
    TRANSACTION_REPEATABLE_READ 事务重复读、
  • Database URL:数据库url,jdbc:mysql://主机ip或者机器名称:mysql监听的端口号/数据库名称, 如:jdbc:mysql://localhost:3306/test
  • JDBC Driver classJDBC驱动
  • username:数据库登陆的用户名
  • passwrod:数据库登陆的密码
下面是原文和翻译:

(1) Variable Name Bound to Pool

 

Variable Name,数据库连接池的变量名,之后JDBC request可以通过选择不同的连接池名来选择不同的数据库连接

Variable name变量名称:与JDBC Request的Variable name保持一致

原因:因为链接数据库是需要在JDBC Connection Configuration中配置好的,然后赋予一个变量名称,那么JDBC Request想要去访问数据库,就必须通过这个配置好的元件去链接,所以JDBC Reques就需要通过读取JDBC Connection Configuration的变量中的信息,

那怎么读取呢,JDBC Reques就需要用到这个变量了,就需要到名称为mysql的元件中去应用。

(2) Connection Pool Configuration

Max Number of Connections    该数据库连接池的最大连接数,一般可设置为0,意思是每个线程都使用单独的数据库连接,线程之间数据库连接不共享
中文:池中允许的最大连接数。在大多数情况下,将其设置为0,这意味着每个线程将得到它自己的池,其中只有一个连接,即线程之间不共享连接。如果您真的想要使用共享池(为什么?),那么将max count与线程数相同,以确保线程不会相互等待。
原文:aximum number of connections allowed in the pool. In most cases, set this to zero (0) . This means that each thread will get its own pool with a single connection in it, i.e. the connections are not shared between threads. If you really want to use shared pooling (why?), then set the max count to the same as the number of threads to ensure threads don't wait on each other.

Max Wait (ms)   在连接池中取回连接最大等待时间
中文:如果在试图检索连接过程中(取回连接)超过所设置期限,连接池抛出一个错误
原文:ool throws an error if the timeout period is exceeded in the process of trying to retrieve a connection

Time Between Eviction Runs (ms)     疏散时间
中文:在空闲对象驱逐线程运行期间,可以休眠的毫秒数。当非正值时,将运行无空闲对象驱逐器线程。(默认为“60000”,1分钟)(如果当前连接池中某个连接在空闲了time Between Eviction Runs Millis时间后任然没有使用,则被物理性的关闭掉。)
原文:The number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run. (Defaults to " 60000 ", 1 minute)

Auto Commit    自动提交sql语句
中文:打开或关闭连接的自动提交。
原文:Turn auto commit on or off for the connections.

(3) Connection Validation by Pool 

这是Jmeter用来检验数据库连接是否有效的一种机制,超过5秒没有使用的话,就会用validation query去测试下这个连接是否有效

Test While Idle   当空闲的时候测试连接是否断开
中文:测试连接池的空闲连接,验证查询将会被使用去测试。
原文:Test idle connections of the pool, see BasicDataSource.html#getTestWhileIdle . Validation Query will be used to test it.

Soft Min Evictable Idle Time(ms)   
中文:最少的时间连接可能在池中闲置,然后才有资格被闲置的对象驱逐出去,额外的条件是至少在池中保持连接。默认值为5000(5秒)
原文:Minimum amount of time a connection may sit idle in the pool before it is eligible for eviction by the idle object evictor, with the extra condition that at least minIdle connections remain in the pool. See BasicDataSource.html#getSoftMinEvictableIdleTimeMillis . Defaults to 5000 (5 seconds)
 

 

(4) Database Connection Configuration

 

Database URL: jdbc:mysql://服务器地址:3306/数据库名  

  (比如:jdbc:mysql://ip:3306/数据库名?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true)

    备注:“&allowMultiQueries=true” 这句话的意思呢  是为了在JDBC中执行多条select语句的时候用的!

JDBC Driver class:数据库JDBC驱动类名:com.mysql.jdbc.Driver

Username:数据库连接用户名

password:数据库连接密码

例如(上面url或class如有问题,可试一试下面的):

Database URL:  jdbc:mysql://localhost:3306/information_schema?serverTimezone=GMT%2B8

JDBC Driver class:  com.mysql.cj.jdbc.Driver

 

Username:root

password:xxx

 

  不同数据库具体的填写方式,可以参考下面的表格:

Datebase

Driver class

Database URL

MySQL

com.mysql.jdbc.Driver

jdbc:mysql://host:port/{dbname}

PostgreSQL

org.postgresql.Driver

jdbc:postgresql:{dbname}

Oracle

oracle.jdbc.driver.OracleDriver

jdbc:oracle:thin:user/pass@//host:port/service

Ingres (2006)

ingres.jdbc.IngresDriver

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

MSSQL

com.microsoft.sqlserver.jdbc.SQLServerDriver

或者

net.sourceforge.jtds.jdbc.Driver

jdbc:sqlserver://IP:1433;databaseName=DBname

或者

jdbc:jtds:sqlserver://localhost:1433/"+"library"

 

  填写后大致如下:

 

三、配置JDBC Request

重要的参数说明:

  • Variable Name:数据库连接池的名字,需要与JDBC Connection Configuration的Variable Name Bound Pool名字保持一致
  • Query:填写的sql语句未尾不要加“;”
  • Parameter valus:参数值
  • Parameter types:参数类型,可参考:Javadoc for java.sql.Types
  • Variable names:保存sql语句返回结果的变量名
  • Result variable name:创建一个对象变量,保存所有返回的结果
  • Query timeout:查询超时时间
  • Handle result set:定义如何处理由callable statements语句返回的结果

执行到这里,我们已经将数据从数据库中原样的查出来了,但具体如何之取出我们需要的数据呢,显然,假如我们查询的sql返回的只是一个数据,上面的方式已经可以满足我们的需求的,如我们查询数据的记录数,

select count(*) from test

  查询出来的结果就是我们需要的内容,或者通过正则表达式的获取即可获取我们的内容。

  但假如像上面那样子,我们获取出来的是多行数据,我们需要如何来对数据进行遍历,只获取出我们需要的数据呢?请看下面的分析。

原文地址:https://www.cnblogs.com/insane-Mr-Li/p/10098523.html