參考
合成自:http://linuxperformance.top/index.php/archives/83/
https://blog.csdn.net/notbaron/article/details/77413379
https://www.cnblogs.com/kismetv/archive/2017/09/30/7615738.html#t24
sysbench是一款開源的多線程性能測試工具,可以執行CPU/內存/線程/IO/數據庫等方面的性能測試。
sysbench支持以下幾種測試模式:
1、CPU運算性能
2、磁盤IO性能
3、調度程序性能
4、內存分配及傳輸速度
5、POSIX線程性能
6、數據庫性能(OLTP基準測試)
下載地址及安裝(Linux)
下載地址:https://github.com/akopytov/sysbench
安裝
-
(1)下載解壓
-
wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbench-1.0.zip"
-
unzip sysbench-1.0.zip
-
cd sysbench-1.0
-
(2)安裝依賴
-
yum install automake libtool –y
-
(3)安裝
-
安裝之前,確保位於之前解壓的sysbench目錄中。
-
./autogen.sh
-
./configure
-
export LD_LIBRARY_PATH=/usr/local/mysql/include #這裏換成機器中mysql路徑下的include
-
make
-
make install
-
(4)安裝成功
-
[root@test sysbench-1.0]# sysbench --version
-
sysbench 1.0.9
如果出現:configure: error: mysql_config executable not found
需要安裝:mariadb-devel (yum install mariadb-devel 安裝mysql_config)
PS:此外可能還要安裝mariadb-server包,yum install mariadb-server。
1、CPU測試
對CPU的性能測試通常有:1.質數計算;2圓周率計算;sysbench使用的就是通過質數相加的測試。對CPU測試直接運行run即可
-
sysbench --threads=20 --events=10000 --debug=on --test=cpu --cpu-max-prime=20000 run
-
20個線程執行1萬條請求,每個請求執行質數相加到20000
2、內存測試
-
測試8K順序分配:
-
sysbench --threads=12 --events=10000 --test=memory --memory-block-size=8K --memory-total-size=100G --memory-access-mode=seq run
-
測試8K隨機分配。
-
sysbench --threads=12 --events=10000 --test=memory --memory-block-size=8K --memory-total-size=100G --memory-access-mode=rnd run
3、文件io測試
-
IO的測試主要用於測試IO的負載性能。主要測試選項爲--file-test-mode。還有可以關注的參數包括--file-block-size、--file-io-mode、--file-fsync-freq 、--file-rw-ratio,具體參數含義請見參數解釋章節。
-
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw prepare
-
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw run
-
sysbench --threads=12 --events=10000 fileio --file-total-size=3G --file-test-mode=rndrw cleanup
-
對比兩臺服務器的io性能,需要跑相同的線程
4、鎖測試
-
互斥鎖測試模擬所有線程在同一時刻併發運行。
-
sysbench --threads=12 mutex --mutex-num=1024 --mutex-locks=10000 --mutex-loops=10000 run
5、線程測試
sysbench threads --num-threads=64 --thread-yields=100 --thread-locks=2 run
6、OLTP測試
oltp是針對數據庫的基準測試,例如每次對數據庫進行優化後執行基準測試來測試不同的配置的tps。sysbench 0.5之後通過一系列LUA腳本來替換之前的oltp,來模擬更接近真實的基準測試環境。這些測試腳本包含:insert.lua、oltp.lua、parallel_prepare.lua、select_random_points.lua、update_index.lua、delete.lua oltp_simple.lua、select.lua、select_random_ranges.lua、update_non_index.lua
-
預置條件:
-
a)創建數據庫:
-
mysqladmin create sbtest -uroot –p
-
或者
-
SQL>create database sbtest
-
b)增加權限:
-
grant usage on . to 'sbtest'@'%' identified by password '*2AFD99E79E4AA23DE141540F4179F64FFB3AC521';
-
其中密碼通過如下命令獲取:
-
select password('sbtest');
-
+-------------------------------------------+
-
| password('sbtest') |
-
+-------------------------------------------+
-
| 2AFD99E79E4AA23DE141540F4179F64FFB3AC521 |
-
+-------------------------------------------+
-
1 row in set (0.00 sec)
-
c)增加權限:
-
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX ON sbtest.TO 'sbtest'@"%";
-
grant all privileges on . to 'sbtest'@'%';
-
flush privileges;
-
或者簡單粗暴:
-
create user 'sbtest'@'127.0.0.1' identified by 'sbtest';
-
grant all privileges on . to
-
flush privileges;
-
e)OLTP測試:
-
準備階段:
-
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on prepare
-
測試階段:
-
命令如下:
-
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on run
-
清理階段:
-
sysbench --test= /usr/local/share/sysbench/tests/include/oltp_legacy/oltp.lua --oltp-table-size=10000 --mysql-table-engine=innodb --oltp-tables-count=10 --mysql-user=sbtest --mysql-password=sbtest --mysql-port=3306 --mysql-host=127.0.0.1 --max-requests=0 --time=10 --report-interval=1 --threads=10 --oltp-point-selects=1 --oltp-simple-ranges=0 --oltp_sum_ranges=0 --oltp_order_ranges=0 --oltp_distinct_ranges=0 --oltp-read-only=on cleanup
-
最後刪除數據庫
-
SQL>drop database sbtest;
完整實例:
1)、準備數據
-
執行模式爲complex,使用了10個表,每個表有10萬條數據,客戶端的併發線程數爲10,執行時間爲120秒,每10秒生成一次報告。
-
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 prepare
2)、執行測試
-
//將測試結果導出到文件中,便於後續分析
-
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 run >> /home/test/mysysbench.log
3)、清理數據
-
//執行完測試後,清理數據,否則後面的測試會受到影響
-
sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-host=192.168.10.10 --mysql-port=3306 --mysql-user=root --mysql-password=123456 cleanup
4)、測試結果
其中,對於我們比較重要的信息包括:
queries:查詢總數及qps
transactions:事務總數及tps
Latency-95th percentile:前95%的請求的最大響應時間,本例中是344毫秒,這個延遲非常大,是因爲我用的MySQL服務器性能很差;在正式環境中這個數值是絕對不能接受的。
語法 sysbench [options]... [testname] [command]
1)、執行sysbench –help,可以看到sysbench的詳細使用方法。
2)、command,要執行的命令,包括prepare、run和cleanup,顧名思義,prepare是爲測試提前準備數據,run是執行正式的測試,cleanup是在測試完成後對數據庫進行清理。
3)、testname指定了要進行的測試,在老版本的sysbench中,可以通過--test參數指定測試的腳本;而在新版本中,--test參數已經聲明爲廢棄,可以不使用--test,而是直接指定腳本。
4)、options:sysbench的參數有很多,其中比較常用的包括:
7、參數解釋
-
通過命令sysbench –help可以瞭解各參數的具體解釋
-
--test=tests/db/oltp.lua 表示調用 tests/db/oltp.lua 腳本進行 oltp 模式測試
-
--threads:客戶端的併發連接數
-
--time:測試執行的時間,單位是秒,該值不要太短,可以選擇120
-
--report-interval:生成報告的時間間隔,單位是秒,如10
-
--oltp_tables_count=10 表示會生成 10 個測試表
-
--oltp-table-size=100000 表示每個測試表填充數據量爲 100000
-
--mysql-engine-trx=STRING指定不同的存儲引擎測試。
-
--oltp-test-mode=STRING測試類型:simple(簡單select測試),complex(事務測試),nontrx(非事務測試),sp(存儲過程) ;默認complex
-
--oltp-sp-name=STRING指定存儲過程進行語句測試
-
--oltp-table-size=N指定表的記錄大小,默認[10000]
-
--oltp-num-tables=N指定測試表的數量,默認1
-
--rand-init=on 表示每個測試表都是用隨機數據來填充的
-
--file-num=N創建測試文件的數量,默認128個
-
--file-block-size=N block size大小,默認16K
-
--file-total-size=SIZE所有文件的總大小,默認2G
-
--file-test-mode=STRING測試類型 {seqwr(順序寫), seqrewr(順序讀寫), seqrd(順序讀), rndrd(隨機讀), rndwr(隨機寫), rndrw(隨機讀寫)} --file-io-mode=STRING I/O模式,需要系統支持默認sync[sync(同步IO),async(異步IO),mmap()]
-
--file-async-backlog=N每個線程的異步操作隊列數,默認128個,需要--file-io-mode=async;
-
--file-extra-flags=STRING additional flags to use on opening files {sync,dsync,direct} []
-
--file-fsync-freq=N當請求數達到多少時執行fsync()刷新,默認100,0代表過程中不執行fsync()
-
--file-fsync-all=[on|off] 執行每一個寫操作後執行fsync()刷新操作,默認關閉off --file-fsync-end=[on|off] 測試結束執行fsync()操作,默認開啓on --file-fsync-mode=STRING 同步刷新方法,默認fsync {fsync, fdatasync} --file-merged-requests=N合併指定數量的IO請求,0代表不合並,默認0 --file-rw-ratio=N 讀寫比例,默認1.5/1
8、建議、注意
建議:
1)、在開始測試之前,應該首先明確:應採用針對整個系統的基準測試,還是針對MySQL的基準測試,還是二者都需要。
2)、如果需要針對MySQL的基準測試,那麼還需要明確精度方面的要求:是否需要使用生產環境的真實數據,還是使用工具生成也可以;前者實施起來更加繁瑣。如果要使用真實數據,儘量使用全部數據,而不是部分數據。
3)、要進行多次纔有意義。需要注意主從同步的狀態。測試必須模擬多線程的情況,單線程情況不但無法模擬真實的效率,也無法模擬阻塞甚至死鎖情況。
注意:
1)、儘量不要在MySQL服務器運行的機器上進行測試,一方面可能無法體現網絡(哪怕是局域網)的影響,另一方面,sysbench的運行(尤其是設置的併發數較高時)會影響MySQL服務器的表現。
2)、可以逐步增加客戶端的併發連接數(--thread參數),觀察在連接數不同情況下,MySQL服務器的表現;如分別設置爲10,20,50,100等。
3)、一般執行模式選擇complex即可,如果需要特別測試服務器只讀性能,或不使用事務時的性能,可以選擇simple模式或nontrx模式。
4)、如果連續進行多次測試,注意確保之前測試的數據已經被清理乾淨。
謝謝分享:
合成自:http://linuxperformance.top/index.php/archives/83/
https://blog.csdn.net/notbaron/article/details/77413379
https://www.cnblogs.com/kismetv/archive/2017/09/30/7615738.html#t24