sysbench 进行系统测试


sysbench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。关于这个项目的详细介绍请看:http://sysbench.sourceforge.net。


它主要包括以下几种方式的测试:


1、cpu性能


2、磁盘io性能


3、调度程序性能


4、内存分配及传输速度


5、POSIX线程性能


6、数据库性能(OLTP基准测试)


目前sysbench主要支持 MySQL,pgsql,oracle 这3种数据库。


一、安装


首先,在 http://sourceforge.net/projects/sysbench 下载源码包。


接下来,按照以下步骤安装:


tar zxf sysbench-0.4.8.tar.gz


cd sysbench-0.4.8


./configure && make && make install


strip /usr/local/bin/sysbench


以上方法适用于 MySQL 安装在标准默认目录下的情况,如果 MySQL 并不是安装在标准目录下的话,那么就需要自己指定 MySQL 的路径了。比如我的 MySQL 喜欢自己安装在 /usr/local/mysql 下,则按照以下方法编译:


/configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib && make && make install


当然了,用上面的参数编译的话,就要确保你的 MySQL lib目录下有对应的 so 文件,如果没有,可以自己下载 devel 或者 share 包来安装。


另外,如果想要让 sysbench 支持 pgsql/oracle 的话,就需要在编译的时候加上参数


--with-pgsql


或者


--with-oracle


这2个参数默认是关闭的,只有 MySQL 是默认支持的。


二、开始测试


编译成功之后,就要开始测试各种性能了,测试的方法官网网站上也提到一些,但涉及到 OLTP 测试的部分却不够准确。在这里我大致提一下:


1、cpu性能测试


sysbench --test=cpu --cpu-max-prime=20000 run


cpu测试主要是进行素数的加法运算,在上面的例子中,指定了最大的素数为 20000,自己可以根据机器cpu的性能来适当调整数值。


2、线程测试


sysbench --test=threads --num-threads=64 --thread-yields=100 --thread-locks=2 run


3、磁盘IO性能测试


sysbench --test=fileio --num-threads=16 --file-total-size=3G --file-test-mode=rndrw prepare


sysbench --test=fileio --num-threads=16 --file-total-size=3G --file-test-mode=rndrw run


sysbench --test=fileio --num-threads=16 --file-total-size=3G --file-test-mode=rndrw cleanup


上述参数指定了最大创建16个线程,创建的文件总大小为3G,文件读写模式为随机读。


4、内存测试


sysbench --test=memory --memory-block-size=8k --memory-total-size=4G run


上述参数指定了本次测试整个过程是在内存中传输 4G 的数据量,每个 block 大小为 8K。


5、OLTP测试


sysbench --test=oltp --mysql-table-engine=myisam --oltp-table-size=1000000 /


--mysql-socket=/tmp/mysql.sock --mysql-user=test --mysql-host=localhost /


--mysql-password=test prepare


上述参数指定了本次测试的表存储引擎类型为 myisam,这里需要注意的是,官方网站上的参数有一处有误,即 –mysql-table-engine,官方网站上写的是 –mysql-table-type,这个应该是没有及时更新导致的。另外,指定了表最大记录数为 1000000,其他参数就很好理解了,主要是指定登录方式。测试 OLTP 时,可以自己先创建数据库 sbtest,或者自己用参数 –mysql-db 来指定其他数据库。–mysql-table-engine 还可以指定为 innodb 等 MySQL 支持的表存储引擎类型。


附:用sysbench进行mysql性能测试


1 、准备数据


# sysbench –debug=off –test=oltp –mysql-host=10.15.2.137 –mysql-user=test –mysql-password=test –oltp-table-size=1000000 –mysql-db=test –oltp-table-name=stest –num-threads=20 –max-requests=10000 –oltp-auto-inc=off –mysql-engine-trx=yes prepare


2、测试


# sysbench –debug=off –test=oltp –mysql-host=10.15.2.137 –mysql-user=test –mysql-password=test –oltp-table-size=1000000 –mysql-db=test –oltp-table-name=stest –num-threads=20 –max-requests=10000 –oltp-auto-inc=off –mysql-engine-trx=yes run


3、删除数据


# sysbench –debug=off –test=oltp –mysql-host=10.15.2.137 –mysql-user=test –mysql-password=test –oltp-table-size=1000000 –mysql-db=test –oltp-table-name=stest –num-threads=20 –max-requests=10000 –oltp-auto-inc=off –mysql-engine-trx=yes cleanup


好了,主要的就是这些了,想要了解更多信息就访问 sysbench 项目的主页吧。




用mysqlslap对MySQL进行压力测试

自己实践:
delimiter //
drop procedure  if exists sp_test  //
create procedure sp_test()
begin
   select * tuike_users;
end
//
show procedure status //
mysqlslap - - defaults- file = /etc / mysql/ my. cnf - - concurrency= 25, 50, 100 - - iterations= 1 - - query= 'call tuike.sp_test();' - - number- of- queries= 5000 - - debug- info - uroot -
==================================================
MySQL5.1地的确提供了好多有力的工具来帮助我们DBA进行数据库管理。
现在看一下这个压力测试工具mysqlslap.
关于他的选项手册上以及--help介绍的很详细。
我解释一下一些常用的选项。
这里要注意的几个选项:
--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 意思同上。
--create-schema 代表自己定义的模式(在MySQL中也就是库)。
--query 代表自己的SQL脚本。
--only-print 如果只想打印看看SQL语句是什么,可以用这个选项。

现在来看一些我测试的例子。

1、用自带的SQL脚本来测试。
MySQL版本为5.1.23
[ root@ localhost ~ ] # mysqlslap - - defaults- file = / usr/ local/ mysql- maria/ my. cnf - - concurrency= 50, 100, 200 - - iterations= 1 - - number- int- cols = 4 - - number- char - cols = 35 - - auto- generate- sql - - auto- generate- sql- add- autoincrement - - auto- generate- sql- load- type = mixed - - engine= myisam, innodb - - number- of- queries= 200 - - debug- info - uroot - p1 - S / tmp/ mysql_3310.

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.063 seconds
        Minimum number of seconds to run all queries: 0.063 seconds
        Maximum number of seconds to run all queries: 0.063 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.070 seconds
        Minimum number of seconds to run all queries: 0.070 seconds
        Maximum number of seconds to run all queries: 0.070 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2

Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.092 seconds
        Minimum number of seconds to run all queries: 0.092 seconds
        Maximum number of seconds to run all queries: 0.092 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.115 seconds
        Minimum number of seconds to run all queries: 0.115 seconds
        Maximum number of seconds to run all queries: 0.115 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.134 seconds
        Minimum number of seconds to run all queries: 0.134 seconds
        Maximum number of seconds to run all queries: 0.134 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2

Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 0.192 seconds
        Minimum number of seconds to run all queries: 0.192 seconds
        Maximum number of seconds to run all queries: 0.192 seconds
        Number of clients running queries: 200
        Average number of queries per client: 1


User time 0.06, System time 0.15
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 5803, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 8173, Involuntary context switches 528


我来解释一下结果的含义。
拿每个引擎最后一个Benchmark示例。
对于INNODB引擎,200个客户端同时运行这些SQL语句平均要花0.192秒。相应的MYISAM为0.092秒。

2、用我们自己定义的SQL 脚本来测试。
这些数据在另外一个MySQL实例上。版本为5.0.45
先看一下这两个表的相关数据。
1)、总记录数。
mysql> select table_rows as rows from information_schema. tables where table_schema= 't_girl' and table_name= 'article' ;
+ - - - - - - - - +
| rows   |
+ - - - - - - - - +
| 296693 |
+ - - - - - - - - +
1 row in set ( 0. 01 sec)

mysql> select table_rows as rows from information_schema. tables where table_schema= 't_girl' and table_name= 'category' ;
+ - - - - - - +
| rows |
+ - - - - - - +
| 113  |
+ - - - - - - +
1 row in set ( 0. 00 sec)

2)、总列数。
mysql> select count ( * ) as column_total from information_schema. columns where table_schema = 't_girl' and table_name = 'article' ;
+ - - - - - - - - - - - - - - +
| column_total |
+ - - - - - - - - - - - - - - +
| 32           |
+ - - - - - - - - - - - - - - +
1 row in set ( 0. 01 sec)

mysql> select count ( * ) as column_total from information_schema. columns where table_schema = 't_girl' and table_name = 'category' ;
+ - - - - - - - - - - - - - - +
| column_total |
+ - - - - - - - - - - - - - - +
| 9            |
+ - - - - - - - - - - - - - - +
1 row in set ( 0. 01 sec)


3)、调用的存储过程
DELIMITER $$

DROP PROCEDURE IF EXISTS `t_girl` . `sp_get_article` $ $

CREATE DEFINER= `root` @ `%` PROCEDURE `sp_get_article` ( IN f_category_id int ,
 IN f_page_size int , IN f_page_no int
)
BEGIN
  set @ stmt = 'select a.* from article as a inner join ' ;
  set @ stmt = concat ( @ stmt , '(select a.aid from article as a ' ) ;
  if f_category_id ! = 0 then
    set @ stmt = concat ( @ stmt , ' inner join (select cid from category where cid = ' , f_category_id, ' or parent_id = ' , f_category_id, ') as b on a.category_id = b.cid' ) ;
  end if;
  if f_page_size > 0 & & f_page_no > 0 then
    set @ stmt = concat ( @ stmt , ' limit ' , ( f_page_no-1) * f_page_size, ',' , f_page_size) ;
  end if;
 
  set @ stmt = concat ( @ stmt , ') as b on (a.aid = b.aid)' ) ;
  prepare s1 from @ stmt ;
  execute s1;
  deallocate prepare s1;
  set @ stmt = NULL ;
END $ $

DELIMITER ;

4)、我们用mysqlslap来测试
以下得这个例子代表用mysqlslap来测试并发数为25,50,100的调用存储过程,并且总共调用5000次。
[ root@localhost ~ ] # mysqlslap - - defaults- file = / usr/ local/ mysql- maria/ my. cnf - - concurrency= 25, 50, 100 - - iterations= 1 - - query= 'call t_girl.sp_get_article(2,10,1);' - - number- of- queries= 5000 - - debug- info - uroot - p - S/ tmp/ mysql50.
Enter password:
Benchmark
        Average number of seconds to run all queries: 3.507 seconds
        Minimum number of seconds to run all queries: 3.507 seconds
        Maximum number of seconds to run all queries: 3.507 seconds
        Number of clients running queries: 25
        Average number of queries per client: 200
平均每个并发运行200个查询用了3.507秒。
Benchmark
        Average number of seconds to run all queries: 3.742 seconds
        Minimum number of seconds to run all queries: 3.742 seconds
        Maximum number of seconds to run all queries: 3.742 seconds
        Number of clients running queries: 50
        Average number of queries per client: 100

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


User time 0.87, System time 0.33
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 1877, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 27218, Involuntary context switches 3100


看一下SHOW PROCESSLIST 结果
mysql> show processlist;
+------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+
| Id   | User | Host               | db                 | Command | Time  | State              | Info                                                                                                 |
+------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+
…………
| 3177 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3178 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3179 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3181 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3180 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3182 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3183 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3187 | root | %                  | t_girl             | Query   |     0 | removing tmp table | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3186 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3194 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3203 | root | %                  | t_girl             | Query   |     0 | NULL               | deallocate prepare s1                                                                                |
…………
| 3221 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3222 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3223 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3224 | root | %                  | t_girl             | Query   |     0 | removing tmp table | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3225 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
| 3226 | root | %                  | t_girl             | Query   |     0 | NULL               | select a.* from article as a inner join (select a.aid from article as a  inner join (select cid from |
+------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+
55 rows in set (0.00 sec)

上面的测试语句其实也可以这样写


[ root@localhost ~ ] # mysqlslap - - defaults- file = / usr/ local/ mysql- maria/ my. cnf - - concurrency= 25, 50, 100 - - iterations= 1 - - create- schema= 't_girl' - - query= 'call sp_get_article(2,10,1);' - - number- of- queries= 5000 - - debug- info - uroot - p - S/ tmp/ mysql50.


小总结一下。
mysqlslap对于模拟多个用户同时对MySQL发起“进攻”提供了方便。同时详细的提供了“高负荷攻击MySQL”的详细数据报告。
而且如果你想对于多个引擎的性能。这个工具再好不过了。