一、 安裝相關(guān)的依賴包
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
進(jìn)入解壓縮包sysbench-0.4.12,并執(zhí)行腳本autogen.sh
# cd sysbench-0.4.12
# ./autogen.sh
關(guān)鍵的三步:configure && make && make install
首先是./configure命令,sysbench默認(rèn)是支持MySQL的benchmarking的,如果不加任何選項(xiàng)則要求保證MySQL的安裝路徑都是默認(rèn)的標(biāo)準(zhǔn)路徑,headfile位于/usr/include目錄下,libraries位于/usr/lib/目錄下。因?yàn)槲业腗ySQL是源碼編譯安裝的,安裝路徑是放在/usr/local/mysql下,所以這里要添加相應(yīng)的選項(xiàng)命令:
# ./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/
注意:這里在編譯時(shí)要將路徑寫到最后的include/mysql及l(fā)ib/mysql,如下所示:
–with-mysql-includes=/usr/local/mysql/include/mysql/
--with-mysql-libs=/usr/local/mysql/lib/mysql/
因?yàn)榫W(wǎng)上好多資料都沒有提到這一層,在編譯時(shí)總是編譯不過去,這里浪費(fèi)了好多精力。
接下來執(zhí)行如下命令:
# make && make install
3)yum安裝
如果覺得源碼安裝麻煩,也可以采用yum安裝,操作如下:
# yum install -y sysbench
4)簡介
sysbench 可以進(jìn)行以下測試:
CPU 運(yùn)算性能測試
磁盤 IO 性能測試
調(diào)度程序性能測試
內(nèi)存分配及傳輸速度測試
POSIX 線程性能測試
數(shù)據(jù)庫性能測試(OLTP 基準(zhǔn)測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 腳本執(zhí)行,例如 oltp_read_only.lua 腳本執(zhí)行只讀測試)
另外,sysbench 還可以通過運(yùn)行命令時(shí)指定自己的 Lua 腳本來自定義測試。
三、 使用
3.1 查看幫助信息
sysbench --help
可以通過 sysbench --help 查看 sysbench 的可用命令、選項(xiàng)及內(nèi)建測試類型的幫助信息,不同測試類型可用的命令也不同:
[root@VM_157_18_centos ~]# sysbench --help
Usage:
sysbench [options]... [testname] [command]
Commands implemented by most tests: prepare run cleanup help # 可用的命令,四個(gè)
General options: # 通用選項(xiàng)
--threads=N 要使用的線程數(shù),默認(rèn) 1 個(gè) [1]
--events=N 最大允許的事件個(gè)數(shù) [0]
--time=N 最大的總執(zhí)行時(shí)間,以秒為單位 [10]
--forced-shutdown=STRING 在 --time 時(shí)間限制到達(dá)后,強(qiáng)制關(guān)閉之前等待的秒數(shù),默認(rèn)“off”禁用(number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable) [off]
--thread-stack-size=SIZE 每個(gè)線程的堆棧大小 [64K]
--rate=N 平均傳輸速率。0 則無限制 [0]
--report-interval=N 以秒為單位定期報(bào)告具有指定間隔的中間統(tǒng)計(jì)信息 0 禁用中間報(bào)告 [0]
--report-checkpoints=[LIST,...] 轉(zhuǎn)儲(chǔ)完整的統(tǒng)計(jì)信息并在指定的時(shí)間點(diǎn)重置所有計(jì)數(shù)器。參數(shù)是一個(gè)逗號(hào)分隔的值列表,表示從測試開始經(jīng)過這個(gè)時(shí)間量時(shí)必須執(zhí)行報(bào)告檢查點(diǎn)(以秒為單位)。報(bào)告檢查點(diǎn)默認(rèn)關(guān)閉。 []
--debug[=on|off] 打印更多 debug 信息 [off]
--validate[=on|off] 盡可能執(zhí)行驗(yàn)證檢查 [off]
--help[=on|off] 顯示幫助信息并退出 [off]
--version[=on|off] 顯示版本信息并退出 [off]
--config-file=FILENAME 包含命令行選項(xiàng)的文件
--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: # 偽隨機(jī)數(shù)發(fā)生器選項(xiàng)
--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: # 日志選項(xiàng)
--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: # 通用的數(shù)據(jù)庫選項(xiàng)
--db-driver=STRING 指定要使用的數(shù)據(jù)庫驅(qū)動(dòng)程序 ('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: # 內(nèi)建的數(shù)據(jù)庫驅(qū)動(dòng)程序,默認(rèn)支持 MySQL 和
PostgreSQL
mysql - MySQL driver
pgsql - PostgreSQL driver
mysql options: # MySQL 數(shù)據(jù)庫專用選項(xiàng)
--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 數(shù)據(jù)庫專用選項(xiàng)
--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: # 內(nèi)建測試類型
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 命令可以查看具體一個(gè)測試類型的幫助信息。
# 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 測試時(shí),會(huì)計(jì)算素?cái)?shù)(對這個(gè)數(shù)字除以 2 到這個(gè)數(shù)字平方根之間的所有數(shù)字來驗(yàn)證素?cái)?shù))直到某個(gè)指定值所需要的時(shí)間。單純的測試一組非常有限 CPU 硬件性能。
CPU 基準(zhǔn)測試時(shí)可以指定線程數(shù)量和素?cái)?shù)上限。
先查看幫助信息:
[root@VM_157_18_centos ~]# sysbench cpu help
sysbench 1.0.9 (using system LuaJIT 2.0.4)
cpu options:
--cpu-max-prime=N 素?cái)?shù)發(fā)生器的上限(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 時(shí),需要?jiǎng)?chuàng)建一組測試文件,測試文件需要大于可用內(nèi)存的大小,避免文件緩存在內(nèi)存中影響結(jié)果。測試流程為:準(zhǔn)備測試文件-》測試-》回收測試文件,命令如下:
# 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 基準(zhǔn)測試,可以通過 --file-test-mode 告訴 sysbench 要運(yùn)行的工作負(fù)載的類型,可用類型有:
seqwr:順序?qū)懭?/p>
seqrewr:順序重寫
seqrd:順序讀取
rndrd:隨機(jī)讀取
rndwr:隨機(jī)寫入
rndrw:隨機(jī)讀取/寫入
上面的例子中使用的是隨機(jī)讀取/寫入(rndrw)。通過 --time 選項(xiàng)指定測試持續(xù)時(shí)間(以秒為單位)。
幫助信息:
# 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
...此處總共創(chuàng)建了 128 個(gè)文件,每個(gè) 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
當(dāng)在 sysbench 中進(jìn)行內(nèi)存測試時(shí),會(huì)分配一個(gè)內(nèi)存緩沖區(qū)并在此執(zhí)行讀寫操作,每次操作都會(huì)讀完或?qū)憹M緩沖區(qū)。然后重復(fù)此操作直到達(dá)到指定大小(--memory-total-size)。可以提供多個(gè)線程(--threads),不同的緩沖區(qū)大小(--memory-block-size)和請求類型(讀或?qū)懀樞蚧螂S機(jī))。
幫助信息:
# 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 個(gè)線程,緩沖區(qū)大小為 8KB,在內(nèi)存中傳輸 4GB 數(shù)據(jù)的測試:
[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 時(shí),每個(gè)工作線程將被分配一個(gè) mutex(一種鎖)。每次執(zhí)行時(shí),每個(gè)線程將循環(huán)若干次(通過 --thread-yields 的數(shù)量設(shè)置),循環(huán)時(shí)這個(gè)線程會(huì)鎖定,在再次執(zhí)行時(shí)解鎖。
通過調(diào)整各種參數(shù),可以模擬具有單個(gè)或多個(gè)鎖的高并發(fā)線程下的情況。
幫助信息:
# 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]
運(yùn)行測試:
[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 時(shí),sysbench 將為每個(gè)線程運(yùn)行一個(gè)請求。這個(gè)請求首先會(huì)對 CPU 施加一些壓力(使用一個(gè)簡單的增量循環(huán),通過 --mutex-loops 參數(shù)設(shè)置),然后隨機(jī)使用一個(gè) mutex(鎖),遞增一個(gè)全局變量并再次釋放鎖。這個(gè)過程根據(jù)鎖的個(gè)數(shù)(--mutex-locks)多次重復(fù)。隨機(jī) mutex 取自大小為 --mutex-num 的參數(shù)池。
幫助信息:
# 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]
運(yùn)行測試:
[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 基準(zhǔn)測試
OLTP 基準(zhǔn)測試模擬了一個(gè)簡單的事務(wù)處理系統(tǒng)的工作負(fù)載。然而最新版本的 sysbench 把 OLTP 這個(gè)曾經(jīng)的內(nèi)建測試類型移除了,如果要用,需要在命令中指定測試類型的位置用 /usr/share/sysbench/oltp_read_only.lua 腳本代替測試類型。
流程:指定數(shù)據(jù)庫-》建表并生成數(shù)據(jù)-》運(yùn)行測試-》清理測試表。
首先生成表,注意替換命令中的數(shù)據(jù)庫密碼和 MySQL 套接字,如果 MySQL 安裝在默認(rèn)位置,可以去掉 --mysql-socket 選項(xiàng):
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
運(yùn)行測試,指定了 2 個(gè)并發(fā)線程,:
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
清理測試時(shí)生成的測試表:
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
例如我的選項(xiàng)就是:
[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