mysqlslap是mysql自带的基准测试工具,优点:查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较.msqlslap为mysql性能优化前后提供了直观的验证依据,建议系统运维和DBA人员应该掌握一些常见的压力测试工具,才能准确的掌握线上数据库支撑的用户流量上限及其抗压性等问题。
常用的选项
--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语句是什么,可以用这个选项
各种测试参数实例(-p后面跟的是mysql的root密码):
单线程测试。测试做了什么。
# mysqlslap -a -uroot -p123456
多线程测试。使用–concurrency来模拟并发连接。
# mysqlslap -a -c 100 -uroot -p123456
迭代测试。用于需要多次执行测试得到平均值。
# mysqlslap -a -i 10 -uroot -p123456
# mysqlslap ---auto-generate-sql-add-autoincrement -a -uroot -p123456
# mysqlslap -a --auto-generate-sql-load-type=read -uroot -p123456
# mysqlslap -a --auto-generate-secondary-indexes=3 -uroot -p123456
# mysqlslap -a --auto-generate-sql-write-number=1000 -uroot -p123456
# mysqlslap --create-schema world -q "select count(*) from City" -uroot -p123456
# mysqlslap -a -e innodb -uroot -p123456
# mysqlslap -a --number-of-queries=10 -uroot -p123456
测试同时不同的存储引擎的性能进行对比:
# mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-info -uroot -p123456
执行一次测试,分别50和100个并发,执行1000次总查询:
# mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --debug-info -uroot -p123456
50和100个并发分别得到一次测试结果(Benchmark),并发数越多,执行完所有查询的时间越长。为了准确起见,可以多迭代测试几次:
# mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --debug-info -uroot -p123456
实例1
说明:测试100个并发线程,测试次数1次,自动生成SQL测试脚本,读、写、更新混合测试,自增长字段,测试引擎为innodb,共运行5000次查询
#mysqlslap -h127.0.0.1 -uroot -p123456789 --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate-sql-load-type=mixed --auto-generate-sql-add-autoincrement --engine=innodb --number-of-queries=5000
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.351 seconds 100个客户端(并发)同时运行这些SQL语句平均要花0.351秒
Minimum number of seconds to run all queries: 0.351 seconds
Maximum number of seconds to run all queries: 0.351 seconds
Number of clients running queries: 100 总共100个客户端(并发)运行这些sql查询
Average number of queries per client:50 每个客户端(并发)平均运行50次查询(对应--concurrency=100,--number-of-queries=5000;5000/100=50)
实例2
#mysqlslap -h127.0.0.1 -uroot -p123456789 --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 --debug-info
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.328 seconds
Minimum number of seconds to run all queries: 0.328 seconds
Maximum number of seconds to run all queries: 0.328 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: 0.358 seconds
Minimum number of seconds to run all queries: 0.358 seconds
Maximum number of seconds to run all queries: 0.358 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: 0.482 seconds
Minimum number of seconds to run all queries: 0.482 seconds
Maximum number of seconds to run all queries: 0.482 seconds
Number of clients running queries: 1000
Average number of queries per client: 5
User time 0.21, System time 0.78
Maximum resident set size 21520, Integral resident set size 0
Non-physical pagefaults 12332, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 36771, Involuntary context switches 1396
实例3(自定义sql语句)
#mysqlslap -h127.0.0.1 -uroot -p123456789 --concurrency=100 --iterations=1 --create-schema=rudao --query='select * from serverlist;' --engine=innodb --number-of-queries=5000 --debug-info
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.144 seconds
Minimum number of seconds to run all queries: 0.144 seconds
Maximum number of seconds to run all queries: 0.144 seconds
Number of clients running queries: 100
Average number of queries per client: 50
User time 0.05, System time 0.09
Maximum resident set size 6132, Integral resident set size 0
Non-physical pagefaults 2078, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 6051, Involuntary context switches 90
实例4(指定sql脚本)
#mysqlslap -h127.0.0.1 -uroot -p123456789 --concurrency=100 --iterations=1 --create-schema=rudao --query=/tmp/query.sql --engine=innodb --number-of-queries=5000 --debug-info
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: 0.157 seconds
Minimum number of seconds to run all queries: 0.157 seconds
Maximum number of seconds to run all queries: 0.157 seconds
Number of clients running queries: 100
Average number of queries per client: 50
User time 0.07, System time 0.08
Maximum resident set size 6152, Integral resident set size 0
Non-physical pagefaults 2107, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 6076, Involuntary context switches 89