我们需要知道的是sysbench并不是一个压力测试工具,是一个基准测试工具。linux自带的版本比较低,我们需要自己安装sysbench。

[root@test2 ~]# sysbench --version
sysbench 0.4.12

安装sysbench,sysbench的源码托管在GitHub上,下载源码:

unzip sysbench-master.zip       #解压源码
yum -y install make automake libtool pkgconfig libaio-devel  #下载依赖包
cd sysbench-master
sh autogen.sh
编译:
./configure --with-mysql-includes=/usr/local/mysql/include --with-mysql-libs=/usr/local/mysql/lib   #根据安装的MySQL的位置,设置目录位置
make
make install

这样安装之后使用sysbench命令时会报错。
[root@test3 sysbench-master]# sysbench --version
sysbench: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory

解决办法:
在/etc/profile文件中加入一行:
export LD_LIBRARY_PATH=/usr/local/mysql/lib

source /etc/profile
命令可以正常使用
[root@test3 sysbench-master]# sysbench --version
sysbench 1.1.0

查看sysbench的一些帮助信息:

[root@test3 ~]# sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:
  --threads=N                     number of threads to use [1]  #线程的数量,默认是1
  --events=N                      limit for total number of events [0]  #限制的最大事件数量,默认是0,不限制
  --time=N                        limit for total execution time in seconds [10]  #整个测试执行的时间
  --warmup-time=N                 #在进行基准测试多少秒之后启用统计信息--forced-shutdown=STRING        #超过--time时间限制后,强制中断,默认是【off】
  --thread-stack-size=SIZE        size of stack per thread [64K]
  --thread-init-timeout=N         wait time in seconds for worker threads to initialize [30]
  --rate=N                        average transactions rate. 0 for unlimited rate [0]
  --report-interval=N             #打印出中间的信念,N表示每隔N秒打印一次,0表示禁用--report-checkpoints=[LIST,...] #转储完全统计信息并在指定时间点复位所有计数器,参数是逗号分隔值的列表,表示从必须执行报告检查点的测试开始所经过的时间(以秒为单位)。 默认情况下,报告检查点处于关闭状态[off]。--debug[=on|off]                print more debugging info [off]
  --validate[=on|off]             #在可能情况下执行验证检查,默认是[off]
  --help[=on|off]                 print help and exit [off]
  --version[=on|off]              print version and exit [off]
  --config-file=FILENAME          File containing command line options
  --luajit-cmd=STRING             perform LuaJIT control command. This option is equivalent to 'luajit -j'. See LuaJIT documentation for more information

#上面是一些通用的配置信息,在具体测试某个测试时,会再详细说明参数设置

首先来进行IO测试

[root@test3 ~]# sysbench fileio help               #查看IO测试的文档
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

fileio options:
  --file-num=N                  number of files to create [128]              #文件的数量
  --file-block-size=N           block size to use in all IO operations [16384] #文件块的大小,如果要是针对INNODB的测试,可以设置为innodb_page_size的大小
  --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【map映射】} [默认为:sync]          #文件的io模式
  --file-async-backlog=N        number of asynchronous operatons to queue per thread [128] #打开文件时的选项,这是与API相关的参数。
  --file-extra-flags=[LIST,...] #打开文件时的选项,这是与API相关的参数。可选有sync,dsync,direct。--file-fsync-freq=N           #执行fsync函数的频率,fsync主要是同步磁盘文件,因为可能有系统和磁盘缓冲的关系。默认为100,如果为0表示不使用fsync。
  --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-不合并),默认为[0]。
  --file-rw-ratio=N             #测试时的读写比例,默认是2:1。

在使用sysbench进行测试的时候,通常分为三个步骤prepare,run,cleanup阶段。

第一步准备数据(prepare阶段):

[root@test3 systext]# sysbench fileio --file-num=10 --file-total-size=50G prepare
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

10 files, 5242880Kb each, 51200Mb total
Creating files for the test...
Extra file open flags: (none)
Creating file test_file.0
Creating file test_file.1
Creating file test_file.2
Creating file test_file.3
Creating file test_file.4
Creating file test_file.5
Creating file test_file.6
Creating file test_file.7
Creating file test_file.8
Creating file test_file.9
53687091200 bytes written in 489.55 seconds (104.59 MiB/sec).

#这里给出一个每秒写入的数据量104.59MB/s, 这里的写入是顺序写入的,表示磁盘的吞吐量为104.59MB/s。
一般对顺序的读写称为吞吐量,对随机的IO使用IOPS来表示
[root@test3 systext]# ll -h #文件大小为5个G
total 50G
-rw------- 1 root root 5.0G Nov 27 09:30 test_file.0
-rw------- 1 root root 5.0G Nov 27 09:31 test_file.1
-rw------- 1 root root 5.0G Nov 27 09:32 test_file.2
-rw------- 1 root root 5.0G Nov 27 09:32 test_file.3
-rw------- 1 root root 5.0G Nov 27 09:33 test_file.4
-rw------- 1 root root 5.0G Nov 27 09:34 test_file.5
-rw------- 1 root root 5.0G Nov 27 09:35 test_file.6
-rw------- 1 root root 5.0G Nov 27 09:36 test_file.7
-rw------- 1 root root 5.0G Nov 27 09:36 test_file.8
-rw------- 1 root root 5.0G Nov 27 09:37 test_file.9

数据准备好之后,进行测试:

#这里进行随机读写测试
[root@test3 systext]# sysbench fileio --file-num=10 --file-total-size=50G --file-block-size=16384 --file-test-mode=rndrw --file-io-mode=sync --file-extra-flags=direct --time=100 --threads=16 --report-interval=10 run sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3) Running the test with following options: #设定的一些参数数值 Number of threads: 16 Report intermediate results every 10 second(s) Initializing random number generator from current time Extra file open flags: directio 10 files, 5GiB each 50GiB total file size Block size 16KiB Number of IO requests: 0 Read/Write ratio for combined random IO test: 1.50 Periodic FSYNC enabled, calling fsync() each 100 requests. Calling fsync() at the end of test, Enabled. Using synchronous I/O mode Doing random r/w test Initializing worker threads... Threads started! [ 10s ] reads: 3.24 MiB/s writes: 2.16 MiB/s fsyncs: 34.08/s latency (ms,95%): 80.025 #每隔10s输出一次报告 [ 20s ] reads: 3.49 MiB/s writes: 2.32 MiB/s fsyncs: 36.70/s latency (ms,95%): 73.135 [ 30s ] reads: 3.45 MiB/s writes: 2.29 MiB/s fsyncs: 37.00/s latency (ms,95%): 75.817 [ 40s ] reads: 3.43 MiB/s writes: 2.29 MiB/s fsyncs: 36.00/s latency (ms,95%): 75.817 [ 50s ] reads: 3.57 MiB/s writes: 2.38 MiB/s fsyncs: 37.40/s latency (ms,95%): 73.135 [ 60s ] reads: 3.08 MiB/s writes: 2.06 MiB/s fsyncs: 32.30/s latency (ms,95%): 86.002 [ 70s ] reads: 3.41 MiB/s writes: 2.27 MiB/s fsyncs: 36.40/s latency (ms,95%): 75.817 [ 80s ] reads: 3.47 MiB/s writes: 2.31 MiB/s fsyncs: 36.20/s latency (ms,95%): 73.135 [ 90s ] reads: 3.46 MiB/s writes: 2.31 MiB/s fsyncs: 36.20/s latency (ms,95%): 77.194 [ 100s ] reads: 3.10 MiB/s writes: 2.07 MiB/s fsyncs: 33.50/s latency (ms,95%): 75.817 Throughput: read: IOPS=215.57 3.37 MiB/s (3.53 MB/s) #通常的机械磁盘随机IOPS也就是200多一点。 write: IOPS=143.72 2.25 MiB/s (2.35 MB/s) #随机写入的速度明显要低很多。 fsync: IOPS=37.13 Latency (ms): min: 0.08 avg: 40.51 max: 1000.31 95th percentile: 77.19 sum: 1601329.71

#随机读大概是2.10M/s,文件块的大小为16KB,可以大概估计磁盘转速: 2.10*1024KB*60s/16KB=7560n/m, 大概就是7500转每分
[root@test3 systext]# sysbench fileio --file-num=10 --file-total-size=50G --file-block-size=16384 --file-test-mode=seqrd --file-io-mode=sync --file-extra-flags=direct --time=100  --threads=16 --report-interval=10 run
sysbench 1.1.0 (using bundled LuaJIT 2.1.0-beta3)

Running the test with following options:
Number of threads: 16
Report intermediate results every 10 second(s)
Initializing random number generator from current time


Extra file open flags: directio
10 files, 5GiB each
50GiB total file size
Block size 16KiB
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing sequential read test
Initializing worker threads...

Threads started!

[ 10s ] reads: 98.88 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.020
[ 20s ] reads: 98.64 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.681
[ 30s ] reads: 93.24 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 2.913
[ 40s ] reads: 89.12 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.028
[ 50s ] reads: 93.17 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.487
[ 60s ] reads: 91.98 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 4.652
[ 70s ] reads: 97.08 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.425
[ 80s ] reads: 93.71 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.020
[ 90s ] reads: 94.63 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.304
[ 100s ] reads: 89.57 MiB/s writes: 0.00 MiB/s fsyncs: 0.00/s latency (ms,95%): 3.364

Throughput:
         read:  IOPS=6016.01 94.00 MiB/s (98.57 MB/s)
         write: IOPS=0.00 0.00 MiB/s (0.00 MB/s)
         fsync: IOPS=0.00

Latency (ms):
         min:                                  0.40
         avg:                                  2.66
         max:                                687.00
         95th percentile:                      3.62
         sum:                            1599247.42

#测试结果可以看到顺序的读和随机读的差距还是超大的
顺序读的测试

相关文章: