一、 安裝相關的依賴包
yum install *libMySQLclient*
yum install libtool
二、 安裝及簡介
1)下載安裝包
# wget http://sourceforge.net/projects/sysbench/sysbench-0.4.12.tar.gz
2) 編譯安裝
# tar –zxvf sysbench-0.4.12.tar.gz
進入解壓縮包sysbench-0.4.12,并執行腳本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
3)yum安裝
如果覺得源碼安裝麻煩,也可以采用yum安裝,操作如下:
# yum install -y sysbench
4)簡介
sysbench 可以進行以下測試:
CPU 運算性能測試
磁盤 IO 性能測試
調度程序性能測試
內存分配及傳輸速度測試
POSIX 線程性能測試
數據庫性能測試(OLTP 基準測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 腳本執行,例如 oltp_read_only.lua 腳本執行只讀測試)
另外,sysbench 還可以通過運行命令時指定自己的 Lua 腳本來自定義測試。
三、 使用
3.1 查看幫助信息
sysbench --help
可以通過 sysbench --help 查看 sysbench 的可用命令、選項及內建測試類型的幫助信息,不同測試類型可用的命令也不同:
[root@VM_157_18_centos ~]# sysbench --help
Usage:
sysbench [options]... [testname] [command]
Commands implemented by most tests: prepare run cleanup help # 可用的命令,四個
General options: # 通用選項
--threads=N 要使用的線程數,默認 1 個 [1]
--events=N 最大允許的事件個數 [0]
--time=N 最大的總執行時間,以秒為單位 [10]
--forced-shutdown=STRING 在 --time 時間限制到達后,強制關閉之前等待的秒數,默認“off”禁用(number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable) [off]
--thread-stack-size=SIZE 每個線程的堆棧大小 [64K]
--rate=N 平均傳輸速率。0 則無限制 [0]
--report-interval=N 以秒為單位定期報告具有指定間隔的中間統計信息 0 禁用中間報告 [0]
--report-checkpoints=[LIST,...] 轉儲完整的統計信息并在指定的時間點重置所有計數器。參數是一個逗號分隔的值列表,表示從測試開始經過這個時間量時必須執行報告檢查點(以秒為單位)。報告檢查點默認關閉。 []
--debug[=on|off] 打印更多 debug 信息 [off]
--validate[=on|off] 盡可能執行驗證檢查 [off]
--help[=on|off] 顯示幫助信息并退出 [off]
--version[=on|off] 顯示版本信息并退出 [off]
--config-file=FILENAME 包含命令行選項的文件
--tx-rate=N 廢棄,改用 --rate [0]
--max-requests=N 廢棄,改用 --events [0]
--max-time=N 廢棄,改用 --time [0]
--num-threads=N 廢棄,改用 --threads [1]
Pseudo-Random Numbers Generator options: # 偽隨機數發生器選項
--rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
--rand-spec-iter=N number of iterations used for numbers generation [12]
--rand-spec-pct=N percentage of values to be treated as 'special' (for special distribution) [1]
--rand-spec-res=N percentage of 'special' values to use (for special distribution) [75]
--rand-seed=N seed for random number generator. When 0, the current time is used as a RNG seed. [0]
--rand-pareto-h=N parameter h for pareto distribution [0.2]
Log options: # 日志選項
--verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]
--percentile=N percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
--histogram[=on|off] print latency histogram in report [off]
General database options: # 通用的數據庫選項
--db-driver=STRING 指定要使用的數據庫驅動程序 ('help' to get list of available drivers)
--db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
--db-debug[=on|off] print database-specific debug information [off]
Compiled-in database drivers: # 內建的數據庫驅動程序,默認支持 MySQL 和
PostgreSQL
mysql - MySQL driver
pgsql - PostgreSQL driver
mysql options: # MySQL 數據庫專用選項
--mysql-host=[LIST,...] MySQL server host [localhost]
--mysql-port=[LIST,...] MySQL server port [3306]
--mysql-socket=[LIST,...] MySQL socket
--mysql-user=STRING MySQL user [sbtest]
--mysql-password=STRING MySQL password []
--mysql-db=STRING MySQL database name [sbtest]
--mysql-ssl[=on|off] use SSL connections, if available in the client library [off]
--mysql-ssl-cipher=STRING use specific cipher for SSL connections []
--mysql-compression[=on|off] use compression, if available in the client library [off]
--mysql-debug[=on|off] trace all client library calls [off]
--mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
--mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off]
pgsql options: # PostgreSQL 數據庫專用選項
--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]
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
See 'sysbench <testname> help' for a list of options for each test.
sysbench <testname> help
通過 sysbench <testname> help 命令可以查看具體一個測試類型的幫助信息。
# sysbench fileio --help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
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-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]
3.2 測試 CPU
CPU 測試時,會計算素數(對這個數字除以 2 到這個數字平方根之間的所有數字來驗證素數)直到某個指定值所需要的時間。單純的測試一組非常有限 CPU 硬件性能。
CPU 基準測試時可以指定線程數量和素數上限。
先查看幫助信息:
[root@VM_157_18_centos ~]# sysbench cpu help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
cpu options:
--cpu-max-prime=N 素數發生器的上限(upper limit for primes generator) [10000]
開始測試:
[root@VM_157_18_centos ~]# sysbench --cpu-max-prime=10000 --threads=2 cpu run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 2
Initializing random number generator from current time
Prime numbers limit: 10000
Initializing worker threads...
Threads started!
CPU speed:
events per second: 727.23
General statistics:
total time: 10.0020s
total number of events: 7275
Latency (ms):
min: 1.34
avg: 2.75
max: 39.37
95th percentile: 12.30
sum: 19982.10
Threads fairness:
events (avg/stddev): 3637.5000/1.50
execution time (avg/stddev): 9.9911/0.00
3.3 測試 fileio
使用 fileio 時,需要創建一組測試文件,測試文件需要大于可用內存的大小,避免文件緩存在內存中影響結果。測試流程為:準備測試文件-》測試-》回收測試文件,命令如下:
# sysbench --file-total-size=28G --test=fileio prepare
# sysbench --file-total-size=28G --file-test-mode=rndrw --time=300 --max-requests=0 --test=fileio run
# sysbench --file-total-size=28G fileio cleanup
對于 I/O 基準測試,可以通過 --file-test-mode 告訴 sysbench 要運行的工作負載的類型,可用類型有:
seqwr:順序寫入
seqrewr:順序重寫
seqrd:順序讀取
rndrd:隨機讀取
rndwr:隨機寫入
rndrw:隨機讀取/寫入
上面的例子中使用的是隨機讀取/寫入(rndrw)。通過 --time 選項指定測試持續時間(以秒為單位)。
幫助信息:
# sysbench fileio help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
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-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]
命令的完整輸出如下:
[root@VM_157_18_centos ~]# sysbench --file-total-size=28G fileio prepare
sysbench 1.0.9 (using system LuaJIT 2.0.4)
128 files, 229376Kb each, 28672Mb total
Creating files for the test...
Extra file open flags: 0
Creating file test_file.0
Creating file test_file.1
...此處總共創建了 128 個文件,每個 229376Kb,總共 28672Mb
Creating file test_file.127
30064771072 bytes written in 255.52 seconds (112.21 MiB/sec).
[root@VM_157_18_centos ~]# sysbench --file-total-size=28G --file-test-mode=rndrw --time=300 --max-requests=0 fileio run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Extra file open flags: 0
128 files, 224MiB each
28GiB 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!
File operations:
reads/s: 96.00
writes/s: 64.00
fsyncs/s: 204.47
Throughput:
read, MiB/s: 1.50
written, MiB/s: 1.00
General statistics:
total time: 300.0016s
total number of events: 109341
Latency (ms):
min: 0.00
avg: 2.74
max: 256.76
95th percentile: 7.17
sum: 299726.28
Threads fairness:
events (avg/stddev): 109341.0000/0.00
execution time (avg/stddev): 299.7263/0.00
[root@VM_157_18_centos ~]# sysbench --file-total-size=28G fileio cleanup
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Removing test files...
2.4 測試 memory
當在 sysbench 中進行內存測試時,會分配一個內存緩沖區并在此執行讀寫操作,每次操作都會讀完或寫滿緩沖區。然后重復此操作直到達到指定大小(--memory-total-size)。可以提供多個線程(--threads),不同的緩沖區大小(--memory-block-size)和請求類型(讀或寫,順序或隨機)。
幫助信息:
# sysbench memory help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
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]
例如,指定 4 個線程,緩沖區大小為 8KB,在內存中傳輸 4GB 數據的測試:
[root@VM_157_18_centos ~]# sysbench --threads=4 --memory-block-size=8k --memory-total-size=4G memory run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 4
Initializing random number generator from current time
Running memory speed test with the following options:
block size: 8KiB
total size: 4096MiB
operation: write
scope: global
Initializing worker threads...
Threads started!
Total operations: 524288 (731249.24 per second)
4096.00 MiB transferred (5712.88 MiB/sec)
General statistics:
total time: 0.7150s
total number of events: 524288
Latency (ms):
min: 0.00
avg: 0.00
max: 43.03
95th percentile: 0.00
sum: 1871.33
Threads fairness:
events (avg/stddev): 131072.0000/0.00
execution time (avg/stddev): 0.4678/0.04
2.5 測試 threads
測試 threads 時,每個工作線程將被分配一個 mutex(一種鎖)。每次執行時,每個線程將循環若干次(通過 --thread-yields 的數量設置),循環時這個線程會鎖定,在再次執行時解鎖。
通過調整各種參數,可以模擬具有單個或多個鎖的高并發線程下的情況。
幫助信息:
# sysbench threads help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
threads options:
--thread-yields=N number of yields to do per request [1000]
--thread-locks=N number of locks per thread [8]
運行測試:
[root@VM_157_18_centos ~]# sysbench --thread-yields=2000 --thread-locks=8 threads run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Initializing worker threads...
Threads started!
General statistics:
total time: 10.0002s
total number of events: 16554
Latency (ms):
min: 0.50
avg: 0.60
max: 37.05
95th percentile: 0.94
sum: 9978.12
Threads fairness:
events (avg/stddev): 16554.0000/0.00
execution time (avg/stddev): 9.9781/0.00
2.6 測試 mutex
測試 mutex 時,sysbench 將為每個線程運行一個請求。這個請求首先會對 CPU 施加一些壓力(使用一個簡單的增量循環,通過 --mutex-loops 參數設置),然后隨機使用一個 mutex(鎖),遞增一個全局變量并再次釋放鎖。這個過程根據鎖的個數(--mutex-locks)多次重復。隨機 mutex 取自大小為 --mutex-num 的參數池。
幫助信息:
# sysbench mutex help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
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 outside mutex lock [10000]
運行測試:
[root@VM_157_18_centos ~]# sysbench mutex run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Initializing worker threads...
Threads started!
General statistics:
total time: 0.2132s
total number of events: 1
Latency (ms):
min: 213.10
avg: 213.10
max: 213.10
95th percentile: 211.60
sum: 213.10
Threads fairness:
events (avg/stddev): 1.0000/0.00
execution time (avg/stddev): 0.2131/0.00
2.7 OLTP 基準測試
OLTP 基準測試模擬了一個簡單的事務處理系統的工作負載。然而最新版本的 sysbench 把 OLTP 這個曾經的內建測試類型移除了,如果要用,需要在命令中指定測試類型的位置用 /usr/share/sysbench/oltp_read_only.lua 腳本代替測試類型。
流程:指定數據庫-》建表并生成數據-》運行測試-》清理測試表。
首先生成表,注意替換命令中的數據庫密碼和 MySQL 套接字,如果 MySQL 安裝在默認位置,可以去掉 --mysql-socket 選項:
sysbench --db-driver=mysql --mysql-user=root --mysql-password=<pwd>
--mysql-socket=<mysql.sock path> --mysql-db=foo --range_size=100
--table_size=10000 --tables=2 --threads=2 --events=0 --time=60
--rand-type=uniform /usr/share/sysbench/oltp_read_only.lua prepare
運行測試,指定了 2 個并發線程,:
sysbench --db-driver=mysql --mysql-user=root --mysql-password=<pwd>
--mysql-socket=<mysql.sock path> --mysql-db=foo --range_size=100
--table_size=10000 --tables=2 --threads=2 --events=0 --time=60
--rand-type=uniform /usr/share/sysbench/oltp_read_only.lua run
清理測試時生成的測試表:
sysbench --db-driver=mysql --mysql-user=root --mysql-password=<pwd>
--mysql-socket=<mysql.sock path> --mysql-db=foo --range_size=100
--table_size=10000 --tables=2 --threads=2 --events=0 --time=60
--rand-type=uniform /usr/share/sysbench/oltp_read_only.lua cleanup
例如我的選項就是:
[root@VM_157_18_centos ~]# sysbench --db-driver=mysql --mysql-user=root --mysql-password=1qaz@WSX --mysql-db=foo --range_size=100 --table_size=10000 --tables=2 --threads=1 --events=0 --time=60 --rand-type=uniform /usr/share/sysbench/oltp_read_only.lua prepare
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Creating table 'sbtest1'...
Inserting 10000 records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest2'...
Inserting 10000 records into 'sbtest2'
Creating a secondary index on 'sbtest2'...
[root@VM_157_18_centos ~]# sysbench --db-driver=mysql --mysql-user=root --mysql-password=1qaz@WSX --mysql-db=foo --range_size=100 --table_size=10000 --tables=2 --threads=1 --events=0 --time=60 --rand-type=uniform /usr/share/sysbench/oltp_read_only.lua run
sysbench 1.0.9 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 1
Initializing random number generator from current time
Initializing worker threads...
Threads started!
SQL statistics:
queries performed:
read: 645708
write: 0
other: 92244
total: 737952
transactions: 46122 (768.67 per sec.)
queries: 737952 (12298.73 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 60.0003s
total number of events: 46122
Latency (ms):
min: 1.06
avg: 1.30
max: 34.08
95th percentile: 1.50
sum: 59896.08
Threads fairness:
events (avg/stddev): 46122.0000/0
[root@VM_157_18_centos ~]# sysbench --db-driver=mysql --mysql-user=root --mysql-password=1qaz@WSX --mysql-db=foo --range_size=100 --table_size=10000 --tables=2 --threads=1 --events=0 --time=60 --rand-type=uniform /usr/share/sysbench/oltp_read_only.lua cleanup
sysbench 1.0.9 (using system LuaJIT 2.0.4)
sysbench --file-total-size=10G --file-test-mode=rndrw --max-time=180 --max-requests=0 --seqrewr=fileio run