mysql自带的压力测试工具mysqlslap,详情如下:
重要参数:
--concurrency代表并发数量,多个可以用逗号隔开,当然你也可以用自己的分隔符隔开,这个时候要用到--delimiter开关。
--engines代表要测试的引擎,可以有多个,用分隔符隔开。
--iterations代表要运行这些测试多少次。
--auto-generate-sql 代表用系统自己生成的SQL脚本来测试。
--auto-generate-sql-load-type 代表要测试的是读还是写还是两者混合的(read,write,update,mixed)
--number-of-queries 代表总共要运行多少次查询。每个客户运行的查询数量可以用查询总数/并发数来计算。比如倒数第二个结果2=200/100。
--debug-info 代表要额外输出CPU以及内存的相关信息。
--number-int-cols 代表示例表中的INTEGER类型的属性有几个。
--number-char-cols 代表示例表中的char类型的属性有几个。
--create-schema 代表自己定义的模式(在MySQL中也就是库)。
--query 代表自己的SQL脚本。
--only-print 如果只想打印看看SQL语句是什么,可以用这个选项。
--auto-generate-sql-add-autoincrement 加自增长列
--commit=N 多少条DML后提交一次
--compress 如果服务器和客户端支持压缩,则压缩信息传递
--detach=N 执行N条语句后断开重连
--defaults-file=# 指定配置文件
--delimiter 说明sql文件中语句间的分隔符是什么
--debug-info 打印内存和cpu信息 ##这个参数没有得到验证
1、添加并发
[mysql@mysql-0001 ~]# mysql28/bin/mysqlslap --defaults-file=/dbdata/mysql5728/data3307/my3307.cnf --concurrency=100 --number-of-queries=1000 --auto-generate-sql -uroot -p123456 -S /dbdata/mysql5728/data3307/my3307.sock
mysqlslap: [ERROR] unknown variable 'default-character-set=utf8'
原因:
这是因为mysqlslap这个工具无法识别Mysql中的配置文件“my.cnf”或“my.ini”中的default-character-set=utf8这个指令
解决办法:
第一种解决方法:
在MySQL的配置文件中 /etc/my.cnf 中将default-character-set=utf8 注释掉,但是,这需要重启MySQL服务,如果你的MySQL服务正在忙,那么,这样的代价会比较大。
第二种解决方法:
是直接以“mysqlslap --no-defaults ******" 命令打开;
[mysql@mysql-0001 ~]# mysql28/bin/mysqlslap --no-defaults --concurrency=100 --number-of-queries=1000 --auto-generate-sql -uroot -p123456 -S /dbdata/mysql5728/data3307/my3307.sock
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 0.254 seconds
Minimum number of seconds to run all queries: 0.254 seconds
Maximum number of seconds to run all queries: 0.254 seconds
Number of clients running queries: 100
Average number of queries per client: 10
参数说明:
--concurrency=100 指定同时有100个客户端连接
--number-of-queries=1000 指定总的测试查询次数(并发客户端数 * 每个客户端的查询次数)
2、自动生成复杂表
[mysql@mysql-0001 ~]# mysql28/bin/mysqlslap --no-defaults --concurrency=100,200 --number-of-queries=1000 --auto-generate-sql --number-int-cols=5 --number-char-cols=20 -uroot -p123456 -S /dbdata/mysql5728/data3307/my3307.sock
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 1.473 seconds
Minimum number of seconds to run all queries: 1.473 seconds
Maximum number of seconds to run all queries: 1.473 seconds
Number of clients running queries: 100
Average number of queries per client: 10
Benchmark
Average number of seconds to run all queries: 1.231 seconds
Minimum number of seconds to run all queries: 1.231 seconds
Maximum number of seconds to run all queries: 1.231 seconds
Number of clients running queries: 200
Average number of queries per client: 5
3、使用自己的测试库和测试语句
写了几个生成随机数据的sql函数rand_name(),rand_value(),rand_num(),rand_creator(),并编写了存储过程insert_devattr,用来将一定量数据批量插入deviceattr表中。
mysql> drop table if exists device,attr,deviceattr;
Query OK, 0 rows affected, 3 warnings (0.00 sec)
mysql> drop function if exists rand_name;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> drop function if exists rand_value;
mysql> drop function if exists rand_num;
mysql> drop function if exists rand_creator;
mysql> drop procedure if exists insert_devattr;
mysql> drop procedure if exists insert_dev;
mysql> create table device
-> (
-> id int primary key,
-> name varchar(40),
-> type varchar(40),
-> aquired int,
-> groupname varchar(40),
-> creator varchar(40)
-> );
mysql> create table attr
-> (
-> id int primary key,
-> name varchar(40)
-> );
mysql> create table deviceattr
-> (
-> id int,
-> name varchar(40),
-> value varchar(40)
-> );
mysql> delimiter //
注意:mysql中的delimiter会告诉MySQL解释器,命令的结束符是什么,默认情况下MySQL的命令是以分号(;)结束的.在遇到(;)时,MySQL就可以执行命令了.
例如:
mysql>delimiter $
就是告诉MySQL解释器,当碰到$时,才执行命令.
例1:mysql>select * from student; #回车时就会执行这条语句
例2:mysql>delimiter $
mysql>select * from student; #回车时不会执行
->$ #在此回车才会执行上述语句
mysql>delimiter ;#将命令结束符重新设定为(;)
mysql> create function rand_creator()
-> returns varchar(20)
-> begin
-> declare return_str varchar(20) default 'aronhe';
-> declare n int default 0;
-> set n = floor(rand()*10);
-> case n
-> when 0 then set return_str = 'aronhe';
-> when 1 then set return_str = 'eeelin';
-> when 2 then set return_str = 'shadowyang';
-> when 3 then set return_str = 'luzhao';
-> when 4 then set return_str = 'tommyzhang';
-> when 5 then set return_str = 'pillarzou';
-> when 6 then set return_str = 'allenpan';
-> when 7 then set return_str = 'beyondli';
-> when 8 then set return_str = 'minshi';
-> when 9 then set return_str = 'bingchen';
-> else set return_str = 'joyhu';
-> end case;
-> return return_str;
-> end//
mysql> create function rand_num()
-> returns int
-> begin
-> declare n int default 0;
-> set n = floor(rand()*100);
-> return n;
-> end//
mysql> create function rand_value()
-> returns varchar(10)
-> begin
-> declare return_str varchar(10) default 'false';
-> declare n int default 0;
-> set n = floor(rand()*10);
-> case
-> when n<5 then set return_str = 'false';
-> when n>5 then set return_str = 'true';
-> else set return_str = 'true';
-> end case;
-> return return_str;
-> end//
mysql> create function rand_name()
-> returns varchar(20)
-> begin
-> declare return_str varchar(20) default '';
-> set return_str = concat('attr',floor(rand()*200));
-> return return_str;
-> end//
mysql> create procedure insert_devattr(in start int,in max int)
-> begin
-> declare i int default 0;
-> repeat
-> set i=i+1;
-> insert into deviceattr values(rand_num(),rand_name(),rand_value());
-> until i =max
-> end repeat;
-> end//
mysql> create procedure insert_dev(in start int,in max int)
-> begin
-> declare i int default 10;
-> repeat
-> set i=i+1;
-> insert into device values(i,concat('runner',floor(rand()*100)),'pc',floor(rand()*2),'PCQQ',rand_creator());
-> until i =max
-> end repeat;
-> end//
mysql> call insert_devattr(0,4000)//
Query OK, 1 row affected (25.18 sec)
mysql> call insert_dev(11,100)//
Query OK, 1 row affected (0.54 sec)
[mysql@mysql-0001 ~]# mysql28/bin/mysqlslap --no-defaults --concurrency=100 --number-of-queries=10000 --create-schema="test01" --query="select id from deviceattr where name ='attr10' or name ='attr20' group by id;" -uroot -p123456 -S /dbdata/mysql5728/data3307/my3307.sock
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 17.256 seconds
Minimum number of seconds to run all queries: 17.256 seconds
Maximum number of seconds to run all queries: 17.256 seconds
Number of clients running queries: 100
Average number of queries per client: 100
5、对于复杂的sql语句使用脚本
[mysql@mysql-0001 ~]# cat select_query.sql
select id from deviceattr where name ='attr10' or name ='attr20' group by id; select * from deviceattr;
[mysql@mysql-0001 ~]# mysql28/bin/mysqlslap --no-defaults --concurrency=100 --number-of-queries=10000 --create-schema="test01" --query="/root/select_query.sql" --delimiter=";" -uroot -p123456 -S /dbdata/mysql5728/data3307/my3307.sock
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
Average number of seconds to run all queries: 22.032 seconds
Minimum number of seconds to run all queries: 22.032 seconds
Maximum number of seconds to run all queries: 22.032 seconds
Number of clients running queries: 100
Average number of queries per client: 100