sysbench使用及自定义oltp测试lua脚本

sysbench是一个多线程基准测试工具,用于评估系统的数据库、CPU、内存等性能。本文详细介绍了sysbench的安装、配置、各测试模式,特别是自定义OLTP测试和lua脚本的使用,适用于数据库性能调优和系统基准测试。
摘要由CSDN通过智能技术生成

一、sysbench介绍

sysbench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。 
目前sysbench代码托管在launchpad上,项目地址:https://launchpad.net/sysbench(原来的官网http://sysbench.sourceforge.net 已经不可用),源码采用bazaar管理。 
注:本文所有的测试都是基于Linux操作系统MySQL数据库的。

二、sysbench安装

1、依赖包:

  • autoconf
  • automake
  • cdbs
  • debhelper (>= 9)
  • docbook-xml
  • docbook-xsl
  • libmysqlclient15-dev
  • libtool
  • xsltproc

2、编译安装:

#./autogen.sh
#./configure
#make

注意事项:./configure命令,sysbench默认是支持MySQL的benchmarking的,如果不加任何选项则要求保证MySQL的安装路径都是默认的标准路径,headfile位于/usr/include目录下,libraries位于/usr/lib/目录下。因为我的MySQL是源码编译安装的,安装路径是放在/usr/local/mysql下,所以这里要添加相应的选项命令: 
./configure --prefix=/usr/local/sysbench --with-mysql=/usr/local/mysql \ 
--with-mysql-includes=/usr/local/mysql/include/ \ 
--with-mysql-libs=/usr/local/mysql/lib/

这样就可以看到/usr/local/sysbench下有一个可执行文件sysbench,这就是sysbench的主程序。

3、功能简介

sysbench目前可以进行如下几个方面的性能测试: 
- fileio - File I/O test #磁盘io性能 
- cpu - CPU performance test #CUP性能 
- memory - Memory functions speed test #内存性能 
- threads - Threads subsystem performance test #POSIX线程性能 
- mutex - Mutex performance test #调度程序性能 
- oltp - OLTP test #数据库性能(OLTP基准测试)

注:在0.4版本的--test选项中是可以直接选用oltp模式,但是在0.4.12.1.1以后oltp测试就转换成调用lua脚本来进行测试了,脚本主要存放在tests/db目录下。这样用户就可以根据自己的系统定制lua脚本,这样的测试就能更精确的测试业务的性能。
 
 
 
  • 1
  • 1

三、开始进行测试

通用配置

接下来我们来分别看一下各个模式的相关参数、测试方法和结果分析。 
sysbench的基本命令格式为: 
sysbench –test=< test-name> [options]… < command> 
主要分为三个部分:

1、–test=< test-name>

这部分是指定测试类型,基本类型有fileio,cpu,memory,threads,mutex,oltp(或者指定lua脚本)

2、[options]…

这部分包括测试需要的各种选项,有全局的也有每个测试模式自由的选项 
(每个测试模式的选项可以用./sysbench –test=< test-name> help来获取)

3、< command>

控制命令,总共有五个 
prepare #准备测试,主要是生成测试数据 
run #执行测试,根据选项限制来执行测试 
cleanup #清除准备阶段生成的测试数据 
help #获取帮助文档 
version #获取版本信息

几个重要的全局参数:

–num-threads=N number of threads to use [1] #测试时使用的线程数 
–max-requests=N limit for total number of requests [10000] #测试过程最多执行多少次请求 
–max-time=N limit for total execution time in seconds [0] #测试过程总共执行多长时间(和–max-requests效果同样,但是两个同时限定的时候谁优先还没有测试) 
–report-interval=N periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0] #每隔多少秒输出测试概况(这个过程你可以观察到mysql redolog的切换情况) 
–db-driver=STRING specifies database driver to use (‘help’ to get list of available drivers) #指定需求测试的数据库类型,默认是mysql 
#mysql链接选项 
–mysql-host=[LIST,…] MySQL server host [localhost] #mysql主机地址 
–mysql-port=N MySQL server port [3306] #mysql端口 
–mysql-socket=[LIST,…] MySQL socket #mysql socket文件位置,指定这个之后 其他的链接选项均可以不指定 
–mysql-user=STRING MySQL user [sbtest] #用来测试的mysql用户名 
–mysql-password=STRING MySQL password [] #密码 
–mysql-db=STRING MySQL database name [sbtest] #测试数据库名 默认sbtest

接下来进入各个测试模式的测试方法

fileio-磁盘io性能

1、主要参数
[root@centostest sysbench]# ./sysbench --test=fileio help
sysbench 0.5:  multi-threaded system evaluation benchmark

fileio options:
  --file-num=N                  number of files to create [128]
  --file-block-size=N           block size to use in all IO operations [16384]
  --file-total-size=SIZE        total size of files to create [2G]
  --file-test-mode=STRING       test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
  --file-io-mode=STRING         file operations mode {sync,async,mmap} [sync]
  --file-extra-flags=STRING     additional flags to use on opening files {sync,dsync,direct} []
  --file-fsync-freq=N           do fsync() after this number of requests (0 - don't use fsync()) [100]
  --file-fsync-all=[on|off]     do fsync() after each write operation [off]
  --file-fsync-end=[on|off]     do fsync() at the end of test [on]
  --file-fsync-mode=STRING      which method to use for synchronization {fsync, fdatasync} [fsync]
  --file-merged-requests=N      merge at most this number of IO requests if possible (0 - don't merge) [0]
  --file-rw-ratio=N             reads/writes ratio for combined test [1.5]

No help available for test 'fileio'.
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

参数详解: 
–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、测试实例

测试总大小为5G的10个文件的随机读写性能:

1>先生成测试文件
sysbench --test=fileio --file-num=10 --file-total-size=5G prepare
 
 
 
  • 1
  • 1
2>开始测试
sysbench --test=fileio --file-total-size=5G --file-test-mode=rndrw --max-time=180 --max-requests=100000000 --num-threads=16 --init-rng=on --file-num=10 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=16384 run
 
 
 
  • 1
  • 1
3>结果分析
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 16
Initializing random number generator from timer.

Random number generator seed is 0 and will be ignored


Extra file open flags: 4000
10 files, 512Mb each
5Gb total file size
Block size 16Kb
Number of random requests for random IO: 100000000
Read/Write ratio for combined random IO test: 1.50
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Threads started!
Time limit exceeded, exiting...
(last message repeated 15 times)
Done.

Operations performed:  89247 reads, 59488 writes, 0 Other = 148735 Total
Read 1.3618Gb  Written 929.5Mb  Total transferred 2.2695Gb  (12.888Mb/sec)
  824.84 Requests/sec executed

General statistics:
    total time:                          180.3188s
    total number of events:              148735
    total time taken by event execution: 2882.8395
    response time:
         min:                                  0.08ms
         avg:                                 19.38ms
         max:                                953.75ms
         approx.  95 percentile:             158.81ms

Threads fairness:
    events (avg/stddev):           9295.9375/371.20
    execution time (avg/stddev):   180.1775/0.07
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

主要看这行输出的信息:

Read 1.3618Gb  Written 929.5Mb  Total transferred 2.2695Gb  (12.888Mb/sec)
  824.84 Requests/sec executed
 
 
 
  • 1
  • 2
  • 1
  • 2

这行信息表示:在180秒时间里面总共完成随机读取1.3618G数据,写入929.5Mb数据,平均每秒随机读写的效率为12.888Mb/秒,IOPS为824.84 Requests/sec 
因为是虚拟机,所有磁盘的表现还是比较差的。

4>清除测试数据
[root@centostest sysbench]# sysbench --test=fileio --file-num=10 --file-total-size=5G cleanup
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Removing test files...
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

cpu-cpu性能测试

1、主要参数
[root@centostest sysbench]# sysbench --test=cpu help
sysbench 0.4.12:  multi-threaded system evaluation benchmark

cpu options:
  --cpu-max-prime=N      upper limit for primes generator [10000]
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

参数详解: 
–cpu-max-prime=N 用来选项指定最大的素数,具体参数可以根据CPU的性能来设置,默认为10000

2、测试实例

根据官网的介绍可知:CPU测试使用64位整数,测试计算素数直到某个最大值所需要的时间。

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

输出如下:

[root@centostest sysbench]# sysbench --test=cpu --cpu-max-prime=20000 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 1
Random number generator seed is 0 and will be ignored


Doing CPU performance benchmark

Primer numbers limit: 20000

Threads started!
Done.


General statistics:
    total time:          
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值