mysqlslap 是 Mysql 自带的压力测试工具,可以模拟出大量客户端同时操作数据库的情况,通过结果信息来了解数据库的性能状况
mysqlslap 的一个主要工作场景就是对数据库服务器做基准测试
在自己的工作电脑上进行测试的。
CPU:i5-8250U 4核心8线程
内存:16GB
硬盘:SSD 256GB
C:\Users\Hzy>mysqlslap --help
mysqlslap Ver 1.0 Distrib 5.7.23, for Win64 (x86_64)
Copyright (c) 2005, 2018, 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:
C:\WINDOWS\my.ini C:\WINDOWS\my.cnf C:\my.ini C:\my.cnf D:\work\mysql-5.7.23-winx64\my.ini D:\work\mysql-5.7.23-winx64\my.cnf D:\Program Files (x86)\MySQL\MySQL Server 5.1\my.ini D:\Program Files (x86)\MySQL\MySQL Server 5.1\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
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
Add GUID based primary keys to auto-generated tables.
--auto-generate-sql-load-type=name
Specify test load type: mixed, update, write, key, or
read; default is mixed.
--auto-generate-sql-secondary-indexes=#
Number of secondary indexes to add to auto-generated
tables.
--auto-generate-sql-unique-query-number=#
Number of unique queries to generate for automatic tests.
--auto-generate-sql-unique-write-number=#
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=# Commit records every X number of statements.
-C, --compress Use compression in server/client protocol.
-c, --concurrency=name
Number of clients to simulate for query to run.
--create=name File or string to use create tables.
--create-schema=name
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 This is a non-debug version. Catch this and exit.
-T, --debug-info This is a non-debug version. Catch this and exit.
--default-auth=name Default authentication client-side plugin to use.
-F, --delimiter=name
Delimiter to use in SQL statements supplied in file or
command line.
--detach=# 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
Number of VARCHAR columns to create in table if
specifying --auto-generate-sql.
-y, --number-int-cols=name
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.
-W, --pipe Use named pipes to connect to server.
--plugin-dir=name Directory for client-side plugins.
-P, --port=# Port number to use for connection.
--post-query=name 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 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 Query to run or file containing query to run.
--secure-auth Refuse client connecting to server if it uses old
(pre-4.1.1) protocol. Deprecated. Always TRUE
--shared-memory-base-name=name
Base name of shared memory.
-s, --silent Run program in silent mode - no output.
-S, --socket=name The socket file to use for connection.
--sql-mode=name Specify sql-mode to run mysqlslap tool.
--ssl-mode=name SSL connection mode.
--ssl Deprecated. Use --ssl-mode instead.
(Defaults to on; use --skip-ssl to disable.)
--ssl-verify-server-cert
Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead.
--ssl-ca=name CA file in PEM format.
--ssl-capath=name CA directory.
--ssl-cert=name X509 cert in PEM format.
--ssl-cipher=name SSL cipher to use.
--ssl-key=name X509 key in PEM format.
--ssl-crl=name Certificate revocation list.
--ssl-crlpath=name Certificate revocation list path.
--tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1
-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.
--concurrency 并发数量,多个可以用逗号隔开
--engines 要测试的引擎,可以有多个,用分隔符隔开,如--engines=myisam,innodb
--iterations 要运行这些测试多少次
--auto-generate-sql 用系统自己生成的SQL脚本来测试
--auto-generate-sql-load-type 要测试的是读还是写还是两者混合的(read,write,update,mixed)
--number-of-queries 总共要运行多少次查询。每个客户运行的查询数量可以用查询总数/并发数来计算
--debug-info 额外输出CPU以及内存的相关信息
--number-int-cols 创建测试表的int型字段数量
--number-char-cols 创建测试表的chat型字段数量
--create-schema 测试的database
--query 自己的SQL 脚本执行测试
--only-print 如果只想打印看看SQL语句是什么,可以用这个选项
使用系统生成的sql脚本和数据库来机型测试。分别使用100、500、1000并发进行测试,总请求数为5000,读写混合压测。
C:\Users\Hzy>mysqlslap --concurrency=100,500,1000 --iterations=1 --auto-generate-sql --auto-generate-sql-load-type=mixed --auto-generate-sql-add-autoincrement --engine=innodb --number-of-queries=5000 -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 1.422 seconds
Minimum number of seconds to run all queries: 1.422 seconds
Maximum number of seconds to run all queries: 1.422 seconds
Number of clients running queries: 100
Average number of queries per client: 50
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 1.734 seconds
Minimum number of seconds to run all queries: 1.734 seconds
Maximum number of seconds to run all queries: 1.734 seconds
Number of clients running queries: 500
Average number of queries per client: 10
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 2.360 seconds
Minimum number of seconds to run all queries: 2.360 seconds
Maximum number of seconds to run all queries: 2.360 seconds
Number of clients running queries: 1000
Average number of queries per client: 5
Average number of … 运行所有语句的平均秒数
Minimum number of … 运行所有语句的最小秒数
Maximum number of … 运行所有语句的最大秒数
Number of clients … 连接数客户端数量
Average number of queries per client 每个客户端运行查询的平均数
总请求数 = Number of clients * Average number of queries per client
使用自己的测试库和测试语句
自动测试可以帮助我们了解硬件层面的状况,对于我们产品特定的情况,还是使用自己的库来测试比较好,可以复制一份产品库过来,然后对此库测试,例如:
C:\Users\Hzy>mysqlslap --concurrency=100 --create-schema=vi_db --query="SELECT * FROM ads_cusprofile_et_info" --number-of-queries=100 -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 6.078 seconds
Minimum number of seconds to run all queries: 6.078 seconds
Maximum number of seconds to run all queries: 6.078 seconds
Number of clients running queries: 100
Average number of queries per client: 1
--create-schema 用来指定测试库名称
--query 是自定义的测试语句
实际使用时,一般是测试多个复杂的语句,可以定义一个脚本文件,例如
echo "SELECT * FROM employees;SELECT * FROM titles;SELECT * FROM dept_emp;SELECT * FROM dept_manager;SELECT * FROM departments;" > ~/select_query.sql
把多个查询语句写入了一个 sql 文件,然后使用此文件执行测试
mysqlslap --concurrency=20 --number-of-queries=1000 --create-schema=employees --query="select_query.sql" --delimiter=”;” -uroot -proot
–query 中指定了sql文件
–delimiter说明sql文件中语句间的分隔符是什么