mysqlslap是一个mysql官方提供的压力测试工具,通过模拟多个并发客户端访问mysql来执行测试,使用起来非常的简单。通过mysqlslap –help可以获得可用的选项。
下面我们就来看看一些比较重要的参数:
–defaults-file,配置文件存放位置
–create-schema,测试的schema,MySQL中schema也就是database
–concurrency,并发数
–engines,测试引擎,可以有多个,用分隔符隔开。
–iterations,迭代的实验次数
–socket,socket,文件位置
–debug-info,打印内存和CPU的信息
–only-print,只打印测试语句而不实际执行
–auto-generate-sql,自动产生测试SQL
–auto-generate-sql-load-type,测试SQL的类型。类型有mixed,update,write,key,read。
–number-of-queries,执行的SQL总数量
–number-int-cols,表内int列的数量
–number-char-cols,表内char列的数量
–query=name,使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试。
测试例子如下:
[root@localhost ~]# /usr/local/mysql/bin/mysqlslap –defaults-file=/etc/my.cnf –concurrency=200 –iterations=1 –number-int-cols=1 –auto-generate-sql –auto-generate-sql-load-type=write –engine=myisam,innodb –number-of-queries=200 -S/tmp/mysql.sock –debug-info -uroot -p123
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.087 seconds
Minimum number of seconds to run all queries: 0.087 seconds
Maximum number of seconds to run all queries: 0.087 seconds
Number of clients running queries: 200
Average number of queries per client: 1
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.551 seconds
Minimum number of seconds to run all queries: 0.551 seconds
Maximum number of seconds to run all queries: 0.551 seconds
Number of clients running queries: 200
Average number of queries per client: 1
User time 0.03, System time 0.05
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 2826, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 3340, Involuntary context switches 96
对于INNODB引擎,200个客户端同时运行这些SQL语句平均要花0.551秒。相应的MYISAM为0.087秒,测试结果也很简明,就不多少说了。
指定数据库的测试:
–create-schema,指定数据库名称
–query, 指定SQL语句,可以定位到某个包含SQL的文件
[root@localhost ~]# /usr/local/mysql/bin/mysqlslap –defaults-file=/etc/my.cnf –concurrency=50 –iterations=1 –create-schema=test –query=/root/test.sql -S/tmp/mysql.sock -uroot -p123
Benchmark
Average number of seconds to run all queries: 0.021 seconds
Minimum number of seconds to run all queries: 0.021 seconds
Maximum number of seconds to run all queries: 0.021 seconds
Number of clients running queries: 50
Average number of queries per client: 1