sysbench是一個開源的、模塊化的、跨平臺的多線程性能測試工具,可以用來進行CPU、內(nèi)存、磁盤I/O、線程、數(shù)據(jù)庫的性能測試。目前支持的數(shù)據(jù)庫有MySQL、Oracle和PostgreSQL。以下操作都將以支持MySQL數(shù)據(jù)庫為例進行。
下載源碼包
在http://sourceforge.net/projects/sysbench/網(wǎng)站上下載,我下載到的源碼包是sysbench-0.4.12.tar.gz。
環(huán)境準備
我的系統(tǒng)是CentOS 5.5,在進行編譯安裝前需要先進性相關(guān)軟件的安裝和更新。
首先,安裝和更新gcc,否則在編譯安裝時會報錯誤:configure: error: no acceptable C compiler found in $PATH,安裝命令為:yum install gcc。
其次,安裝和更新libtool,否則在編譯安裝時會報類似錯誤:
../libtool: line 838: X--tag=CC: command not found
../libtool: line 871: libtool: ignoring unknown tag : command not found
../libtool: line 838: X--mode=link: command not found
../libtool: line 1004: *** Warning: inferring the mode of operation is deprecated.: command not found
../libtool: line 1005: *** Future versions of Libtool will require --mode=MODE be specified.: command not found
../libtool: line 2231: X-g: command not found
../libtool: line 2231: X-O2: command not found
../libtool: line 2231: X-rdynamic: command not found
../libtool: line 1951: X-L/usr/lib/mysql: No such file or directory
../libtool: line 2400: Xsysbench: command not found
Fatal server error:
Server is already active for display 0
If this server is no longer running, remove /tmp/.X0-lock
and start again.
../libtool: line 2412: Xsysbench: command not found
../libtool: line 2547: X-lmysqlclient_r: command not found
../libtool: line 2547: X-lz: command not found
../libtool: line 2547: X-lcrypt: command not found
../libtool: line 2547: X-lnsl: command not found
../libtool: line 2547: X-lm: command not found
../libtool: line 2547: X-lmygcc: command not found
../libtool: line 2547: X-lrt: command not found
../libtool: line 2547: X-lm: command not found
../libtool: line 2629: X-L/root/tool/sysbench-0.4.12/sysbench: No such file or directory
../libtool: line 2547: X-lmysqlclient_r: command not found
../libtool: line 2547: X-lz: command not found
../libtool: line 2547: X-lcrypt: command not found
../libtool: line 2547: X-lnsl: command not found
../libtool: line 2547: X-lmygcc: command not found
../libtool: line 2547: X-lrt: command not found
../libtool: line 2547: X-lm: command not found
../libtool: line 2629: X-L/root/tool/sysbench-0.4.12/sysbench: No such file or directory
../libtool: line 2547: X-lmysqlclient_r: command not found
../libtool: line 2547: X-lz: command not found
../libtool: line 2547: X-lcrypt: command not found
../libtool: line 2547: X-lnsl: command not found
../libtool: line 2547: X-lmygcc: command not found
../libtool: line 2547: X-lrt: command not found
../libtool: line 2547: X-lm: command not found
../libtool: line 5162: Xgcc -pthread "" "" -o @OUTPUT@ sysbench.o sb_timer.o sb_options.o sb_logger.o db_driver.o "" tests/fileio/libsbfileio.a tests/threads/libsbthreads.a tests/memory/libsbmemory.a tests/cpu/libsbcpu.a tests/oltp/libsboltp.a tests/mutex/libsbmutex.a drivers/mysql/libsbmysql.a -L/root/tool/sysbench-0.4.12/sysbench -lmysqlclient_r -lz -lcrypt -lnsl -lmygcc -lrt -lm: No such file or directory
../libtool: line 5163: Xgcc -pthread "" "" -o @OUTPUT@ sysbench.o sb_timer.o sb_options.o sb_logger.o db_driver.o "" tests/fileio/libsbfileio.a tests/threads/libsbthreads.a tests/memory/libsbmemory.a tests/cpu/libsbcpu.a tests/oltp/libsboltp.a tests/mutex/libsbmutex.a drivers/mysql/libsbmysql.a -L/root/tool/sysbench-0.4.12/sysbench -lmysqlclient_r -lz -lcrypt -lnsl -lmygcc -lrt -lm: No such file or directory
安裝命令為:yum install libtool
編譯安裝
準備好環(huán)境之后就可以開始進行編譯安裝了。
首先對壓縮包進行解壓:tar zxf sysbench-0.4.12.tar.gz。
其次,在解壓好的目錄下運行命令:./autogen.sh,對環(huán)境進行清理。該操作是sysbench0.4.12獨有的,不進行的話也會出現(xiàn)類似如上第2個錯誤。
然后,進行編譯安裝。如果MySQL是默認安裝的,則直接運行如下命令進行編譯安裝:
./configure --build=i686-pc-linux-gnu && make && make install
其中,--build指定操作系統(tǒng)的類型,否則可能會出現(xiàn)如下錯誤:
checking build system type... Invalid configuration `i686-pc-linux-': machine `i686-pc-linux' not recognized
configure: error: /bin/sh config/config.sub i686-pc-linux- failed
如果MySQL不是安裝在標準默認目錄下,那么就還需要指定MySQL的路徑。如MySQL安裝在/var/mysql下,則運行如下命令進行編譯安裝:
./configure --build=i686-pc-linux-gnu –with-mysql-includes=/var/mysql/include –with-mysql-libs=/var/mysql/lib && make && make install
如果需要sysbench支持Oracle或者PostgreSQL的話,還需要在編譯時加上--with-pgsql或者--with-oracle。
進行測試
編譯安裝成功后就可以開始進行性能測試了。測試的方法和選項很多,這里大致介紹一下相關(guān)的命令和選項。
CPU性能測試
sysbench --test=cpu --cpu-max-prime=5000 run
CPU的性能測試主要通過素數(shù)的運算來進行,--cpu-max-prime用來選項指定最大的素數(shù),具體參數(shù)可以根據(jù)CPU的性能來設置。
內(nèi)存性能測試
sysbench --test=memory --memory-block-size=8k --memory-total-size=4G run
--memory-block-size指定每個block的大小,--memory-total-size指定總的傳輸量,另外還有其他選項,可通過命令sysbench –test=memory help進行查看。
磁盤I/O性能測試
首先生成需要測試的文件
sysbench --test=fileio --num-threads=16 --file-num=2 --file-total-size=2G --file-test-mode=rndrw --file-rw-ratio=2 prepare
然后執(zhí)行測試
sysbench --test=fileio --num-threads=16 --file-num=2 --file-total-size=2G --file-test-mode=rndrw --file-rw-ratio=2 run
最后清理測試文件
sysbench --test=fileio --num-threads=16 --file-num=2 --file-total-size=2G --file-test-mode=rndrw --file-rw-ratio=2 cleanup
--num-threads指定最大創(chuàng)建的線程數(shù),--file-num指定文件數(shù)目,--file-taotal-size指定創(chuàng)建的文件總大小,--file-test-mode指定文件讀寫模式,rndrw為隨機讀,--file-rw-ratio指定讀寫比率。另外還有更多的選項可以通過命令sysbench –test=fileio help進行查看。
線程測試
sysbench --test=threads --num-threads=32 --thread-yields=100 --thread-locks=2 run
--num-threads指定線程數(shù),--thread-yield指定每個請求的壓力,--thread-locks指定每個線程的鎖數(shù)量。
數(shù)據(jù)庫性能測試
首先,準備測試數(shù)據(jù)
sysbench --test=oltp --mysql-host=server1 --mysql-user=root --mysql-password=MySQLPASSWORD --mysql-db=test --oltp-table-name=sbtest --mysql-table-engine=MyISAM --oltp-table-size=1000000 prepare
然后執(zhí)行測試
sysbench --test=oltp --mysql-host=server1 --mysql-port=3306 --mysql-user=root --mysql-password=MySQLPASSWORD --mysql-db=test --oltp-table-name=sbtest --mysql-table-engine=MyISAM --mysql-engine-trx=no --oltp-skip-trx=on --oltp-table-size=1000000 --max-requests=1000 --num-threads=100 run
最后清理測試數(shù)據(jù)
sysbench --test=oltp --mysql-host=server1 --mysql-user=root --mysql-password=MySQLPASSWORD --mysql-db=test --oltp-table-name=sbtest cleanup
--mysql-host指定使用的MySQL服務器的地址,--mysql-user指定連接MySQL的用戶名,--mysql-password指定密碼,--mysql-db指定使用的數(shù)據(jù)庫,--oltp-table-name指定使用的表,--mysql-table-engine指定表所使用的數(shù)據(jù)庫引擎,--mysql-engine-trx說明使用的數(shù)據(jù)庫引擎是否是事務的,--oltp-skip-trx指定是否忽略事務語句(BEGIN/COMMIT),--oltp-table-size指定數(shù)據(jù)表中的記錄數(shù),--max-requests指定最大請求數(shù),--num-threads指定線程數(shù),其他更多的選項可以使用命令sysbench –test=oltp help查看。