MySQL/MariaDB数据库的性能测试
MySQL/MariaDB数据库的性能测试
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.数据库服务衡量指标
qps:
query per second(每秒支持多少查询)
tps:
transaction per second(每秒支持多少个事务性能)
二.压力测试工具
mysqlslap: Mariadb自带的测试脚本
Sysbench:功能强大 https://github.com/akopytov/sysbench
tpcc-mysql
MySQL Benchmark Suite
MySQL super-smack
MyBench
三.Mysqlslap使用案例
1>.Mysqlslap概述
Mysqlslap:
来自于mariadb包,测试的过程默认生成一个mysqlslap的schema,生成测试表t1,查询和插入测试数据,mysqlslap库自动生成,如果已经存在则先删除。用--only-print来打印实际的测试过程,整个测试完成后不会在数据库中留下痕迹
使用格式:
mysqlslap [options]
2>.Mysqlslap常用参数 [options] 说明:
--auto-generate-sql, -a
自动生成测试表和数据,表示用mysqlslap工具自己生成的SQL脚本来测试并发压力
--auto-generate-sql-load-type=type
测试语句的类型。代表要测试的环境是读操作还是写操作还是两者混合的。取值包括:read,key,write,update和mixed(默认) --auto-generate-sql-add-auto-increment
代表对生成的表自动添加auto_increment列,从5.1.18版本开始支持
--number-char-cols=N, -x N
自动生成的测试表中包含多少个字符类型的列,默认1
--number-int-cols=N, -y N
自动生成的测试表中包含多少个数字类型的列,默认1
--number-of-queries=N
总的测试查询次数(并发客户数×每客户查询次数)
--query=name,-q
使用自定义脚本执行测试,例如可以调用自定义的存储过程或者sql语句来执行测试
--create-schema
代表自定义的测试库名称,测试的schema
--commint=N
多少条DML后提交一次 --compress, -C
如服务器和客户端都支持压缩,则压缩信息
--concurrency=N, -c N
表示并发量,即模拟多少个客户端同时执行select。可指定多个值,以逗号或者--delimiter参数指定值做为分隔符 如:--concurrency=100,200,500
--engine=engine_name, -e engine_name
代表要测试的引擎,可以有多个,用分隔符隔开。例如:--engines=myisam,innodb
--iterations=N, -i N
测试执行的迭代次数,代表要在不同并发环境下,各自运行测试多少次
--only-print
只打印测试语句而不实际执行。
--detach=N
执行N条语句后断开重连
--debug-info, -T
打印内存和CPU的相关信息
[root@node103.yinzhengjie.org.cn ~]# mysqlslap --help mysqlslap Ver 1.0 Distrib 5.5.64-MariaDB, for Linux (x86_64) Copyright (c) 2005, 2018, Oracle, MariaDB Corporation Ab and others. Run a query multiple times against the server. Usage: mysqlslap [OPTIONS] Default options are read from the following files in the given order: /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf The following groups are read: mysqlslap client client-server client-mariadb 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. The following specify which files/extra groups are read (specified before remaining options): --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=# Additionally read default groups with # appended as a suffix. -?, --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 Check memory and open file usage at exit. -T, --debug-info Print some debug info at 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. -e, --engine=name Comma separated list of storage engines to use for creating the table. The test is run for each engine. You can also specify an option for an engine after a `:', like memory:max_row=2300 -h, --host=name Connect to host. --init-command=name SQL Command to execute when connecting to MySQL server. Will automatically be re-executed when reconnecting. -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. -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-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. Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) ------------------------------------- ------------------------------------ auto-generate-sql FALSE auto-generate-sql-add-autoincrement FALSE auto-generate-sql-execute-number 0 auto-generate-sql-guid-primary FALSE auto-generate-sql-load-type mixed auto-generate-sql-secondary-indexes 0 auto-generate-sql-unique-query-number 10 auto-generate-sql-unique-write-number 10 auto-generate-sql-write-number 100 commit 0 compress FALSE concurrency (No default value) create (No default value) create-schema mysqlslap debug-check FALSE debug-info FALSE default-auth (No default value) delimiter detach 0 engine (No default value) host (No default value) init-command (No default value) iterations 1 no-drop FALSE number-char-cols (No default value) number-int-cols (No default value) number-of-queries 0 only-print FALSE plugin-dir (No default value) port 3306 post-query (No default value) post-system (No default value) pre-query (No default value) pre-system (No default value) query (No default value) silent FALSE socket (No default value) ssl FALSE ssl-ca (No default value) ssl-capath (No default value) ssl-cert (No default value) ssl-cipher (No default value) ssl-key (No default value) ssl-verify-server-cert FALSE user (No default value) verbose (No default value) [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
3>.单线程测试
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie Benchmark Average number of seconds to run all queries: 0.003 seconds Minimum number of seconds to run all queries: 0.003 seconds Maximum number of seconds to run all queries: 0.003 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
4>.多线程测试(使用–concurrency来模拟并发连接)
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie -c 100 Benchmark Average number of seconds to run all queries: 0.653 seconds Minimum number of seconds to run all queries: 0.653 seconds Maximum number of seconds to run all queries: 0.653 seconds Number of clients running queries: 100 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]#
5>.迭代测试(用于需要多次执行测试得到平均值)
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie -i 10 Benchmark Average number of seconds to run all queries: 0.002 seconds Minimum number of seconds to run all queries: 0.002 seconds Maximum number of seconds to run all queries: 0.003 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie --auto-generate-sql-add-autoincrement Benchmark Average number of seconds to run all queries: 0.002 seconds Minimum number of seconds to run all queries: 0.002 seconds Maximum number of seconds to run all queries: 0.002 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie -a --auto-generate-sql-load-type=read Benchmark Average number of seconds to run all queries: 0.001 seconds Minimum number of seconds to run all queries: 0.001 seconds Maximum number of seconds to run all queries: 0.001 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -a -uroot -pyinzhengjie -a auto-generate-sql-secondary-indexes=3 Benchmark Average number of seconds to run all queries: 0.003 seconds Minimum number of seconds to run all queries: 0.003 seconds Maximum number of seconds to run all queries: 0.003 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a --auto-generate-sql-write-number=1000 Benchmark Average number of seconds to run all queries: 0.007 seconds Minimum number of seconds to run all queries: 0.007 seconds Maximum number of seconds to run all queries: 0.007 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie --create-schema mysql -q "select count(*) from user" Benchmark Average number of seconds to run all queries: 0.000 seconds Minimum number of seconds to run all queries: 0.000 seconds Maximum number of seconds to run all queries: 0.000 seconds Number of clients running queries: 1 Average number of queries per client: 1 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a -e innodb Benchmark Running for engine innodb Average number of seconds to run all queries: 0.003 seconds Minimum number of seconds to run all queries: 0.003 seconds Maximum number of seconds to run all queries: 0.003 seconds Number of clients running queries: 1 Average number of queries per client: 0 [root@node103.yinzhengjie.org.cn ~]#
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a --number-of-queries=10 Benchmark Average number of seconds to run all queries: 0.003 seconds Minimum number of seconds to run all queries: 0.003 seconds Maximum number of seconds to run all queries: 0.003 seconds Number of clients running queries: 1 Average number of queries per client: 10 [root@node103.yinzhengjie.org.cn ~]#
6>.测试同时不同的存储引擎的性能进行对比
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-infoBenchmark Running for engine myisam Average number of seconds to run all queries: 0.081 seconds Minimum number of seconds to run all queries: 0.080 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.183 seconds Minimum number of seconds to run all queries: 0.081 seconds Maximum number of seconds to run all queries: 0.583 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.110 seconds Minimum number of seconds to run all queries: 0.097 seconds Maximum number of seconds to run all queries: 0.130 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.118 seconds Minimum number of seconds to run all queries: 0.111 seconds Maximum number of seconds to run all queries: 0.126 seconds Number of clients running queries: 100 Average number of queries per client: 10 User time 0.77, System time 0.46 Maximum resident set size 10628, Integral resident set size 0 Non-physical pagefaults 13187, Physical pagefaults 0, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 46331, Involuntary context switches 29 [root@node103.yinzhengjie.org.cn ~]#
7>.执行一次测试,分别50和100个并发,执行1000次总查询
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a --concurrency=50,100 --number-of-queries 1000 --debug-info Benchmark Average number of seconds to run all queries: 0.197 seconds Minimum number of seconds to run all queries: 0.197 seconds Maximum number of seconds to run all queries: 0.197 seconds Number of clients running queries: 50 Average number of queries per client: 20 Benchmark Average number of seconds to run all queries: 0.121 seconds Minimum number of seconds to run all queries: 0.121 seconds Maximum number of seconds to run all queries: 0.121 seconds Number of clients running queries: 100 Average number of queries per client: 10 User time 0.10, System time 0.05 Maximum resident set size 9744, Integral resident set size 0 Non-physical pagefaults 3967, Physical pagefaults 0, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 5373, Involuntary context switches 15 [root@node103.yinzhengjie.org.cn ~]# [root@node103.yinzhengjie.org.cn ~]#
8>.50和100个并发分别得到一次测试结果(Benchmark),并发数越多,执行完所有查询的时间越长。为了准确起见,可以多迭代测试几次
[root@node103.yinzhengjie.org.cn ~]# mysqlslap -uroot -pyinzhengjie -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --debug-info Benchmark Average number of seconds to run all queries: 0.201 seconds Minimum number of seconds to run all queries: 0.098 seconds Maximum number of seconds to run all queries: 0.595 seconds Number of clients running queries: 50 Average number of queries per client: 20 Benchmark Average number of seconds to run all queries: 0.123 seconds Minimum number of seconds to run all queries: 0.113 seconds Maximum number of seconds to run all queries: 0.138 seconds Number of clients running queries: 100 Average number of queries per client: 10 User time 0.38, System time 0.27 Maximum resident set size 10600, Integral resident set size 0 Non-physical pagefaults 8716, Physical pagefaults 0, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 26126, Involuntary context switches 19 [root@node103.yinzhengjie.org.cn ~]#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/11841682.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。