MySQL从5.1.4版开始带有一个压力测试工具mysqlslap,通过模拟多个并发客户端访问mysql来执行测试,使用起来非常简单,通过mysqlslap –help可以获得可用的选项。这里列一些主要的参数,更详细的说明参考官方手册。
-a, --auto-generate-sql 自动生成测试表和数据 --auto-generate-sql-load-type=name 测试语句的类型。取值包括:read,key,write,update和mixed(默认) -x, --number-char-cols=N 自动生成的测试表中包含多少个字符类型的列,默认1 -y, --number-int-cols=N 自动生成的测试表中包含多少个数字类型的列,默认1 --number-of-queries=N 总的测试查询次数(并发客户数×每客户查询次数) -q, --query=name 使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试 --create-schema=name 测试的schema,MySQL中schema也就是database,默认生成一个mysqlslap --commit=N 多少条DML后提交一次 -C, --compress 如果服务器和客户端支持都压缩,则压缩信息传递 -c, --concurrency=N 并发量,也就是模拟多少个客户端同时执行select。可指定多个值,以逗号或者–delimiter参数指定的值做为分隔符 -e, --engine=name 创建测试表所使用的存储引擎,可指定多个 -i, --iterations=N 测试执行的迭代次数 --detach=N 执行N条语句后断开重连 -T, --debug-info 打印内存和CPU的信息 --only-print 只打印测试语句而不实际执行
打印测试语句
[root@VMUest ~]# mysqlslap -a --only-print 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'); ... SELECT intcol1,charcol1 FROM t1; INSERT INTO t1 VALUES (364531492,'qMa5SuKo4M5OM7ldvisSc6WK9rsG9E8sSixocHdgfa5uiiNTGFxkDJ4EAwWC2e4NL1BpAgWiFRcp1zIH6F1BayPdmwphatwnmzdwgzWnQ6SRxmcvtd6JRYwEKdvuWr'); DROP SCHEMA IF EXISTS `mysqlslap`;
分别50和100个并发,执行 1000次总查询,迭代5次,不同的存储引擎
[root@VMUest ~]# mysqlslap -a -h192.168.85.129 -umydba -pmysql5635 -P3306 --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-info 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.432 seconds Minimum number of seconds to run all queries: 0.382 seconds Maximum number of seconds to run all queries: 0.532 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.512 seconds Minimum number of seconds to run all queries: 0.414 seconds Maximum number of seconds to run all queries: 0.710 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.898 seconds Minimum number of seconds to run all queries: 0.606 seconds Maximum number of seconds to run all queries: 1.520 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.744 seconds Minimum number of seconds to run all queries: 0.606 seconds Maximum number of seconds to run all queries: 0.954 seconds Number of clients running queries: 100 Average number of queries per client: 10 User time 1.47, System time 2.65 Maximum resident set size 11060, Integral resident set size 0 Non-physical pagefaults 26225, Physical pagefaults 0, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 40290, Involuntary context switches 5719
在sakila库上测试并发
[root@VMUest ~]# mysqlslap -h192.168.85.129 -umydba -pmysql5635 -P3306 --create-schema=sakila --query="select * from actor where actor_id=19" --concurrency=50 --number-of-queries 1000 --iterations=5