sysbench测试mysql

一、sysbench安装

 

sysbench压力测试工具简介:

sysbench是一个开源的、模块化的、跨平台的多线程性能测试工具,可以用来进行CPU、内存、磁盘I/O、线程、数据库的性能测试。目前支持的数据库有MySQL、Oracle和PostgreSQL。以下操作都将以支持MySQL数据库为例进行。
Sysbench的测试主要包括以下几个方面:
1、磁盘io性能
2、cpu性能
3、内存分配及传输速度
4、POSIX线程性能
5、调度程序性能
6、数据库性能(OLTP基准测试).
 

1)下载解压
yum install gcc gcc-c++ automake make libtool mysql-community-devel mysql-devel

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)安装成功
 sysbench —version

sysbench语法

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

sysbench的基本语法如下:

sysbench [options]... [testname] [command]

下面说明实际使用中,常用的参数和命令。

(1)command

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

* prepare

准备阶段,也就是装载数据。

filo test 中: 就是创建指定大小的文件

oltp test 中: 就是创建指定大小的表

 

* run

实际测试阶段

 

* cleanup

收尾阶段,清除之前测试的数据。

只介绍常用的选项

选项

描述

默认值

--num-threads

多少个线程

1

--max-requests

多少个请求,0意味着无限制

1000

--max-time

测试多长时间,0意味着无限制

0

--test

测试什么模块

必须要求

--report-interval

阶段性的汇报测试统计信息,0.4以上版本新增

 

  • --test=fileio 模块的选项

提前注明:--file-test-mode

* seqwr

sequential write

 

* seqrewr

sequential rewrite

 

* seqrd

sequential read

 

* rndrd

random read

 

* rndwr

random write

 

* rndrw

combined random read/write

  • test option for fileio

选项

描述

默认值

--file-num

创建文件的数量

128

--file-block-size

IO操作的大小

16k

--file-total-size

所有文件的总大小

2G

--file-test-mode

seqwr,seqrewr, seqrd, rndrd, rndwr, rndwr(上面已经介绍)

必须

--file-io-mode

i/O 模式,sync, async, fastmmap, slowmmap

sync

--file-extra-flags

以额外的标记(O_SYNC,O_DSYNC,O_DIRECT)打开

-

--file-fsync-freq

多少请求后使用fsync

100

--file-fsync-all

每次写IO都必须fsync

no

--file-fsync-mode

用什么样的模式来同步文件fsync, fdatasync (see above)

fsync

--file-rw-ratio

随机读写请求的比例

1.5

举例:

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

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

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

(2)testname

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

 

3)options

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

MySQL连接信息参数

  • --mysql-host:MySQL服务器主机名,默认localhost;如果在本机上使用localhost报错,提示无法连接MySQL服务器,改成本机的IP地址应该就可以了。

  • --mysql-port:MySQL服务器端口,默认3306

  • --mysql-user:用户名

  • --mysql-password:密码

MySQL执行参数

  • --oltp-test-mode:执行模式,包括simple、nontrx和complex,默认是complex。simple模式下只测试简单的查询;nontrx不仅测试查询,还测试插入更新等,但是不使用事务;complex模式下测试最全面,会测试增删改查,而且会使用事务。可以根据自己的需要选择测试模式。

  • --oltp-tables-count:测试的表数量,根据实际情况选择

  • --oltp-table-size:测试的表的大小,根据实际情况选择

  • --threads:客户端的并发连接数

  • --time:测试执行的时间,单位是秒,该值不要太短,可以选择120

  • --report-interval:生成报告的时间间隔,单位是秒,如10

sysbench使用举例

sysbench使用的一个例子:

(1)准备数据

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

或者

sysbench --test=/home/sysbench-1.0/tests/include/oltp_legacy/oltp.lua --mysql-host=ip --mysql-port=3306 --mysql-user=root --mysql-password=pwd --oltp-test-mode=complex --oltp-tables-count=100 --oltp-table-size=1000000 --threads=100 --time=120 --report-interval=100 prepare

 

注意最后一行,一项测试开始前需要用prepare来准备好表和数据,run执行真正的压测,cleanup用来清除数据和表。实际prepare的表结构:

Create database subtest;

此模式用于测试真实数据库性能。在prepare阶段创建表,sbtest默认

CREATE TABLE `sbtest` (

`id` int(10) unsigned NOT NULL auto_increment,

`k` int(10) unsigned NOT NULL default '0',

`c` char(120) NOT NULL default '',

`pad` char(60) NOT NULL default '',

PRIMARY KEY  (`id`),

KEY `k` (`k`));

 

其中,执行模式为complex,使用了10个表,每个表有10万条数据,客户端的并发线程数为10,执行时间为120秒,每10秒生成一次报告。

 

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

5、测试结果

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

queries:查询总数及qps

transactions:事务总数及tps

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

混合读写

读写测试还是用oltp.lua,只需把--oltp-read-only等于off。

CREATE TABLE `dbtest1a` (

`id` int(10) unsigned NOT NULL auto_increment,

`k` int(10) unsigned NOT NULL default '0',

`c` char(120) NOT NULL default '',

`pad` char(60) NOT NULL default '',

PRIMARY KEY  (`id`),

KEY `k` (`k`));

 

./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 prepare

 

./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 run

 

./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx --oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 cleanup

 

测试fileio命令帮助:

[root@service82 sysbench]# sysbench  --test=fileio help
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.16 (using bundled LuaJIT 2.1.0-beta2)

参数详解: --file-num=N          代表生成测试文件的数量,默认为128。
--file-block-size=N      测试时所使用文件块的大小,如果想磁盘针对innodb存储引擎进行测试,可以将其设置为16384,即innodb存储引擎页的大小。默认为16384。
--file-total-size=SIZE     创建测试文件的总大小,默认为2G大小。
--file-test-mode=STRING 文件测试模式,包含:seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)。
--file-io-mode=STRING   文件操作的模式,sync(同步),async(异步),fastmmap(快速mmap),slowmmap(慢速mmap),默认为sync同步模式。
--file-async-backlog=N   对应每个线程队列的异步操作数,默认为128。
--file-extra-flags=STRING 打开文件时的选项,这是与API相关的参数。
--file-fsync-freq=N      执行fsync()函数的频率。fsync主要是同步磁盘文件,因为可能有系统和磁盘缓冲的关系。 0代表不使用fsync函数。默认值为100。
--file-fsync-all=[on|off]  每执行完一次写操作,就执行一次fsync。默认为off。
--file-fsync-end=[on|off] 在测试结束时执行fsync函数。默认为on。
--file-fsync-mode=STRING文件同步函数的选择,同样是和API相关的参数,由于多个操作系统对于fdatasync支持不同,因此不建议使用fdatasync。默认为fsync。
--file-merged-requests=N 大多情况下,合并可能的IO的请求数,默认为0。
--file-rw-ratio=N         测试时的读写比例,默认时为1.5,即可3:2。


 测试cpu命令帮助:

ysbench --test=cpu help sysbench 0.4.12: multi-threaded system evaluation benchmark cpu options: 参数详解: --cpu-max-prime=N 用来选项指定最大的素数,具体参数可以根据CPU的性能来设置,默认为10000
 

测试memory命令帮助:

sysbench  --test=memory help
WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.16 (using bundled LuaJIT 2.1.0-beta2)

参数详解: --memory-block-size=SIZE 测试内存块的大小,默认为1K --memory-total-size=SIZE 数据传输的总大小,默认为100G --memory-scope=STRING 内存访问的范围,包括全局和本地范围,默认为global --memory-hugetlb=[on|off] 是否从HugeTLB池分配内存的开关,默认为off --memory-oper=STRING 内存操作的类型,包括read, write, none,默认为write --memory-access-mode=STRING 内存访问模式,包括seq,rnd两种模式,默认为seq
 

测试threads命令帮助

sysbench  --test=threads help

参数详解: 
  --thread-yields=N      指定每个请求的压力,默认为1000
  --thread-locks=N       指定每个线程的锁数量,默认为8

测试:

./sysbench --test=tests/db/oltp.lua --max-time=100 --oltp-dist-type=uniform --max-requests=0 --mysql-user=root --mysql-password="" --mysql-table-engine=innodb
--oltp-table-size=100 --oltp-tables-count=8 --oltp-range-size=90 --oltp-point-selects=1 --oltp-simple-ranges=1 --oltp-sum-ranges=1 --oltp-order-ranges=1 --oltp-distinct-ranges=1 --oltp-non-index-updates=10 --num-threads=20  --mysql-host=127.0.0.1 --mysql-port=3306 run

参数说明:
–test 指定使用的测试脚本
–max-time=100 指定测试时长为100s
–mysql-user 数据库帐号
–mysql-password 数据库密码
–mysql-table-engine 存储引擎类型
–test 测试的类型oltp系统
–num-threads 测试的线程数
–mysql-host 数据库地址
–mysql-port 数据库端口

测试结果:

Threads started!

OLTP test statistics:
    queries performed:
        read:                            179470-- 读总数
        write:                           453935-- 写总数
        other:                           70678-- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
        total:                           704083 -- 全部总数
    transactions事务量:                        34784  (347.32 per sec.)-- 总事务数(每秒事务数)
    read/write requests读写总操作:                 633405 (6324.57 per sec.) -- 读写总数(每秒读写次数)
    other operations其他操作:                    70678  (705.72 per sec.)-- 其他操作总数(每秒其他操作次数)
    ignored errors:                      1110   (11.08 per sec.)-- 每秒忽略的错误
    reconnects:                          0      (0.00 per sec.)--每秒重连次数

General statistics:
    total time(总时长):                          100.1499s -- 总耗时
    total number of events:              34784 -- 共发生多少事务数
    total time taken by event execution: 1996.2507s -- 所有事务耗时相加(不考虑并行因素)
    response time(相应时间):
         min最小:                                  2.54ms-- 最小耗时
         avg平均:                                 57.39ms-- 平均耗时
         max最大:                               1074.85ms -- 最长耗时
         approx.  95 percentile:             133.50ms-- 超过99%平均耗时

Threads fairness:
    events (avg/stddev):           1739.2000/35.03
    execution time (avg/stddev):   99.8125/0.05
 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值