mysqlslap是mysql自带的一款性能压测工具,经过模拟多个并发客户端访问MySQL来进行压力测试,同时提供了详细的数据性能报告。此工具能够自动生成测试表和数据,而且能够模拟读写、混合读写、查询等不一样的使用场景,也可以很好的对比多个存储引擎在相同环境的并发压力下的性能差别。本文是使用的《分布式服务架构——原理、设计与实践》的案例。mysql
λ mysqlslap -a -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.141 seconds Minimum number of seconds to run all queries: 0.141 seconds Maximum number of seconds to run all queries: 0.141 seconds Number of clients running queries: 1 Average number of queries per client: 0
测试结果显示单线程链接一次服务器须要141ms。sql
λ mysqlslap -a -c 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: 5.500 seconds Minimum number of seconds to run all queries: 5.500 seconds Maximum number of seconds to run all queries: 5.500 seconds Number of clients running queries: 100 Average number of queries per client: 0
能够看到使用100个线程同时链接一次服务器须要5500ms,同单线程比,响应时间大幅度提高,这个不知道是什么缘由,须要后续查询下。数据库
λ mysqlslap -a -i 10 -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.370 seconds Minimum number of seconds to run all queries: 0.140 seconds Maximum number of seconds to run all queries: 1.203 seconds Number of clients running queries: 1 Average number of queries per client: 0
最小响应时间和单线程测试同样,可是平均响应时间仍是有差,个人数据库可能被下毒了。服务器
λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=read -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 0.062 seconds Minimum number of seconds to run all queries: 0.062 seconds Maximum number of seconds to run all queries: 0.062 seconds Number of clients running queries: 10 Average number of queries per client: 100
读操做在并发数是10,进行1000次的读操做,每次的读须要62ms。多线程
λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=write -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 11.875 seconds Minimum number of seconds to run all queries: 11.875 seconds Maximum number of seconds to run all queries: 11.875 seconds Number of clients running queries: 10 Average number of queries per client: 100
毫无疑问,我这台机子的瓶颈在写,平均写时间要花掉11.875秒,苍天啊,下面用混合的方式印证下。架构
λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=mixed -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Average number of seconds to run all queries: 5.844 seconds Minimum number of seconds to run all queries: 5.844 seconds Maximum number of seconds to run all queries: 5.844 seconds Number of clients running queries: 10 Average number of queries per client: 100
基本上,在混合模式下的性能和在多线程状况下的性能是相同,推测在单线程的时候,是随机读写,在多线程下是混合模式操做的,对单线程进行反复的执行,印证了,单线程是随机读写测试的。并发
λ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --engine=myisam,innodb --iterations=5 -uroot -proot mysqlslap: [Warning] Using a password on the command line interface can be insecure. Benchmark Running for engine myisam Average number of seconds to run all queries: 0.115 seconds Minimum number of seconds to run all queries: 0.109 seconds Maximum number of seconds to run all queries: 0.125 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.121 seconds Minimum number of seconds to run all queries: 0.094 seconds Maximum number of seconds to run all queries: 0.156 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: 4.434 seconds Minimum number of seconds to run all queries: 3.797 seconds Maximum number of seconds to run all queries: 5.421 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: 3.759 seconds Minimum number of seconds to run all queries: 2.969 seconds Maximum number of seconds to run all queries: 4.250 seconds Number of clients running queries: 100 Average number of queries per client: 10
上面现实了myisam和innodb两种引擎的性能,具体差别须要查阅mysql的原理的书籍查找缘由。分布式
数据库做为应用系统的性能的重要一环,掌握其数据库的性能测试方法有助于判断系统的性能瓶颈所在,已经系统架构中,数据库的部署。工具
mysqlslap测试的参数的解释,咱们能够经过使用mysqlslap --help来显示使用方法:性能
Default options are read from the following files in the given order:
/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf
--c表明并发数量,多个能够用逗号隔开,concurrency=10,50,100, 并发链接线程数分别是十、50、100个并发。
--engines表明要测试的引擎,能够有多个,用分隔符隔开。
--iterations表明要运行这些测试多少次。
-- a 指的是auto-generate-sql 表明用系统本身生成的SQL脚原本测试。
--auto-generate-sql-load-type 表明要测试的是读仍是写仍是二者混合的(read,write,update,mixed)
--number-of-queries 表明总共要运行多少次查询。每一个客户运行的查询数量能够用查询总数/并发数来计算。
--debug-info 表明要额外输出CPU以及内存的相关信息。
--number-int-cols :建立测试表的 int 型字段数量
--auto-generate-sql-add-autoincrement : 表明对生成的表自动添加auto_increment列,从5.1.18版本开始
--number-char-cols 建立测试表的 char 型字段数量。
--create-schema 测试的schema,MySQL中schema也就是database。
--query 使用自定义脚本执行测试,例如能够调用自定义的一个存储过程或者sql语句来执行测试。
--only-print 若是只想打印看看SQL语句是什么,能够用这个选项。
本文最后参考了:https://blog.csdn.net/jjwen/article/details/51569234