06构建一个数据库测试

Created Thursday 29 June 2017

In this section, you will learn how to create a basic Test Plan to test a database server. You will create fifty users that send 2 SQL requests to the database server. Also, you will tell the users to run their tests 100 times. So, the total number of requests is (50 users) x (2 requests) x (repeat 100 times) = 10’000 JDBC requests. To construct the Test Plan, you will use the following elements: Thread Group, JDBC Request, Summary Report.
本章,你将学习如何创建一个基础的对数据库服务器的测试计划,你将会创建50个用户,发送2SQL请求到数据库服务器,而且,你会告诉用户运行测试100次。所以总的请求数是(50 users) x (2 requests) x (repeat 100 times) = 10’000 JDBC requests。为了构造这个测试计划,你将会使用下面的元素:Tread Group,JDBC Request,Summary Report。

This example uses the MySQL database driver. To use this driver, its containing .jar file (ex. mysql-connector-java-X.X.X-bin.jar) must be copied to the JMeter ./lib directory (see JMeter’s Classpath for more details).
这个例子使用MySQL数据库驱动,为了使用这个驱动,包含该驱动的jar文件要复制到JMeter的lib目录下面。

6.1 Adding Users

The first step you want to do with every JMeter Test Plan is to add a Thread Group element. The Thread Group tells JMeter the number of users you want to simulate, how often the users should send requests, and the how many requests they should send.
每个JMeter测试计划,你要做的i第一步都是添加一个Thread Group元素,Tread Group告诉JMeter你想要模拟多少个用户,用户发送请求的多久一次,用户应用发送多少次请求。

Go ahead and add the ThreadGroup element by first selecting the Test Plan, clicking your right mouse button to get the Add menu, and then select Add → ThreadGroup.
回过头来,先通过选中Test Plan来添加ThreadGroup 元素,点击你的鼠标右键到添加菜单,然后选择Add → ThreadGroup。

You should now see the Thread Group element under Test Plan. If you do not see the element, then “expand” the Test Plan tree by clicking on the Test Plan element.
你应该能够在测试计划下面看到Thread Group元素,如果不能看见这个元素,可以通过点击测试计划元素来“展开”测试计划树。

Next, you need to modify the default properties. Select the Thread Group element in the tree, if you have not already selected it. You should now see the Thread Group Control Panel in the right section of the JMeter window (see Figure 6.1 below)

接下来,你需要修改默认属性,如果你还没有选中线程组的话,那么请选中,那么你就可以在JMeter窗口的右边部分看到线程组的控制面板。

创建名为test的数据库 创建一个test数据库_测试


Figure 6.1. Thread Group with Default Values

Start by providing a more descriptive name for our Thread Group. In the name field, enter JDBC Users.
先给我们的线程组去一个形象的名字,在名称字段,输入JDBC Users。

You will need a valid database, database table, and user-level access to that table. In the example shown here, the database is ‘cloud’ and the table name is ‘vm_instance’.
你需要一个有效的数据库,数据表,和用户对表的访问权,例子中所示,数据库为‘cloud’,表名为‘vm_instance’。

Next, increase the number of users to 50.
接下来,增长用户数到50。

In the next field, the Ramp-Up Period, leave the value of 10 seconds. This property tells JMeter how long to delay between starting each user. For example, if you enter a Ramp-Up Period of 10 seconds, JMeter will finish starting all of your users by the end of the 10 seconds. So, if we have 50 users and a 10 second Ramp-Up Period, then the delay between starting users would be 200 milliseconds (10 seconds / 50 users = 0.2 user per second). If you set the value to 0, then JMeter will immediately start all of your users.
下一个字段Ramp-Up Period,保留为10s。这个属性告诉JMeter每个用户启动之间有多少的延迟。例如,如果你输入Ramp-Up Period为10s,那么JMeter会在10s结束时完成所有的用户的启动,所以,如果我们有50个用户,10s的Ramp-Up Period,那么每个用户之间的延迟是200ms(10/50=200ms)。如果你设置为0,那么JMeter会立即启动所有的用户。

Finally, enter a value of 100 in the Loop Count field. This property tells JMeter how many times to repeat your test. To have JMeter repeatedly run your Test Plan, select the Forever checkbox.
最后,输入Loop Count字段为100,这个属性告诉JMeter重复多少次测试,如果想让JMeter反复的运行你的测试计划,选中Forever复选框。

In most applications, you have to manually accept changes you make in a Control Panel. However, in JMeter, the Control Panel automatically accepts your changes as you make them. If you change the name of an element, the tree will be updated with the new text after you leave the Control Panel (for example, when selecting another tree element).在大多数应用中,你要对你在控制面板中做的修改手动确认,而在JMeter中,控制面板会自动的接受你做的修改。如果你修改了元素名,在你离开了控制面板后,测试树就会用新的文本更新(例如,当你选中另一个树上元素的时候)。

See Figure 6.2 for the completed JDBC Users Thread Group.

创建名为test的数据库 创建一个test数据库_jmeter_02


Figure 6.2. JDBC Users Thread Group

6.2 Adding JDBC Requests

Now that we have defined our users, it is time to define the tasks that they will be performing. In this section, you will specify the JDBC requests to perform.
我们已经定义了我们的用户,是时候定义我们将要执行的任务了,本节,我们会指定要执行的JDBC请求。

Begin by selecting the JDBC Users element. Click your right mouse button to get the Add menu, and then select Add → Config Element → JDBC Connection Configuration. Then, select this new element to view its Control Panel (see Figure 6.3).
先选中JDBC Users元素,点击鼠标右键,选择Add菜单,然后选择Add -> Config Element - JDBC Connection Configuration。然后选中这个新元素来查看它的控制面板。

Set up the following fields (these assume we will be using a MySQL database called ‘cloud’):
设置下面的字段(假设我们将会使用一个叫’cloud’的MySQL数据库):

  • Variable name (here: myDatabase) bound to pool. This needs to uniquely identify the configuration. It is used by the JDBC Sampler to identify the configuration to be used.
  • 绑定到池的变量名。这需要唯一配置标识,JDBC Sample会用来去标识要使用的配置。
  • Database URL: jdbc:<mysql://ipOfTheServer:3306/cloud>
  • 数据库URL:jdbc:<mysql://ipOfTheServer:3306/cloud>
  • JDBC Driver class: com.mysql.jdbc.Driver
  • JDBC驱动类:com.mysql.jdbc.Driver
  • Username: the username of database
  • 用户名:数据库用户名
  • Password: password for the username
  • 密码:用户名的密码

The other fields on the screen can be left as the defaults.
屏幕中的其他字段可以保留为默认。

JMeter creates a database connection pool with the configuration settings as specified in the Control Panel. The pool is referred to in JDBC Requests in the ‘Variable Name’ field. Several different JDBC Configuration elements can be used, but they must have unique names. Every JDBC Request must refer to a JDBC Configuration pool. More than one JDBC Request can refer to the same pool.

JMeter使用在控制面板中的配置设置来创建一个数据库连接池,这个池会通过‘Variable Name’字段关联JDBC请求。可以使用多个不同的JDBC Configuration元素,但是必须使用唯一的名字,每个JDBC请求必须引用一个JDBC配置池,可以多个JDBC请求引用同一个池。

创建名为test的数据库 创建一个test数据库_jmeter_03


Figure 6.3. JDBC ConfigurationSelecting the JDBC Users element again. Click your right mouse button to get the Add menu, and then select Add → Sampler → JDBC Request. Then, select this new element to view its Control Panel (see Figure 6.4).

再次选中JDBC Users元素,点击鼠标右键到添加菜单,然后选择Add → Sampler → JDBC Request,然后选中新的元素来查看它的控制面板。

创建名为test的数据库 创建一个test数据库_Group_04


Figure 6.4. JDBC Request

In our Test Plan, we will make two JDBC requests. The first one is for select all ‘Running’ VM instances, and the second is to select ‘Expunging’ VM instance (obviously you should change these to examples appropriate for your particular database). These are illustrated below.
在我们的测试计划中,我们会制作两个JDBC请求。第一个是查询所有的‘运行中’虚拟实例,第二个是查询‘消除’的虚拟实例(很明显你需要修改例子来适应你自己的数据库),下面会介绍。

JMeter sends requests in the order that you add them to the tree.
JMeter按照你在树上添加他们的顺序发送请求。

Start by editing the following properties (see Figure 6.5):
先来编辑以下属性:

  • Change the Name to ‘VM Running’.
  • 修改名称为‘VM Running’。
  • Enter the Pool Name: ‘myDatabase’ (same as in the configuration element)
  • 输出数据库池的名称(和配置元素保持相同)
  • Enter the SQL Query String field.
  • 输入SQL查询字符字段
  • Enter the Parameter values field with ‘Running’ value.
  • 用‘Running’输入到参数值
  • Enter the Parameter types with ‘VARCHAR’.
  • 用‘VARCHAR’输入到参数类型

创建名为test的数据库 创建一个test数据库_测试_05


Figure 6.5. JDBC Request for the first SQL request

Next, add the second JDBC Request and edit the following properties (see Figure 6.6):
接下来,添加第二个JDBC请求并编辑下面的属性

  • Change the Name to ‘VM Expunging’.
  • 修改名称为‘VM Expunging’
  • Change the value of Parameter values to ‘Expunging’.
  • 修改参数值为‘Expunging’

创建名为test的数据库 创建一个test数据库_Test_06


Figure 6.6. JDBC Request for the second request

6.3 Adding a Listener to View/Store the Test Results

The final element you need to add to your Test Plan is a Listener. This element is responsible for storing all of the results of your JDBC requests in a file and presenting the results.

最后你需要加到测试计划的元素是一个监听器。监听器负责保存所有的JDBC请求结果到文件中并展示结果。

Select the JDBC Users element and add a Summary Report listener (Add → Listener → Summary Report).

选中JDBC Users元素,然后添加一个Summary Report 监听器。

Save the test plan, and run the test with the menu Run → Start or Ctrl + R

保存测试计划,并通过菜单Run -> Start或者Ctrl + R来运行测试。

The listener shows the results.

监听器显示结果。

创建名为test的数据库 创建一个test数据库_jmeter_07


Figure 6.7. Graph results Listener