mysqlslap 压力测试工具

 

背景

  由于一些不可描述的原因,我要确定一条业务SQL在给定的MySQL环境下的执行效率;比如说200个session同时跑同样一条SQL

  我们数据库的吞吐量怎么样?

 

  刚收到这个需求的时候,感觉这个要自己写一个程序才行了,还好身边有个老司机;他推荐用mysqlslap解决,所以也就有了这次

  对mysqlslap的学习。

 

mysqlslap介绍

  mysqlslap可以模拟若干个并发的MySQL客户端,来给MySQL做压力测试;官方文档中对mysqlslap的介绍非常的短(只有一页)

  https://dev.mysql.com/doc/refman/8.0/en/mysqlslap.html

 

mysqlslap实践

  表结构的定义,索引的组织都会性能数据库的性能;那么问题就来了,我们如何量化一个变化对性能的影响,比如说把个列的数据类型

  由int 改成biging 之后对性能的影响有多大?要得到两都之间的差异我们只要进行两次测试就行了,面对这样的需求mysqlslap就能解决

 

  回到正题,本文的立意不是向读者展示一次测试的过程,而是想展示mysqlslap中表现出来的性质,主要是讲表表现的与笔者期望不一致

  的地方

 

  我设计了如下表结构用于mysqlslap的性能测试

create table log(id int not null auto_increment primary key, 
                       connection_id int,      -- 连接的id
                       access_time datetime(6) -- 执行操作的时间
                       );

  执行的SQL语句如下

 

  1、开始第一次执行

mysqlslap --host=127.0.0.1 --password=3306 --user=root --password=xxxxxx --create="create table log(id int not null auto_increment primary key, connection_id int,access_time datetime(6));"  --query="insert into log(connection_id,access_time) values(connection_id(),now());" --concurrency=2
Benchmark
        Average number of seconds to run all queries: 0.031 seconds
        Minimum number of seconds to run all queries: 0.031 seconds
        Maximum number of seconds to run all queries: 0.031 seconds
        Number of clients running queries: 2
        Average number of queries per client: 1

  --create 用于指定初始化语句 我这里就是建了个表

  --query  用于指定要执行的语句,我这里就执行一条insert

  --concurrency 用于指定并发度, 这里指定了两个并发的连接

 

  2、看一下mysqlslap在数据库留下了什么

show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

  可以看到mysqlslap在执行完成后什么都没有留下,它把自己的库都给删除了 --no-drop 可以改变它的这一行为

mysqlslap --host=127.0.0.1 --password=3306 --user=root --password=mtls0352 --create=./create.sql --query=./query.sql --concurrency=2 --no-drop                                                        
mysqlslap: [Warning] Using a password on the command line interface can be insecure.                         
Benchmark
        Average number of seconds to run all queries: 0.032 seconds
        Minimum number of seconds to run all queries: 0.032 seconds
        Maximum number of seconds to run all queries: 0.032 seconds
        Number of clients running queries: 2
        Average number of queries per client: 1

  这个时候我们可以去数据库中查看一下mysqlslap给我们留下了什么

show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mysqlslap          |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

use mysqlslap;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------+
| Tables_in_mysqlslap |
+---------------------+
| log                 |
+---------------------+
1 row in set (0.00 sec)

mysql> show create table log;                                                                                
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                            |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| log   | CREATE TABLE `log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `connection_id` int(11) DEFAULT NULL,
  `access_time` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from log;                                                                                    
+----+---------------+----------------------------+
| id | connection_id | access_time                |
+----+---------------+----------------------------+
|  1 |            28 | 2018-09-01 12:36:05.000000 |
|  2 |            29 | 2018-09-01 12:36:05.000000 |
+----+---------------+----------------------------+
2 rows in set (0.00 sec)

   

  为了介绍mysqlslap中另一个比较重要的参数--iterations,我这里就不用mysqlslap这个数据库,我要自己建一个专门的数据库

create database tempdb;
use tempdb;

create table log(id int not null auto_increment primary key, 
                       connection_id int,      -- 连接的id
                       access_time datetime(6) -- 执行操作的时间
                       );

  改mysqlslap的写法如下

mysqlslap --host=127.0.0.1 --password=3306 --user=root --password=mtls0352  --query="insert into tempdb.log(connection_id,access_time) values(connection_id(),now());" --concurrency=2 --iterations=4
mysqlslap: [Warning] Using a password on the command line interface can be insecure.                         
Benchmark
        Average number of seconds to run all queries: 0.026 seconds
        Minimum number of seconds to run all queries: 0.023 seconds
        Maximum number of seconds to run all queries: 0.030 seconds
        Number of clients running queries: 2
        Average number of queries per client: 1

  查看mysqlslap中留下的日志

select * from tempdb.log;                                                                             
+----+---------------+----------------------------+
| id | connection_id | access_time                |
+----+---------------+----------------------------+
|  1 |            50 | 2018-09-01 12:48:58.000000 |
|  2 |            49 | 2018-09-01 12:48:58.000000 |
|  3 |            51 | 2018-09-01 12:48:58.000000 |
|  4 |            52 | 2018-09-01 12:48:58.000000 |
|  5 |            54 | 2018-09-01 12:48:58.000000 |
|  6 |            53 | 2018-09-01 12:48:58.000000 |
|  7 |            55 | 2018-09-01 12:48:58.000000 |
|  8 |            56 | 2018-09-01 12:48:58.000000 |
+----+---------------+----------------------------+
8 rows in set (0.00 sec)

  这里总共 8 行就是 --concurrency=2 与 --iterations=4 的乘积,从connection_id 可以看出mysqlslap并没有对连接进行重用。

 

 

 

我对mysqlslap的一些看法

  讲一讲mysqlslap的生命周期、每一轮测试中mysqlslap都会经历如下三个阶段

    1): 建立连接 创建数据库 执行初始化步骤

    2): 执行查询--query中执行的操作

    3): 删除mysqlslap数据库 断开连接

    在--iterations 大于1的情况下重复上面的步骤iterations次

 

  总的来说mysqlslap是一个看上去很美,用起来一点都不让人省心的工具,比较说--no-drop和--iterations一起出现的时候它就不知道变通了,

  不知道变通就算了,最要命的问题在于长一点的SQL只要把并发调高一点mysqlslap就会hang住,第一次我用它的时候是抱着绝对的信任的,

  结果等了半天没有反映,现在想想自己当时还是太年轻了 too young !

 

  总的来说我更加喜欢sysbench!

 

 

  

 

----

posted on 2018-08-29 10:59  蒋乐兴的技术随笔  阅读(494)  评论(0编辑  收藏  举报

导航