您的位置:首页 > 运维架构 > Linux

Linux性能优化(二)——sysbench压力测试工具

2021-01-16 18:43 891 查看

一、sysbench简介

1、sysbench简介

sysBench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。sysbench提供如下测试:
(1)CPU性能
(2)磁盘IO性能
(3)调度程序性能
(4)内存分配及传输速度 
(5)POSIX线程性能
(6)数据库性能(OLTP基准测试)      
sysbench支持 MySQL、PostgreSQL、Oracle 数据库。

2、sysbench安装

Build构建依赖安装:

yum -y install make automake libtool pkgconfig libaio-devel
yum -y install mariadb-devel openssl-devel
yum -y install postgresql-devel

源码下载:

git clone https://github.com/akopytov/sysbench.git

生成配置工具:
autogen.sh

配置选项:
--with-pgsql:支持PostgreSQL
--with-oracle:支持Oracle
--without-mysql:无MySQL支持
configure [options]
编译:
make -j

安装:
make install

3、sysbench命令

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

testname是sysbench要执行的测试类型,可选项包括fileio、cpu、memory、threads、mutex,可以使用--test参数指定。
command是sysbench要执行的命令,包括prepare、run、cleanup,prepare为测试准备数据,run执行正式测试,cleanup在测试完成后对数据库进行清理。
sysbench通用参数选项如下:
--threads: 线程数,若设置为2,则sysbench会启动2个线程,同时分别进行素数的计算,默认值为1。
--time: 运行时长,单位秒。若设置为5,则sysbench会在5秒内循环进行素数计算,每完成一轮就是一个event,默认值为10。
--events: event上限次数,若设置为100,则表示当完成100次event后,即使时间富余也停止运行。默认值为0,则表示不限event次数。
--forced-shutdown=STRING:超时强制中断,默认为off。 --thread-stack-size=SIZE:线程栈大小,默认64K
--thread-init-timeout=N:线程初始化等待时间,默认为30秒
--rate=N:平均事务率,0表示不限制,默认为0。
--report-interval=N:测试进度报告输出的时间间隔,0表示关闭,默认为0。
--report-checkpoints=[LIST,...]:转储完全统计信息并在指定时间点复位所有计数器,参数是逗号分隔值的列表,表示从必须执行报告检查点的测试开始所经过的时间(以秒为单位)。 默认情况下,报告检查点处于关闭状态[off]。
--debug[=on|off]:打印调试信息,默认为off
--validate[=on|off]:尽可能执行验证检查,默认为off。
--help[=on|off]:打印help信息并退出,默认为off
--version[=on|off]:打印版本信息并退出,默认为off
--config-file=FILENAME:命令行选项文件
--luajit-cmd=STRING:执行LuaJIT控制命令

二、CPU性能基准测试

1、命令选项

sysbench --test=cpu help

--cpu-max-prime:素数生成数量的上限,若设置为3,则表示2、3、5(要计算1-5共5次),默认值为10000。

2、CPU性能测试

sysbench cpu --cpu-max-prime=20000 --threads=8 --time=30 run


Prime numbers limit: 20000,每个线程产生的素数上限均为2000
events per second: 650.7,所有线程每秒完成了650.74次event
total time: 10.0017s,共耗时10秒
total number of events: 6510,10秒内所有线程一共完成了6510次event
min: 3.03,完成1次event的最少耗时3.03秒
avg: 3.07,所有event的平均耗时3.07毫秒
max: 3.27,完成1次event的最多耗时3.27毫秒
95th percentile: 3.13, 95%次event在3.13秒毫秒内完成
sum: 19999.91,每个线程耗时10秒,2个线程叠加耗时就是20秒
events (avg/stddev): 3255.0000/44.00 // 平均每个线程完成3255次event,标准差为44
execution time (avg/stddev): 10.0000/0.00 // 每个线程平均耗时10秒,标准差为0

三、内存负载测试

 1、命令选项

sysbench  --test=memory help

--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。

2、内存负载测试

sysbench memory --threads=12 --memory-block-size=8K --memory-total-size=100G --memory-access-mode=seq run

开启12个线程,内存块大小为8K,顺序读写

sysbench memory --threads=12 --memory-block-size=8K --memory-total-size=100G --memory-access-mode=rnd run

四、磁盘IO性能基准测试

1、命令选项

 

sysbench --test=fileio help

--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。

2、数据准备

sysbench fileio --file-num=16 --file-total-size=2G prepare

3、执行测试

sysbench fileio --file-total-size=2G --file-test-mode=rndrd --time=180  --threads=16  --file-num=16 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=16384 run

4、清理数据

sysbench fileio --file-num=16 --file-total-size=2G cleanup

清理测试数据

五、多线程调度基准测试

1、命令选项

sysbench  --test=mutex help 

--mutex-num=N:数组互斥的总大小,默认为4096。
--mutex-locks=N:每个线程互斥锁的数量,默认为50000。
--mutex-loops=N:内部互斥锁的空循环数量,默认为10000。

2、多线程调度测试

sysbench mutex --threads=8 --mutex-num=2000 --mutex-locks=10000 --mutex-loops=5000 run

六、POST多线程基准测试

 1、命令选项

 

sysbench  --test=threads help

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

2、多线程测试

sysbench threads --threads=8 --thread-yields=100 --thread-locks=2 run

七、OLTP基准测试

1、命令选项

sysbench --test=oltp help

--oltp-test-mode=STRING:测试模式,simple、complex、nontrx、sp,默认为complex。
--oltp-reconnect-mode=STRING:重连接模式,session不使用重连接,transaction在每次事务结束后重连接,query在每个SQL语句执行完重连接,random对于每个事务随机选择重连接模式,默认为session。
--oltp-sp-name=STRING:存储过程名称,默认为空
--oltp-read-only=[on|off]:只读模式。Update、delete、insert语句不可执行,默认为off
--oltp-skip-trx=[on|off]:省略begin/commit语句,默认为off。
--oltp-range-size=N:查询范围,默认为100。--oltp-point-selects=N:number of point selects [10]
--oltp-simple-ranges=N          number of simple ranges [1]
--oltp-sum-ranges=N             number of sum ranges [1]
--oltp-order-ran 8000 ges=N           number of ordered ranges [1]
--oltp-distinct-ranges=N        number of distinct ranges [1]
--oltp-index-updates=N          number of index update [1]
--oltp-non-index-updates=N      number of non-index updates [1]
--oltp-nontrx-mode=STRING   查询类型对于非事务执行模式{select, update_key, update_nokey, insert, delete} [select]
--oltp-auto-inc=[on|off]      AUTO_INCREMENT是否开启。默认是on
--oltp-connect-delay=N     在多少微秒后连接数据库。默认是10000
--oltp-user-delay-min=N    每个请求最短等待时间。单位是ms。默认是0
--oltp-user-delay-max=N    每个请求最长等待时间。单位是ms。默认是0
--oltp-table-name=STRING  测试时使用到的表名。默认是sbtest
--oltp-table-size=N         测试表的记录数。默认是10000
--oltp-dist-type=STRING    分布的随机数{uniform(均匀分布),Gaussian(高斯分布),special(空间分布)}。默认是special
--oltp-dist-iter=N    产生数的迭代次数。默认是12
--oltp-dist-pct=N    值的百分比被视为'special' (for special distribution)。默认是1
--oltp-dist-res=N    ‘special’的百分比值。默认是75

2、Lua脚本

/usr/share/sysbench/bulk_insert.lua
/usr/share/sysbench/oltp_common.lua
/usr/share/sysbench/oltp_delete.lua
/usr/share/sysbench/oltp_insert.lua
/usr/share/sysbench/oltp_point_select.lua
/usr/share/sysbench/oltp_read_only.lua
/usr/share/sysbench/oltp_read_write.lua
/usr/share/sysbench/oltp_update_index.lua
/usr/share/sysbench/oltp_update_non_index.lua
/usr/share/sysbench/oltp_write_only.lua
/usr/share/sysbench/select_random_points.lua
/usr/share/sysbench/select_random_ranges.lua

3、准备数据

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

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

4、执行测试

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

5、清理数据

sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 cleanup

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: