一、sysbench壓力測試工具簡介:
sysbench是一個開源的、模塊化的、跨平台的多線程性能測試工具,可以用來進行CPU、內存、磁盤I/O、線程、數據庫的性能測試。目前支持的數據庫有MySQL、Oracle和PostgreSQL。以下操作都將以支持MySQL數據庫為例進行。
1. sysbench工具安裝:
默認支持<span "="">MySQL,如果需要測試<span "="">Oracle/PostgreSQL,則在<span "="">configure時需要加上<span "="">–with-oracle或者<span "="">–with-pgsql參數<span "="">.
<span "="">
1.1 安裝環境
<span "="">CentOS release 6.3 (Final)
<span "="">MySQL 5.6.13
<span "="">MySQL_HOME=/usr/local/mysql/
<span "="">Sysbench 0.4.12
1.2 下載安裝包
# wget http://sourceforge.net/projects/sysbench/sysbench-0.4.12.tar.gz
1.3 編譯安裝
# tar –zxvf sysbench-0.4.12.tar.gz
進入解壓縮包<span "="">sysbench-0.4.12,並執行腳本<span "="">autogen.sh
# cd sysbench-0.4.12
# ./autogen.sh
關鍵的三步:configure && make && make install
首先是./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/mysql/ \
--with-mysql-libs=/usr/local/mysql/lib/mysql/
注意:這里在編譯時要將路徑寫到最后的include/mysql及lib/mysql,如下所示:
–with-mysql-includes=/usr/local/mysql/include/mysql/
--with-mysql-libs=/usr/local/mysql/lib/mysql/
因為網上好多資料都沒有提到這一層,在編譯時總是編譯不過去,這里浪費了好多精力。
接下來執行如下命令:
# make && make install
1.4 yum安裝
如果覺得源碼安裝麻煩,也可以采用yum安裝,操作如下:
# yum install -y sysbench
2. sysbench支持的功能參數
首先,看看sysbench都支持哪些功能參數:
[root@db-master sysbench]# sysbench --help
Usage:
sysbench [general-options]... --test= [test-options]... command
General options:
--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]
--forced-shutdown=STRING amount of time to wait after --max-time before forcing shutdown [off]
--thread-stack-size=SIZE size of stack per thread [32K]
--init-rng=[on|off] initialize random number generator [off]
--test=STRING test to run
--debug=[on|off] print more debugging info [off]
--validate=[on|off] perform validation checks where possible [off]
--help=[on|off] print help and exit
--version=[on|off] print version and exit
Compiled-in tests:
fileio - File I/O test
cpu - CPU performance test
memory - Memory functions speed test
threads - Threads subsystem performance test
mutex - Mutex performance test
oltp - OLTP test
Commands: prepare run cleanup help version
See 'sysbench --test= help' for a list of options for each test.
Sysbench的測試主要包括以下幾個方面:
1、磁盤io性能
2、cpu性能
3、內存分配及傳輸速度
4、POSIX線程性能
5、調度程序性能
6、數據庫性能(OLTP基准測試).
2.1 測試fileio命令幫助:
[root@db-master ~]# sysbench --test=fileio help
sysbench 0.4.12: 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,fastmmap,slowmmap} [sync]
--file-async-backlog=N number of asynchronous operatons to queue per thread [128]
--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]
參數詳解:
--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.2 測試cpu命令幫助:
[root@db-master ~]# 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]
參數詳解:
--cpu-max-prime=N 用來選項指定最大的素數,具體參數可以根據CPU的性能來設置,默認為10000
2.3 測試memory命令幫助:
[root@db-master ~]# sysbench --test=memory help
sysbench 0.4.12: multi-threaded system evaluation benchmark
memory options:
--memory-block-size=SIZE size of memory block for test [1K]
--memory-total-size=SIZE total size of data to transfer [100G]
--memory-scope=STRING memory access scope {global,local} [global]
--memory-hugetlb=[on|off] allocate memory from HugeTLB pool [off]
--memory-oper=STRING type of memory operations {read, write, none} [write]
--memory-access-mode=STRING memory access mode {seq,rnd} [seq]
參數詳解:
--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.4 測試threads命令幫助:
[root@db-master ~]# sysbench --test=threads help
sysbench 0.4.12: multi-threaded system evaluation benchmark
threads options:
--thread-yields=N number of yields to do per request [1000]
--thread-locks=N number of locks per thread [8]
參數詳解:
--thread-yields=N 指定每個請求的壓力,默認為1000
--thread-locks=N 指定每個線程的鎖數量,默認為8
2.5 測試mutex命令幫助:
[root@db-master ~]# sysbench --test=mutex help
sysbench 0.4.12: multi-threaded system evaluation benchmark
mutex options:
--mutex-num=N total size of mutex array [4096]
--mutex-locks=N number of mutex locks to do per thread [50000]
--mutex-loops=N number of empty loops to do inside mutex lock [10000]
參數詳解:
--mutex-num=N 數組互斥的總大小。默認是4096
--mutex-locks=N 每個線程互斥鎖的數量。默認是50000
--mutex-loops=N 內部互斥鎖的空循環數量。默認是10000
2.6 測試oltp命令幫助:
[root@db-master ~]# sysbench --test=oltp help
sysbench 0.4.12: multi-threaded system evaluation benchmark
oltp options:
--oltp-test-mode=STRING test type to use {simple,complex,nontrx,sp} [complex]
--oltp-reconnect-mode=STRING reconnect mode {session,transaction,query,random} [session]
--oltp-sp-name=STRING name of store procedure to call in SP test mode []
--oltp-read-only=[on|off] generate only 'read' queries (do not modify database) [off]
--oltp-skip-trx=[on|off] skip BEGIN/COMMIT statements [off]
--oltp-range-size=N range size for range queries [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-ranges=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 mode for non-transactional test {select, update_key, update_nokey, insert, delete} [select]
--oltp-auto-inc=[on|off] whether AUTO_INCREMENT (or equivalent) should be used on id column [on]
--oltp-connect-delay=N time in microseconds to sleep after connection to database [10000]
--oltp-user-delay-min=N minimum time in microseconds to sleep after each request [0]
--oltp-user-delay-max=N maximum time in microseconds to sleep after each request [0]
--oltp-table-name=STRING name of test table [sbtest]
--oltp-table-size=N number of records in test table [10000]
--oltp-dist-type=STRING random numbers distribution {uniform,gaussian,special} [special]
--oltp-dist-iter=N number of iterations used for numbers generation [12]
--oltp-dist-pct=N percentage of values to be treated as 'special' (for special distribution) [1]
--oltp-dist-res=N percentage of 'special' values to use (for special distribution) [75]
General database options:
--db-driver=STRING specifies database driver to use ('help' to get list of available drivers)
--db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
Compiled-in database drivers:
mysql - MySQL driver
pgsql - PostgreSQL driver
mysql options:
--mysql-host=[LIST,...] MySQL server host [localhost]
--mysql-port=N MySQL server port [3306]
--mysql-socket=STRING MySQL socket
--mysql-user=STRING MySQL user [sbtest]
--mysql-password=STRING MySQL password []
--mysql-db=STRING MySQL database name [sbtest]
--mysql-table-engine=STRING storage engine to use for the test table {myisam,innodb,bdb,heap,ndbcluster,federated} [innodb]
--mysql-engine-trx=STRING whether storage engine used is transactional or not {yes,no,auto} [auto]
--mysql-ssl=[on|off] use SSL connections, if available in the client library [off]
--myisam-max-rows=N max-rows parameter for MyISAM tables [1000000]
--mysql-create-options=STRING additional options passed to CREATE TABLE []
pgsql options:
--pgsql-host=STRING PostgreSQL server host [localhost]
--pgsql-port=N PostgreSQL server port [5432]
--pgsql-user=STRING PostgreSQL user [sbtest]
--pgsql-password=STRING PostgreSQL password []
--pgsql-db=STRING PostgreSQL database name [sbtest]
參數詳解:
--oltp-test-mode=STRING 執行模式{simple,complex(advanced transactional),nontrx(non-transactional),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-ranges=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
3. 總結:
以上就是sysbench工具的安裝配置及相關選項參數的介紹,可以根據自己的需要添加對應的參數進行壓力測試即可。在下一篇文檔中將會對上面的幾種模式進行相應的測試。
2.1 測試數據庫服務器的硬件配置信息如下:
CPU: 24核心線程數,Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz
MEM: 64G,8*8G=64G
DISK: 15000轉/秒
2.2 對磁盤IO性能的測試:
2.2.1 創建fileio文件
創建初始化fileio文件:
[root@db-master sysbench]# sysbench --test=fileio --file-num=16 --file-total-size=2G prepare
sysbench 0.4.12: multi-threaded system evaluation benchmark
16 files, 131072Kb each, 2048Mb total
Creating files for the test...
2.2.1 開始fileio測試
接下來開始對這些文件進行測試,使用16個線程隨機讀進行測試結果如下:
[root@db-master sysbench]# sysbench --test=fileio --file-total-size=2G --file-test-mode=rndrd --max-time=180 --max-requests=100000000 --num-threads=16 --init-rng=on --file-num=16 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=16384 run
可以看到隨機讀取的性能為75.988Mb/sec,隨機讀的IOPS為4863.25 Requests/sec.說明服務器的硬件配置還不錯。
2.2.3 測試完成執行cleanup
測試結束后,記得執行cleanup,以確保測試所產生的文件都已刪除:
[root@db-master sysbench]# sysbench --test=fileio --file-num=16 --file-total-size=2G cleanup
sysbench 0.4.12: multi-threaded system evaluation benchmark
Removing test files...
如果需要測試seqwr(順序寫), seqrewr(順序讀寫), seqrd(順序讀), rndrd(隨機讀), rndwr(隨機寫), rndrw(隨機讀寫)等6種模式,並且還可能需要測試不同的線程和不同的文件塊下磁盤的性能表現,這時,可以使用如下腳本達到測試目的。
測試腳本內容如下:
#!/bin/bash
#==============================================================================
#
# FILE: sysbench_auto.sh
#
# USAGE: ./sysbench_auto.sh
#
# DESCRIPTION: This file is sysbench_auto.sh
# AUTHOR: Kevin Lu (kevin), kevin@gmail.com
# ORGANIZATION: cmcc
# CREATED: 02/26/2014 17:35
# REVISION: v1.0.1
#==============================================================================
for size in {8G,64G}
do
for mode in {seqwr,seqrewr,seqrd,rndrd,rndwr,rndrw}
do
for blksize in {4096,16384}
do
sysbench --test=fileio --file-num=64 --file-total-size=$size prepare
for threads in {1,4,8,16,32}
do
echo "=============testing $blksize in $threads threads"
echo PARAS $size $mode $threads $blksize > sysbench-size-$size-mode-$mode-threads-$threads-blksz-$blksize
for i in {1,2,3}
do
sysbench --test=fileio --file-total-size=$size --file-test-mode=$mode --max-time=180 --max-requests=100000 --num-threads=$threads --
init-rng=on --file-num=64 --file-extra-flags=direct --file-fsync-freq=0 --file-block-size=$blksize run|tee -a sysbench-size-$size-mo
de-$mode-threads-$threads-blksz-$blksize 2>&1
done
done
sysbench --test=fileio --file-total-size=$size cleanup
done
done
done
-- 腳本運行后,在當前目錄下會生成如下文件:
3.1 對mysql事務型OLTP的測試:
3.1.1 prepare准備階段
對於mysql的OLTP測試,和file一樣,同樣需要經歷prepare,run,cleanup三個階段。prepare階段會在數據庫中產生一張指定行數的表,默認表在sbtest架構下,表名為
sbtest(sysbench默認生成表的存儲引擎為innodb),如創建一張8000萬條記錄的表:
[root@db-master sysbench]# sysbench --test=oltp --oltp-table-size=80000000 --db-driver=mysql --mysql-socket=/data/mysqlsoft3307/mysql.sock --mysql-user=dba_manager --mysql-password='111111' --mysql-db=test prepare
sysbench 0.4.12: multi-threaded system evaluation benchmark
Creating table 'sbtest'...
Creating 10 records in table 'sbtest'...
3.1.2 run運行測試
接下來對上面產生的表進行oltp的測試:
[root@db-master sysbench]# sysbench --test=oltp --oltp-table-size=80000000 --oltp-read-only=off --init-rng=on --num-threads=16 --max-requests=0 --oltp-dist-type=uniform --max-time=3600 --mysql-user=dba_manager --mysql-password='111111' --db-driver=mysql --mysql-socket=/data/mysqlsoft3307/mysql.sock run > result.log
參數說明:
--max-time=3600 指定測試時長為1小時
--mysql-db=test 指定測試的數據庫名
[root@db-master sysbench]# cat result.log
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.
Doing OLTP test.
Running mixed OLTP test
Using Uniform distribution
Using "BEGIN" for starting transactions
Using auto_inc on the id column
Threads started!
Time limit exceeded, exiting...
(last message repeated 15 times)
Done.
OLTP test statistics:
queries performed:
read: 137346874
write: 49052449
other: 19620980
total: 206020303
transactions: 9810489 (2725.13 per sec.)
deadlocks: 2 (0.00 per sec.)
read/write requests: 186399323 (51777.50 per sec.)
other operations: 19620980 (5450.26 per sec.)
Test execution summary:
total time: 3600.0060s
total number of events: 9810489
total time taken by event execution: 57542.1464
per-request statistics:
min: 3.00ms
avg: 5.87ms
max: 212.09ms
approx. 95 percentile: 8.44ms
Threads fairness:
events (avg/stddev): 613155.5625/1486.47
execution time (avg/stddev): 3596.3842/0.01
以上測試結果顯示了很多操作的詳細信息,transactions代表測試結果的評判標准即TPS,上述測試結果是 2725.13 per sec.
可以對數據庫進行調優后,再使用sysbench對OLTP進行測試,看看TPS是不是會有所提高。
注意:sysbench的測試只是基准測試,並不能代表實際企業環境下的性能指標。