【MySQL】Sysbench 性能压测

合成自:http://linuxperformance.top/index.php/archives/83/

            https://blog.csdn.net/notbaron/article/details/77413379

            https://www.cnblogs.com/kismetv/archive/2017/09/30/7615738.html#t24

sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试。

sysbench支持以下几种测试模式:

1、CPU运算性能
2、磁盘IO性能
3、调度程序性能
4、内存分配及传输速度
5、POSIX线程性能
6、数据库性能(OLTP基准测试)

下载地址及安装(Linux)

     下载地址:https://github.com/akopytov/sysbench

安装

(1)下载解压
wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbench-1.0.zip"
unzip sysbench-1.0.zip
cd sysbench-1.0
(2)安装依赖
yum install automake libtool –y
(3)安装
安装之前,确保位于之前解压的sysbench目录中。

./autogen.sh
./configure
export LD_LIBRARY_PATH=/usr/local/mysql/include #这里换成机器中mysql路径下的include
make
make install
(4)安装成功
[root@test sysbench-1.0]# sysbench --version
sysbench 1.0.9

     如果出现:configure: error: mysql_config executable not found
     需要安装:mariadb-devel (yum install mariadb-devel 安装mysql_config)

PS:此外可能还要安装mariadb-server包,yum install mariadb-server。

1、CPU测试

     对CPU的性能测试通常有:1.质数计算;2圆周率计算;sysbench使用的就是通过质数相加的测试。对CPU测试直接运行run即可

sysbench --threads=20 --events=10000 --debug=on --test=cpu --cpu-max-prime=20000 run
20个线程执行1万条请求,每个请求执行质数相加到20000   

2、内存测试

测试8K顺序分配:
sysbench --threads=12 --events=10000 --test=memory --memory-block-size=8K --memory-total-size=100G --memory-access-mode=seq run
测试8K随机分配。
sysbench --threads=12 --events=10000 --test=memory --memory-block-size=8K --memory-total-size=100G --memory-access-mode=rnd run

3、文件io测试

IO的测试主要用于测试IO的负载性能。主要测试选项为--file-test-mode。还有可以关注的参数包括--file-block-size、--file-io-mode、--file-fsync-freq 、--file-rw-ratio,具体参数含义请见参数解释章节。
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw prepare
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw run
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw cleanup
对比两台服务器的io性能,需要跑相同的线程

 

4、锁测试

互斥锁测试模拟所有线程在同一时刻并发运行。
sysbench --threads=12 mutex --mutex-num=1024 --mutex-locks=10000 --mutex-loops=10000 run

5、线程测试

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

6、OLTP测试

     oltp是针对数据库的基准测试,例如每次对数据库进行优化后执行基准测试来测试不同的配置的tps。sysbench 0.5之后通过一系列LUA脚本来替换之前的oltp,来模拟更接近真实的基准测试环境。这些测试脚本包含:insert.lua、oltp.lua、parallel_prepare.lua、select_random_points.lua、update_index.lua、delete.lua oltp_simple.lua、select.lua、select_random_ranges.lua、update_non_index.lua

预置条件:
a)创建数据库:
mysqladmin create sbtest -uroot –p
或者
SQL>create database sbtest
b)增加权限:
grant usage on . to 'sbtest'@'%' identified by password '*2AFD99E79E4AA23DE141540F4179F64FFB3AC521';
其中密码通过如下命令获取:
select password('sbtest');
+-------------------------------------------+
| password('sbtest') |
+-------------------------------------------+
| 2AFD99E79E4AA23DE141540F4179F64FFB3AC521 |
+-------------------------------------------+
1 row in set (0.00 sec)
c)增加权限:
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX ON sbtest.TO 'sbtest'@"%";
grant all privileges on . to 'sbtest'@'%';
flush privileges;
或者简单粗暴:
create user 'sbtest'@'127.0.0.1' identified by 'sbtest';
grant all privileges on . to
flush privileges;
e)OLTP测试:
准备阶段:
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on prepare
测试阶段:
命令如下:
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on run
清理阶段:
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on cleanup
最后删除数据库
SQL>drop database sbtest;

完整实例:

1)、准备数据

执行模式为complex,使用了10个表,每个表有10万条数据,客户端的并发线程数为10,执行时间为120秒,每10秒生成一次报告。
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 prepare

2)、执行测试

//将测试结果导出到文件中,便于后续分析
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 run >> /home/test/mysysbench.log

3)、清理数据

//执行完测试后,清理数据,否则后面的测试会受到影响
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 cleanup

4)、测试结果

其中,对于我们比较重要的信息包括:

queries:查询总数及qps

transactions:事务总数及tps

Latency-95th percentile:前95%的请求的最大响应时间,本例中是344毫秒,这个延迟非常大,是因为我用的MySQL服务器性能很差;在正式环境中这个数值是绝对不能接受的。

语法 sysbench [options]... [testname] [command]

1)、执行sysbench –help,可以看到sysbench的详细使用方法。

2)、command,要执行的命令,包括prepare、run和cleanup,顾名思义,prepare是为测试提前准备数据,run是执行正式的测试,cleanup是在测试完成后对数据库进行清理。

3)、testname指定了要进行的测试,在老版本的sysbench中,可以通过--test参数指定测试的脚本;而在新版本中,--test参数已经声明为废弃,可以不使用--test,而是直接指定脚本。

4)、options:sysbench的参数有很多,其中比较常用的包括:

7、参数解释

通过命令sysbench –help可以了解各参数的具体解释
--test=tests/db/oltp.lua 表示调用 tests/db/oltp.lua 脚本进行 oltp 模式测试
--threads:客户端的并发连接数
--time:测试执行的时间,单位是秒,该值不要太短,可以选择120
--report-interval:生成报告的时间间隔,单位是秒,如10
--oltp_tables_count=10 表示会生成 10 个测试表
--oltp-table-size=100000 表示每个测试表填充数据量为 100000
--mysql-engine-trx=STRING指定不同的存储引擎测试。
--oltp-test-mode=STRING测试类型:simple(简单select测试),complex(事务测试),nontrx(非事务测试),sp(存储过程) ;默认complex
--oltp-sp-name=STRING指定存储过程进行语句测试
--oltp-table-size=N指定表的记录大小,默认[10000]
--oltp-num-tables=N指定测试表的数量,默认1
--rand-init=on 表示每个测试表都是用随机数据来填充的
--file-num=N创建测试文件的数量,默认128个
--file-block-size=N block size大小,默认16K
--file-total-size=SIZE所有文件的总大小,默认2G
--file-test-mode=STRING测试类型 {seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)} --file-io-mode=STRING I/O模式,需要系统支持默认sync[sync(同步IO),async(异步IO),mmap()]
--file-async-backlog=N每个线程的异步操作队列数,默认128个,需要--file-io-mode=async;
--file-extra-flags=STRING additional flags to use on opening files {sync,dsync,direct} []
--file-fsync-freq=N当请求数达到多少时执行fsync()刷新,默认100,0代表过程中不执行fsync()
--file-fsync-all=[on|off] 执行每一个写操作后执行fsync()刷新操作,默认关闭off --file-fsync-end=[on|off] 测试结束执行fsync()操作,默认开启on --file-fsync-mode=STRING 同步刷新方法,默认fsync {fsync, fdatasync} --file-merged-requests=N合并指定数量的IO请求,0代表不合并,默认0 --file-rw-ratio=N 读写比例,默认1.5/1

8、建议、注意

建议:

1)、在开始测试之前,应该首先明确:应采用针对整个系统的基准测试,还是针对MySQL的基准测试,还是二者都需要。

2)、如果需要针对MySQL的基准测试,那么还需要明确精度方面的要求:是否需要使用生产环境的真实数据,还是使用工具生成也可以;前者实施起来更加繁琐。如果要使用真实数据,尽量使用全部数据,而不是部分数据。

3)、要进行多次才有意义。需要注意主从同步的状态。测试必须模拟多线程的情况,单线程情况不但无法模拟真实的效率,也无法模拟阻塞甚至死锁情况。

注意:

1)、尽量不要在MySQL服务器运行的机器上进行测试,一方面可能无法体现网络(哪怕是局域网)的影响,另一方面,sysbench的运行(尤其是设置的并发数较高时)会影响MySQL服务器的表现。

2)、可以逐步增加客户端的并发连接数(--thread参数),观察在连接数不同情况下,MySQL服务器的表现;如分别设置为10,20,50,100等。

3)、一般执行模式选择complex即可,如果需要特别测试服务器只读性能,或不使用事务时的性能,可以选择simple模式或nontrx模式。

4)、如果连续进行多次测试,注意确保之前测试的数据已经被清理干净。

谢谢分享:

合成自:http://linuxperformance.top/index.php/archives/83/

            https://blog.csdn.net/notbaron/article/details/77413379

            https://www.cnblogs.com/kismetv/archive/2017/09/30/7615738.html#t24

猜你喜欢

转载自blog.csdn.net/ma15732625261/article/details/81355269
今日推荐