2011年6月2日星期四

MySQL 压力测试工具 mysqlslap

FROM: MySQL 压力测试工具 mysqlslap

从 5.1.4 开始,MySQL 自带有一个压力测试工具 mysqlslap, 它通过模拟多个并发客户端访问 MySQL 来执行测试,使用起来非常简单。通过mysqlslap --help可以获得可用的选项,这里列一些主要的参数,更详细的说明参考官方手册

  • --auto-generate-sql, -a 自动生成测试表和数据。

  • --auto-generate-sql-load-type=type 测试语句的类型。取值包括:read,key,write,update和mixed(默认)。

  • --number-char-cols=N, -x N 自动生成的测试表中包含多少个字符类型的列,默认 1。

  • --number-int-cols=N, -y N 自动生成的测试表中包含多少个数字类型的列,默认 1。

  • --number-of-queries=N 总的测试查询次数(并发客户数×每客户查询次数)。

  • --query=name,-q 使用自定义脚本执行测试,eg: 可以调用自定义的一个存储过程或者 SQL 语句来执行测试。

  • --create-schema 指定测试的数据库。

  • --commint=N 多少条 DML 后提交一次。

  • --compress, -C 如果服务器和客户端支持都压缩,则压缩信息传递。

  • --concurrency=N, -c N 并发量,也就是模拟多少个客户端同时执行 SELECT。可指定多个值,以逗号或者--delimiter参数指定的值做为分隔符。

  • --engine=engine_name, -e engine_name 创建测试表所使用的存储引擎,可指定多个。

  • --iterations=N, -i N 测试执行的迭代次数。

  • --detach=N执行 N 条语句后断开重连。

  • --debug-info, -T打印内存和CPU的信息。

  • --only-print 只打印测试语句而不实际执行。

  • --defaults-file=mysql_configuration_file_directory 配置文件存放位置。

  • --socket=socket_directory, -S socket_directory socket文件位置。



测试的过程需要生成测试表和测试数据,mysqlslap 会自动生成一个名为 mysqlslap 的 schema,如果已经存在则先删除。可用--only-print来打印实际的测试过程。
[bash]
[root@localhost bin]# /usr/local/mysql/bin/mysqlslap -a --only-print
[/bash]

DROP SCHEMA IF EXISTS `mysqlslap`;
CREATE SCHEMA `mysqlslap`;
use mysqlslap;
CREATE TABLE `t1` (intcol1 INT(32) ,charcol1 VARCHAR(128));
INSERT INTO t1 VALUES (1804289383,'mxvtvmC9127qJNm06sGB8R92q2j7vTiiITRDGXM9ZLzkdekbWtmXKwZ2qG1llkRw5m9DHOFilEREk3q7oce8O3BEJC0woJsm6uzFAEynLH2xCsw1KQ1lT4zg9rdxBL');
......
DROP SCHEMA IF EXISTS `mysqlslap`;

可以看出,最后会删除一开始创建的 schema,so, 整个测试完成后不会在数据库中留下痕迹。
假如执行一次测试,分别模拟 50 和 100 个并发,都执行 1000 次查询,那么:
[bash]
[root@localhost bin]# /usr/local/mysql/bin/mysqlslap -a --concurrency=50,100 --number-of-queries=1000 --debug-info
[/bash]

Benchmark
Average number of seconds to run all queries: 0.676 seconds
Minimum number of seconds to run all queries: 0.676 seconds
Maximum number of seconds to run all queries: 0.676 seconds
Number of clients running queries: 50
Average number of queries per client: 20

Benchmark
Average number of seconds to run all queries: 0.922 seconds
Minimum number of seconds to run all queries: 0.922 seconds
Maximum number of seconds to run all queries: 0.922 seconds
Number of clients running queries: 100
Average number of queries per client: 10


User time 0.17, System time 0.40
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 1653, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 14740, Involuntary context switches 6135

Voluntary context switches 7319, Involuntary context switches 681

上结果可以看出,50 和 100 个并发分别得到一次测试结果(Benchmark),并发数越多,执行完所有查询的时间越长。为了准确起见,可以多迭代测试几次:
[bash]
[root@localhost bin]# /usr/local/mysql/bin/mysqlslap -a --concurrency=50,100 --number-of-queries=1000 --iterations=5 --debug-info
[/bash]

Benchmark
Average number of seconds to run all queries: 0.833 seconds
Minimum number of seconds to run all queries: 0.803 seconds
Maximum number of seconds to run all queries: 0.865 seconds
Number of clients running queries: 50
Average number of queries per client: 20

Benchmark
Average number of seconds to run all queries: 0.980 seconds
Minimum number of seconds to run all queries: 0.948 seconds
Maximum number of seconds to run all queries: 1.007 seconds
Number of clients running queries: 100
Average number of queries per client: 10


User time 1.03, System time 2.35
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 7148, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 115255, Involuntary context switches 4125


测试同时不同的存储引擎的性能进行对比:
[bash]
[root@localhost bin]# /usr/local/mysql/bin/mysqlslap -a --concurrency=50,100 --number-of-queries=1000 --iterations=5 --engine=myisam,innodb --debug-info
[/bash]

Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.781 seconds
Minimum number of seconds to run all queries: 0.766 seconds
Maximum number of seconds to run all queries: 0.800 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.908 seconds
Minimum number of seconds to run all queries: 0.880 seconds
Maximum number of seconds to run all queries: 0.929 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.897 seconds
Minimum number of seconds to run all queries: 0.858 seconds
Maximum number of seconds to run all queries: 0.929 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: 1.189 seconds
Minimum number of seconds to run all queries: 1.086 seconds
Maximum number of seconds to run all queries: 1.306 seconds
Number of clients running queries: 100
Average number of queries per client: 10


User time 2.02, System time 4.24
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 14556, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 172917, Involuntary context switches 52011

100 个并发时,InnoDB 引擎,运行 1000 句 SQL 平均要需要 1.189 秒;而对于 MyISAM, 为 0.908 秒。

测试自定义 SQL:
[bash]
[root@localhost bin]# /usr/local/mysql/bin/mysqlslap --defaults-file=/usr/local/mysql/etc/my.cnf --create-schema=hstestdb --concurrency=50,100 --number-of-queries=1000 --iterations=5 --query="SELECT * FROM hstestdb.hstesttbl WHERE k='k2'" --debug-info -u root -p -S /usr/local/mysql/tmp/mysql.sock
Enter password:
[/bash]

Benchmark
Average number of seconds to run all queries: 0.271 seconds
Minimum number of seconds to run all queries: 0.256 seconds
Maximum number of seconds to run all queries: 0.288 seconds
Number of clients running queries: 50
Average number of queries per client: 20

Benchmark
Average number of seconds to run all queries: 0.324 seconds
Minimum number of seconds to run all queries: 0.292 seconds
Maximum number of seconds to run all queries: 0.333 seconds
Number of clients running queries: 100
Average number of queries per client: 10


User time 0.22, System time 0.63
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 4355, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 16691, Involuntary context switches 2837

没有评论:

发表评论