MySQL 性能测试&压力测试-mysqlslap

MySQLSlap是从MySQL的5.1.4版开始就开始官方提供的压力测试工具。MySQLSlap通过模拟多个并发客户端并发访问MySQL来执行压力测试,同时提供了较详细的SQL执行数据性能报告,并且能很好的对比多个存储引擎(MyISAM,InnoDB等)在相同环境下的相同并发压力下的性能差别。 MySQLSlap特点是语法简洁,使用方便,可对mysql进行针对性的压力测试,一般情况下足够。但是如果需要测试硬件、系统性能,或者对数据库性能要求较高,需要对数据库进行长时间的高并发、压力测试,那就最好使用Sysbench等工具。

  • 工具的打开姿势
  • 工具的常用参数举例
  • 使用实例

一、工具的打开姿势

如果使用rpm安装,下载mysql client rpm包安装后可直接使用 其他方式安装查看mysql中是否存在mysqlslap,在mysql的bin文件中查看是否存在mysqlslap即可: /usr/local/mysql/bin 目录下查看是否有mysqlslap -,有说明已经存在

注意:如果执行mysqlslap -help 出现:
-bash: mysqlslap: command not found
说明没有快捷方式:执行 ln -fs /usr/local/mysql/bin/mysqlslap   /usr/bin/mysqlslap 即可

二、工具的常用参数举例

通过mysqlslap –help可以获得可用的选项,这里列一些主要的参数,更详细的说明参考官方手册。

常用参数:(,后面是命令缩写)
--create-schema=name
# 指定测试的数据库名,默认是mysqlslap
--engine=name , -e name  
# 创建测试表所使用的存储引擎,可指定多个
--query=name,-q 
# 查询脚本,可以是SQL语句,也可以调用存储过程
--concurrency=N , -c N 
# 表示并发量,模拟N个客户端并发执行。可指定多个值,以逗号或者
--number-of-queries=N
# 总的测试查询次数(并发客户数×每客户查询次数),比如并发是10,总次数是100,那么10个客户端各执行10个
--iterations=N ,-i N
# 迭代执行的次数,即重复的次数(相同的测试进行N次,求一个平均值),指的是整个步骤的重复次数,包括准备数据、测试load、清理
--commit=N
# 执行N条DML后提交一次
--auto-generate-sql, -a 
# 自动生成测试表和数据,表示用mysqlslap工具自己生成的SQL脚本来测试并发压力。
--auto-generate-sql-load-type=name 
# 测试语句的类型。代表要测试的环境是读操作还是写操作还是两者混合的。
# 取值包括:read (scan tables), write (insert into tables), key (read primary keys), update (update primary keys), or mixed (half inserts, half scanning selects). 默认值是:mixed.
--auto-generate-sql-add-auto-increment 
# 对生成的表自动添加auto_increment列,从5.1.18版本开始支持。
--number-char-cols=name  ,-x N
# 自动生成的测试表中包含N个字符类型的列,默认1
--number-int-cols=name   -y N
#  自动生成的测试表中包含N个数字类型的列,默认1
--debug-info, -T 
# 打印内存和CPU的信息
--csv[=name] 
#生产CSV格式数据文件 (可以用来进行更一步的数据分析哦~)

查看帮助信息 mysqlslap –help

[root@Betty libmysql]# mysqlslap --help

mysqlslap  Ver 1.0 Distrib 5.6.10, for Linux (x86_64)

Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Run a query multiple times against the server.

Usage: mysqlslap [OPTIONS]

Default options are read from the following files in the given order:

/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

The following groups are read: mysqlslap client
The following options may be given as the first argument:

--print-defaults        Print the program argument list and exit.

--no-defaults           Don't read default options from any option file,

                        except for login file.

--defaults-file=#       Only read default options from the given file #.

--defaults-extra-file=# Read this file after the global files are read.

--defaults-group-suffix=#

                        Also read groups with concat(group, suffix)

--login-path=#          Read this path from the login file.

  -?, --help          Display this help and exit.

  -a, --auto-generate-sql 自动生成测试表和数据

                      Generate SQL where not supplied by file or command line.

  --auto-generate-sql-add-autoincrement 增加auto_increment一列

                      Add an AUTO_INCREMENT column to auto-generated tables.

  --auto-generate-sql-execute-number=# 自动生成的查询的个数

                      Set this number to generate a set number of queries to

                      run.

  --auto-generate-sql-guid-primary 增加基于GUID的主键

                      Add GUID based primary keys to auto-generated tables.

  --auto-generate-sql-load-type=name 测试语句的类型。取值包括:read,key,write,update和mixed(默认)

                      read:查询 write:插入 key:读主键 update:更新主键 mixed:一半插入一半查询

                      Specify test load type: mixed, update, write, key, or

                      read; default is mixed.

  --auto-generate-sql-secondary-indexes=# 增加二级索引的个数,默认是0

                      Number of secondary indexes to add to auto-generated

                      tables.

  --auto-generate-sql-unique-query-number=# 不同查询的数量,默认值是10

                      Number of unique queries to generate for automatic tests.

  --auto-generate-sql-unique-write-number=# 不同插入的数量,默认是100

                      Number of unique queries to generate for

                      auto-generate-sql-write-number.

  --auto-generate-sql-write-number=# 

                      Number of row inserts to perform for each thread (default

                      is 100).

  --commit=#          多少条DML后提交一次

                      Commit records every X number of statements.

  -C, --compress      如果服务器和客户端支持都压缩,则压缩信息传递

                      Use compression in server/client protocol.

  -c, --concurrency=name 模拟N个客户端并发执行select。可指定多个值,以逗号或者 --delimiter 参数指定的值做为分隔符

                      Number of clients to simulate for query to run.

  --create=name       指定用于创建表的.sql文件或者字串

                      File or string to use create tables.

  --create-schema=name 指定待测试的数据库名,MySQL中schema也就是database,默认是mysqlslap

                      Schema to run tests in.

  --csv[=name]        Generate CSV output to named file or to stdout if no file

                      is named.

  -#, --debug[=#]     This is a non-debug version. Catch this and exit.

  --debug-check       Check memory and open file usage at exit.

  -T, --debug-info    打印内存和CPU的信息

                      Print some debug info at exit.

  --default-auth=name Default authentication client-side plugin to use.

  -F, --delimiter=name 文件中的SQL语句使用分割符号

                      Delimiter to use in SQL statements supplied in file or

                      command line.

  --detach=#          每执行完N个语句,先断开再重新打开连接

                      Detach (close and reopen) connections after X number of

                      requests.

  --enable-cleartext-plugin 

                      Enable/disable the clear text authentication plugin.

  -e, --engine=name   创建测试表所使用的存储引擎,可指定多个

                      Storage engine to use for creating the table.

  -h, --host=name     Connect to host.

  -i, --iterations=#  迭代执行的次数

                      Number of times to run the tests.

  --no-drop           Do not drop the schema after the test.

  -x, --number-char-cols=name 自动生成的测试表中包含多少个字符类型的列,默认1

                      Number of VARCHAR columns to create in table if

                      specifying --auto-generate-sql.

  -y, --number-int-cols=name 自动生成的测试表中包含多少个数字类型的列,默认1

                      Number of INT columns to create in table if specifying

                      --auto-generate-sql.

  --number-of-queries=# 总的测试查询次数(并发客户数×每客户查询次数)

                      Limit each client to this number of queries (this is not

                      exact).

  --only-print        只输出模拟执行的结果,不实际执行

                      Do not connect to the databases, but instead print out

                      what would have been done.

  -p, --password[=name] 

                      Password to use when connecting to server. If password is

                      not given it's asked from the tty.

  --plugin-dir=name   Directory for client-side plugins.

  -P, --port=#        Port number to use for connection.

  --post-query=name   测试完成以后执行的SQL语句的文件或者字符串 这个过程不影响时间计算

                      Query to run or file containing query to execute after

                      tests have completed.

  --post-system=name  测试完成以后执行的系统语句 这个过程不影响时间计算

                      system() string to execute after tests have completed.

  --pre-query=name    测试执行之前执行的SQL语句的文件或者字符串 这个过程不影响时间计算

                      Query to run or file containing query to execute before

                      running tests.

  --pre-system=name   测试执行之前执行的系统语句 这个过程不影响时间计算

                      system() string to execute before running tests.

  --protocol=name     The protocol to use for connection (tcp, socket, pipe,

                      memory).

  -q, --query=name    指定自定义.sql脚本执行测试。例如可以调用自定义的一个存储过程或者sql语句来执行测试

                      Query to run or file containing query to run.

  -s, --silent        不输出

                      Run program in silent mode - no output.

  -S, --socket=name   The socket file to use for connection.

  --ssl               Enable SSL for connection (automatically enabled with

                      other flags).

  --ssl-ca=name       CA file in PEM format (check OpenSSL docs, implies

                      --ssl).

  --ssl-capath=name   CA directory (check OpenSSL docs, implies --ssl).

  --ssl-cert=name     X509 cert in PEM format (implies --ssl).

  --ssl-cipher=name   SSL cipher to use (implies --ssl).

  --ssl-key=name      X509 key in PEM format (implies --ssl).

  --ssl-crl=name      Certificate revocation list (implies --ssl).

  --ssl-crlpath=name  Certificate revocation list path (implies --ssl).

  --ssl-verify-server-cert 

                      Verify server's "Common Name" in its cert against

                      hostname used when connecting. This option is disabled by

                      default.

  -u, --user=name     User for login if not current user.

  -v, --verbose       输出更多的信息

                      More verbose output; you can use this multiple times to

                      get even more verbose output.

  -V, --version       Output version information and exit.

[root@Betty libmysql]#
说明:
测试的过程需要生成测试表,插入测试数据,这个mysqlslap可以自动生成,默认生成一个mysqlslap的schema,
如果已经存在则先删除。可以用--only-print来打印实际的测试过程,整个测试完成后不会在数据库中留下痕迹

三、工具的常用参数举例

单线程测试。-a自己生成的SQL脚本来测试并发压力。

[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a -uroot -p
Enter password:
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: 1
        Average number of queries per client: 0
使用 --only-print 选项,可以查看 mysqlslap 在测试过程中如何执行的 sql 语句。
在这种方式下,仅会对数据库进行模拟操作。如下显示的自动产生测试表和数据的情况下,mysqlslap 的执行过程 如下
[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a --only-print
DROP SCHEMA IF EXISTS `mysqlslap`;
CREATE SCHEMA `mysqlslap`;
use mysqlslap;
CREATE TABLE `t1` (intcol1 INT(32) ,charcol1 VARCHAR(128));
INSERT INTO t1 VALUES (1804289383,'mxvtvmC9127qJNm06sGB8R92q2j7vTiiITRDGXM9ZLzkdekbWtmXKwZ2qG1llkRw5m9DHOFilEREk3q7oce8O3BEJC0woJsm6uzFAEynLH2xCsw1KQ1lT4zg9rdxBL');
.........
.........
.........
INSERT INTO t1 VALUES (364531492,'qMa5SuKo4M5OM7ldvisSc6WK9rsG9E8sSixocHdgfa5uiiNTGFxkDJ4EAwWC2e4NL1BpAgWiFRcp1zIH6F1BayPdmwphatwnmzdwgzWnQ6SRxmcvtd6JRYwEKdvuWr');
DROP SCHEMA IF EXISTS `mysqlslap`;

多线程测试。使用–concurrency来模拟并发连接。


[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a -c 100 -uroot -p
Enter password:
Benchmark
        Average number of seconds to run all queries: 0.185 seconds
        Minimum number of seconds to run all queries: 0.185 seconds
        Maximum number of seconds to run all queries: 0.185 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

迭代测试。用于需要多次执行测试得到平均值。

[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a -i 10 -uroot -p
Enter password:
Benchmark
        Average number of seconds to run all queries: 0.040 seconds
        Minimum number of seconds to run all queries: 0.027 seconds
        Maximum number of seconds to run all queries: 0.065 seconds
        Number of clients running queries: 1
        Average number of queries per client: 0

测试同时不同的存储引擎的性能进行对比:

[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-info -uroot -p
Enter password:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.079 seconds
        Minimum number of seconds to run all queries: 0.077 seconds
        Maximum number of seconds to run all queries: 0.083 seconds
        Number of clients running queries: 50
        Average number of queries per client: 20

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.087 seconds
        Minimum number of seconds to run all queries: 0.082 seconds
        Maximum number of seconds to run all queries: 0.093 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.210 seconds
        Minimum number of seconds to run all queries: 0.193 seconds
        Maximum number of seconds to run all queries: 0.225 seconds
        Number of clients running queries: 50
        Average number of queries per client: 20

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.181 seconds
        Minimum number of seconds to run all queries: 0.155 seconds
        Maximum number of seconds to run all queries: 0.199 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10

User time 0.88, System time 0.88
Maximum resident set size 13648, Integral resident set size 0
Non-physical pagefaults 37239, Physical pagefaults 0, Swaps 0
Blocks in 0 out 40, Messages in 0 out 0, Signals 0
Voluntary context switches 98303, Involuntary context switches 425

执行一次测试,分别50和100个并发,执行1000次总查询:


[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --debug-info -uroot -p
Enter password:
Benchmark
        Average number of seconds to run all queries: 0.236 seconds
        Minimum number of seconds to run all queries: 0.236 seconds
        Maximum number of seconds to run all queries: 0.236 seconds
        Number of clients running queries: 50
        Average number of queries per client: 20

Benchmark
        Average number of seconds to run all queries: 0.194 seconds
        Minimum number of seconds to run all queries: 0.194 seconds
        Maximum number of seconds to run all queries: 0.194 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10

User time 0.10, System time 0.08
Maximum resident set size 12764, Integral resident set size 0
Non-physical pagefaults 5454, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 8846, Involuntary context switches 15

50和100个并发分别得到一次测试结果(Benchmark),并发数越多,执行完所有查询的时间越长。为了准确起见,可以多迭代测试几次:

[user_121@WEB-TEST-121MAI /etc]$ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --debug-info -uroot -p
Enter password:
Benchmark
        Average number of seconds to run all queries: 0.203 seconds
        Minimum number of seconds to run all queries: 0.185 seconds
        Maximum number of seconds to run all queries: 0.221 seconds
        Number of clients running queries: 50
        Average number of queries per client: 20

Benchmark
        Average number of seconds to run all queries: 0.186 seconds
        Minimum number of seconds to run all queries: 0.174 seconds
        Maximum number of seconds to run all queries: 0.199 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10

User time 0.44, System time 0.50
Maximum resident set size 13356, Integral resident set size 0
Non-physical pagefaults 27150, Physical pagefaults 0, Swaps 0
Blocks in 0 out 56, Messages in 0 out 0, Signals 0
Voluntary context switches 47747, Involuntary context switches 134

指定所查的库 和sql语句

[user_121@WEB-TEST-121MAI /etc]$ mysqlslap  -c 50 -i  20 --create-schema='new121dbproduce' --query="select * from br_goods where goods_id<20000" --number-of-queries=10 --debug-info -uroot -p
Enter password:
Benchmark
        Average number of seconds to run all queries: 0.528 seconds
        Minimum number of seconds to run all queries: 0.504 seconds
        Maximum number of seconds to run all queries: 0.560 seconds
        Number of clients running queries: 50
        Average number of queries per client: 0

User time 3.57, System time 2.18
Maximum resident set size 102548, Integral resident set size 0
Non-physical pagefaults 467382, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 109432, Involuntary context switches 218

针对远程主机上的 mysql 进行测试。

[root@Betty Shell]# mysqlslap -a --concurrency=50,100 --number-of-queries=1000 -h 172.16.81.99 -P 3306 -p
Enter password:
Benchmark

        Average number of seconds to run all queries: 2.009 seconds

        Minimum number of seconds to run all queries: 2.009 seconds

        Maximum number of seconds to run all queries: 2.009 seconds

        Number of clients running queries: 50

        Average number of queries per client: 20

Benchmark

        Average number of seconds to run all queries: 4.519 seconds

        Minimum number of seconds to run all queries: 4.519 seconds

        Maximum number of seconds to run all queries: 4.519 seconds

        Number of clients running queries: 100

        Average number of queries per client: 10

使用 --defaults-file 选项,指定从配置文件中读取选项配置。 使用 --number-int-cols 选项,指定表中会包含 4 个 int 型的列。 使用 --number-char-cols 选项,指定表中会包含 35 个 char 型的列。 使用 --engine 选项,指定针对何种存储引擎进行测试。

mysqlslap --defaults-file=/etc/my.cnf --concurrency=50,100,200 --iterations=1 --number-int-cols=4 --number-char-cols=35 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=200 --debug-info -uroot -p
Enter password:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.023 seconds
        Minimum number of seconds to run all queries: 0.023 seconds
        Maximum number of seconds to run all queries: 0.023 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.023 seconds
        Minimum number of seconds to run all queries: 0.023 seconds
        Maximum number of seconds to run all queries: 0.023 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.037 seconds
        Minimum number of seconds to run all queries: 0.037 seconds
        Maximum number of seconds to run all queries: 0.037 seconds
        Number of clients running queries: 200
        Average number of queries per client: 1

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.075 seconds
        Minimum number of seconds to run all queries: 0.075 seconds
        Maximum number of seconds to run all queries: 0.075 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.072 seconds
        Minimum number of seconds to run all queries: 0.072 seconds
        Maximum number of seconds to run all queries: 0.072 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.138 seconds
        Minimum number of seconds to run all queries: 0.138 seconds
        Maximum number of seconds to run all queries: 0.138 seconds
        Number of clients running queries: 200
        Average number of queries per client: 1

User time 0.07, System time 0.14
Maximum resident set size 9284, Integral resident set size 0
Non-physical pagefaults 6383, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 11557, Involuntary context switches 131

其他例子:

楼主没有亲历去执行下面的代码,执行出错需要自行查找原因哦~

提供你自己的创建 SQL 语句和查询 SQL 语句,有 50 个客户端查询,每个查询 200 次(在单行上输入命令):

[root@Betty ~]# mysqlslap --delimiter=";" --create="CREATE TABLE a (b int);INSERT INTO a VALUES (23)" --query="SELECT * FROM a" --concurrency=50 --iterations=200
Benchmark

        Average number of seconds to run all queries: 0.005 seconds

        Minimum number of seconds to run all queries: 0.003 seconds

        Maximum number of seconds to run all queries: 0.007 seconds

        Number of clients running queries: 50

        Average number of queries per client: 1

[root@Betty ~]#

让 mysqlslap 创建查询的 SQL 语句,使用的表有 2 个 INT 行和 3 个 VARCHAR 行。使用 5 个客户端,每一个查询 20 次!不要创建表或插入数据。(换言之,用之前测试的模式和数据):

[root@Betty ~]# mysqlslap --concurrency=5 --iterations=20 --number-int-cols=2 --number-char-cols=3 --auto-generate-sql

Benchmark

        Average number of seconds to run all queries: 0.025 seconds

        Minimum number of seconds to run all queries: 0.012 seconds

        Maximum number of seconds to run all queries: 0.084 seconds

        Number of clients running queries: 5

        Average number of queries per client: 0

[root@Betty ~]#

告诉程序从指定的 create.sql 文件去加载 create,insert 和查询等 SQL 语句。该文件有多个表的 create 和insert 语句, 它们都是通过“;”分隔开的。query.sql 文件则有 多个查询语句, 分隔符也是“;”。执行所有的加载语句,然后通过查询文件执行所有的查询语句,分别在 5 个客户端上每个执行 5 次:

[root@Betty ~]# mysqlslap --concurrency=5 --iterations=5 --query=query.sql --create=create.sql --delimiter=";"

服务器压力测试(ab工具版)

传送门:http://www.jianshu.com/p/43d04d8baaf7