sysbench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。其主要可以进行 cpu性能、磁盘io性能、调度程序性能、内存分配及传输速度、POSIX线程性能、数据库性能(OLTP基准测试) 几方面的测试。目前sysbench主要支持 MySQL,pgsql,oracle 这3种数据库的测试 。

一、sysbench的安装

sysbench项目之前是托管在sourceforge上的,不过最近发现通过原来的地址访问已经报404 。在github上找了该项目,地址为:https://github.com/dallasmarlow/sysbench 。由于我测试主机使用的ubuntu ,使用apt源的方式进行安装,如下:

<pre class="prettyprint linenums lang-bsh">#sudo apt-get install sysbench
<br />

安装完成后,可以使用sysbench –help或man sysbench查看使用帮助和详细参数介绍 。

二、CPU性能基准测试

测试CPU的性能,可以直接使用sysbench –test=cpu run最简单的方式进行测试,不过为了加压和性能区分,还可以使用以下参数:

  • –num-threads 线程数,默认值为1
  • –max-requests 最大请求数,默认值为10000
  • –cpu-max-prime 最大素数,使用该参数后,会使用64-bit int型进行性能测试计算
  • –percentile 取值百分比值,默认是95 ,即丢弃5%的长请求,在剩余的95%里取最大值
<br />

使用参数后的测试示例如下:

<pre class="prettyprint linenums lang-bsh">#指定线程数和最大请求数测试CPU
$ sysbench --num-threads=16  --max-requests=20000 --test=cpu  run
sysbench 0.4.12:  multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 16
Doing CPU performance benchmark
Threads started!
Done.
Maximum prime number checked in CPU test: 10000
Test execution summary:
    total time:                          10.3292s
    total number of events:              20000
    total time taken by event execution: 164.7408
    per-request statistics:
         min:                                  1.03ms
         avg:                                  8.24ms
         max:                                 56.75ms
         approx.  95 percentile:              29.04ms
Threads fairness:
    events (avg/stddev):           1250.0000/12.58
    execution time (avg/stddev):   10.2963/0.01
#利用线程数、最大请求数、最大素数测试CPU
$ sysbench --num-threads=16  --max-requests=20000 --test=cpu --cpu-max-prime=50000 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 16
Doing CPU performance benchmark
Threads started!
Done.
Maximum prime number checked in CPU test: 50000
Test execution summary:
    total time:                          94.0378s
    total number of events:              20000
    total time taken by event execution: 1503.9831
    per-request statistics:
         min:                                 35.65ms
         avg:                                 75.20ms
         max:                                129.40ms
         approx.  95 percentile:             105.41ms
Threads fairness:
    events (avg/stddev):           1250.0000/154.73
    execution time (avg/stddev):   93.9989/0.02
<br />

注:使用最大素数,是通过增加算法的复杂度,增加CPU的工作量,从而能更大程度的在不同的机器上比对CPU的性能 。

CPU基准测试报告:

线程数 最大请求数 计算最大素数 时间 请求时间统计
最小 最大 平均
16 20000 50000 10.3292s 1.03ms
32 50000 50000 25.8323s 1.03ms

可以多次变更以上几个参数的值做对比测试,并将测试数据可以按以下表格的方式进行比对 。

三、I/O基准测试

IO测试涉及到的参数如下:

<pre class="prettyprint linenums lang-bsh">$ sysbench --test=fileio run
sysbench 0.4.12:  multi-threaded system evaluation benchmark
FATAL: Missing required argument: --file-test-mode
  --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,fastmmap,slowmmap} [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]
<br />

其中测试模式里分为 连续/顺序写(seqwr)、连续改写(seqrewr)、连续读(seqrd)、随机读(rndrd)、随机写(rndwr)、随机读写(rndrw) 六种 。

以man手册里的测试示例为例,一次测试需要三步,如:

<pre class="prettyprint linenums lang-bsh">$ 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
<br />

其中第一步为创建128(默认个数)个测试文件;第二步为按指定的参数进行模式测试;第三步为清理第一步生成的文件 。如果直接执行第二步会出现如下报错:

<pre class="prettyprint linenums lang-bsh">FATAL: Failed to read file! file: 13 pos: 16531456 errno = 0 ()
FATAL: Failed to read file! file: 48 pos: 5537792 errno = 0 ()
FATAL: Failed to read file! file: 113 pos: 13156352 errno = 0 ()
FATAL: Failed to read file! file: 3 pos: 0 errno = 0 ()
FATAL: Failed to read file! file: 17 pos: 9060352 errno = 0 ()
FATAL: Failed to read file! file: 96 pos: 8503296 errno = 0 ()
FATAL: Failed to read file! file: 26 pos: 9568256 errno = 0 ()
FATAL: Failed to read file! file: 26 pos: 15794176 errno = 0 ()
FATAL: Failed to read file! file: 127 pos: 18300928 errno = 0 ()
FATAL: Failed to read file! file: 3 pos: 6340608 errno = 0 ()
FATAL: Failed to read file! file: 119 pos: 8421376 errno = 0 ()
FATAL: Failed to read file! file: 9 pos: 21331968 errno = 0 ()
FATAL: Failed to read file! file: 62 pos: 17448960 errno = 0 ()
FATAL: Failed to read file! file: 87 pos: 20398080 errno = 0 ()
FATAL: Failed to read file! file: 20 pos: 10649600 errno = 0 ()
FATAL: Failed to read file! file: 16 pos: 311296 errno = 0 ()
Done.
<br />

第一步操作后,生成的测试文件名如下:

<pre class="prettyprint linenums">yang@yang-acer:/tmp/sysbench$ ls
test_file.0    test_file.109  test_file.12   test_file.16  test_file.27  test_file.38  test_file.49  test_file.6   test_file.70  test_file.81  test_file.92
test_file.1    test_file.11   test_file.120  test_file.17  test_file.28  test_file.39  test_file.5   test_file.60  test_file.71  test_file.82  test_file.93
test_file.10   test_file.110  test_file.121  test_file.18  test_file.29  test_file.4   test_file.50  test_file.61  test_file.72  test_file.83  test_file.94
test_file.100  test_file.111  test_file.122  test_file.19  test_file.3   test_file.40  test_file.51  test_file.62  test_file.73  test_file.84  test_file.95
test_file.101  test_file.112  test_file.123  test_file.2   test_file.30  test_file.41  test_file.52  test_file.63  test_file.74  test_file.85  test_file.96
test_file.102  test_file.113  test_file.124  test_file.20  test_file.31  test_file.42  test_file.53  test_file.64  test_file.75  test_file.86  test_file.97
test_file.103  test_file.114  test_file.125  test_file.21  test_file.32  test_file.43  test_file.54  test_file.65  test_file.76  test_file.87  test_file.98
test_file.104  test_file.115  test_file.126  test_file.22  test_file.33  test_file.44  test_file.55  test_file.66  test_file.77  test_file.88  test_file.99
test_file.105  test_file.116  test_file.127  test_file.23  test_file.34  test_file.45  test_file.56  test_file.67  test_file.78  test_file.89
test_file.106  test_file.117  test_file.13   test_file.24  test_file.35  test_file.46  test_file.57  test_file.68  test_file.79  test_file.9
test_file.107  test_file.118  test_file.14   test_file.25  test_file.36  test_file.47  test_file.58  test_file.69  test_file.8   test_file.90
test_file.108  test_file.119  test_file.15   test_file.26  test_file.37  test_file.48  test_file.59  test_file.7   test_file.80  test_file.91
<br />

执行第二步操作测试的结果为:

<pre class="prettyprint linenums lang-bsh">$ sysbench --num-threads=16 --test=fileio --file-total-size=3G --file-test-mode=rndrw run
sysbench 0.4.12:  multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 16
Extra file open flags: 0
128 files, 24Mb each
3Gb total file size
Block size 16Kb
Number of random requests for random IO: 10000
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
Threads started!
Done.
Operations performed:  6000 Read, 4002 Write, 12800 Other = 22802 Total
Read 93.75Mb  Written 62.531Mb  Total transferred 156.28Mb  (2.8256Mb/sec)
  180.84 Requests/sec executed
Test execution summary:
    total time:                          55.3085s
    total number of events:              10002
    total time taken by event execution: 306.1095
    per-request statistics:
         min:                                  0.00ms
         avg:                                 30.60ms
         max:                                508.92ms
         approx.  95 percentile:             162.07ms
Threads fairness:
    events (avg/stddev):           625.1250/66.54
    execution time (avg/stddev):   19.1318/1.07
<br />

注:实际测试中最好几种模式的都逐一进行测试比对 。

测试基准报告:

线程数 测试模式 最大请求数 文件大小 传输速度 总执行时间
最小 最大 平均
16 rndrw 20000 10G 2.8256Mb/sec 200.3292s 0.00ms
16 rndwr 20000 10G 2.2149Mb/sec 352.7680s 0.00ms
16 seqwr 20000 10G 2.3958Mb/sec 200.3293s 0.00ms
16 seqrd 20000 10G 2.1065Mb/sec 125.8323s 0.00ms

备注:IO测试中,最好让线程、最大请求数、文件大小三个值的固定为合适的值,通过改变不同的测试模式进行性能比对 。

四、内存基准测试

内存测试相对也是较简单的,可以直接使用 sysbench –test=memory run 进行内存测试 。虽然在test选项里有memory项,但在sysbench的man手册里,并未将memory的参数单独列出,其参数解释部分作为mutex的一部分给出了。具体可选参数主要有如下部分:

<pre class="prettyprint linenums lang-bsh">--mutex-num
  Number of mutexes. The actual mutex to lock is chosen randomly before each lock (default: 4096)
--memory-scope
  Possible values: global, local. Specifies whether each thread will use a globally allocated memory block, or a local one. (default: global)
--memory-total-size
  Total size of data to transfer (default: 100G)
--memory-oper
  Type of memory operations. Possible values: read, write
<br />

一般在测试中主要做内存的读写性能的对比测试,结果类似如下:

<pre class="prettyprint linenums lang-bsh">$ sysbench --test=memory --memory-total-size=200G --memory-oper=read run
sysbench 0.4.12:  multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 1
Doing memory operations speed test
Memory block size: 1K
Memory transfer size: 204800M
Memory operations type: read
Memory scope type: global
Threads started!
Done.
Operations performed: 209715200 (5331397.74 ops/sec)
204800.00 MB transferred (5206.44 MB/sec)
Test execution summary:
    total time:                          39.3359s
    total number of events:              209715200
    total time taken by event execution: 28.1049
    per-request statistics:
         min:                                  0.00ms
         avg:                                  0.00ms
         max:                                  0.07ms
         approx.  95 percentile:               0.00ms
Threads fairness:
    events (avg/stddev):           209715200.0000/0.00
    execution time (avg/stddev):   28.1049/0.00
<br />

基准测试报告:

线程数 测试模式 最大请求数 总测试数据 传输性能 传输速度 总执行时间 请求统计
最小 最大 平均
16 read 20000 200G 5331397.74 ops/sec 5206.44 MB/sec 39.3359s 0.00ms
16 read 20000 500G 5331397.74 ops/sec 5206.44 MB/sec 352.7680s 0.00ms
16 write 20000 200G 5331397.74 ops/sec 5206.44 MB/sec 200.3293s 0.00ms
16 write 20000 500G 5331397.74 ops/sec 5206.44 MB/sec 125.8323s 0.00ms

上面除了第一条数据是我测试的结果,后面的数据都是伪造的 。

备注:这里最大值可能会为0,出现该问题的原因是由于–percentile默认值是95,会将5%的最大值drop掉。而内存里读取速度较快,剔除掉5%的最大值后,就有可能为0 。

五、其他

threads、mutex基本上和上提到的类似 ,这里不再特别说明。其中参数最多的是oltp基准测试 ,其主要用于进行数据库性能测试 ,测试示例如下:

<pre class="prettyprint linenums lang-bsh">$ sysbench --test=oltp --mysql-table-type=myisam --oltp-table-size=1000000 --mysql-socket=/tmp/mysql.sock prepare
$ sysbench --num-threads=16 --max-requests=100000 --test=oltp --oltp-table-size=1000000 --mysql-socket=/tmp/mysql.sock --oltp-read-only run
<br />

也可以使用–mysql-host、–mysql-port、–mysql-password参数测试其他主机上的数据库 。

参考文档:

http://www.jhaagmans.com/aws/ec2-performance-and-cost-vs-dedicated-servers-and-in-house-solutions.html

http://wenku.baidu.com/view/04298f8dbceb19e8b9f6ba0e.html

备注:为了测试方便,以上内容本人已脚本化并上传到github上https://github.com/361way/sysbench-shell