mysqlslap是一种诊断程序用来模拟MySQL服务器的客户端负载并报告每个阶段的时间。它模拟多个客户端同时访问MySQL服务。
mysqlslap语法如下:
Usage: mysqlslap [OPTIONS]
它有许多选项可以使用:
有些选项比如–create或–query能够让你指这一个字符串包含一个SQL语句或一个文件来包含SQL语句。如果你指定一个文件,缺省情况是它必须每行包含一个SQL语句(也就是说隐式语句分隔符是换行符)。使用–delimiter选项来指定不同的分隔符,它能让你指定一个语句跨越多行或者将多个语句放入一行。在文件中不能包含注释,mysqlslap不能解析它们。
mysqlslap运行分为三个阶段
1.创建方案,表和可选的任何存储过程或用于测试的数据。这个阶段使用单个客房端连接。
2.运行负载测试,这个阶段可以使用多个客户端连接。
3.清除(断开连接,如果指定删除表)。这个阶段使用单个客户端连接。
例如:
假设你创建和查询SQL语句,而且想使用50个客户端来执行查询并且每个语句执行200次(在单行中输入命令):
mysqlslap --delimiter=";" --create="create table a(b int);insert into a values(11)" --query="select * from a" --concurrency=50 --iterations=200
执行结果如下:
[mysql@localhost ~]$ mysqlslap -h192.168.1.250 -P33306 -uroot -p123456 mysql --delimiter=";" --create="create table a(b int);insert into a values(11)" --query="select * from a" --concurrency=50 --iterations=200 mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.268 seconds Minimum number of seconds to run all queries: 0.257 seconds Maximum number of seconds to run all queries: 0.370 seconds Number of clients running queries: 50 Average number of queries per client: 1
让mysqlslap来使用一个有两个INT列和三个varchar列的表来构建查询SQL语句。使用五个客户端每个客户端查询20次。不创建表或不插入数据(使用之前的测试方案和数据):
mysqlslap --concurrency=5 --iterations=20 --number-int-cols=2 --number-char-cols=3 --auto-generate-sql
执行结果如下:
[mysql@localhost ~]$ mysqlslap -h192.168.1.250 -P33306 -uroot -p123456 mysql --concurrency=5 --iterations=20 --number-int-cols=2 --number-char-cols=3 --auto-generate-sql mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.247 seconds Minimum number of seconds to run all queries: 0.182 seconds Maximum number of seconds to run all queries: 0.361 seconds Number of clients running queries: 5 Average number of queries per client: 0
为了告诉程序从指定的文件中加载创建,插入和查询的SQL语句,这里给–create选项指定create.sql文件,它包含了由分号隔开的多个创建表的语句和多个插入语句。–query选项指定的query.sql文件包含由分号隔开的多个查询语句。运行所有加载的语句,然后使用五个客户端运行查询文件中的所有查询(每个运行五次):
mysqlslap --concurrency=5 --iterations=5 --query=query.sql --create=create.sql --delimiter=";"
执行结果如下:
[mysql@localhost ~]$ mysqlslap -h192.168.1.250 -P33306 -uroot -p123456 mysql --concurrency=5 --iterations=5 --query=query.sql --create=create.sql --delimiter=";" mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.035 seconds Minimum number of seconds to run all queries: 0.034 seconds Maximum number of seconds to run all queries: 0.038 seconds Number of clients running queries: 5 Average number of queries per client: 5
mysqlslap支持以下选项,可以在命令行指定或在选项文件中通过[mysqlslap]和[client]组来指定
[mysql@localhost ~]$ mysqlslap --help mysqlslap Ver 1.0 Distrib 5.7.26, for Linux (x86_64) Copyright (c) 2005, 2019, 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 /mysqlsoft/mysql/etc/my.cnf /mysqlsoft/mysql/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 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. --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 -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.