0、參考資料&適用範圍
參考資料:
https://github.com/akopytov/sysbench 地址下的README.md文檔。
文檔中詳細介紹了安裝、用法、命令行參數。
適用範圍:
壓測linux CPU/內存性能
壓測MySQL、Tidb、MariaDB數據庫TPS/OPS
1、安裝
RHEL/CentOS爲例,其它平臺參考官方github文檔
下載安裝包
https://github.com/akopytov/sysbench 地址下載最新的安裝包
或者
wget https://github.com/akopytov/sysbench/archive/1.0.19.tar.gz
安裝前準備工作
yum -y install make automake libtool pkgconfig libaio-devel
# For MySQL support, replace with mysql-devel on RHEL/CentOS 5
yum -y install mariadb-devel openssl-devel
# For PostgreSQL support
yum -y install postgresql-devel
一把梭安裝
yum -y install sysbench
安裝sysbench(源碼編譯形式)
./autogen.sh
#Add --with-pgsql to build with PostgreSQL support
./configure
make -j
make install
注意1:默認安裝位置爲:/usr/local,如果要安裝多個 sysbench,使用 ./configure --prefix 指定安裝位置;
注意2:如果您在非標準位置具有MySQL標頭和庫(並且mysql_config在其中找不到PATH),則可以使用–with-mysql-includes和–with-mysql-libs選項明確指定它們.
注意3:默認支持mysql,要編譯沒有MySQL支持的sysbench,請使用–without-mysql。如果要支持pg使用選項–with-pgsql
編譯時建議不指定 --with-mysql-includes、–with-mysql-libs,而是在操作系統上設置 LD_LIBRARY_PATH 解決“libmysqlclient.so.18: cannot open shared object file: No such file or directory”問題
2、壓測
2.1、CPU壓測
常用參數說明
–cpu-max-prime: 素數生成數量的上限
- 若設置爲3,則表示2、3、5(這樣要計算1-5共5次)
- 若設置爲10,則表示2、3、5、7、11、13、17、19、23、29(這樣要計算1-29共29次)
- 默認值爲10000
–threads: 線程數
- 若設置爲1,則sysbench僅啓動1個線程進行素數的計算
- 若設置爲2,則sysbench會啓動2個線程,同時分別進行素數的計算
- 默認值爲1
–time: 運行時長,單位秒
- 若設置爲5,則sysbench會在5秒內循環往復進行素數計算,
從輸出結果可以看到在5秒內完成了幾次,
比如配合--cpu-max-prime=3,則表示第一輪算得3個素數,
如果時間還有剩就再進行一輪素數計算,直到時間耗盡。
每完成一輪就叫一個event
- 默認值爲10
- 相同時間,比較的是誰完成的event多
–events: event上限次數
- 若設置爲100,則表示當完成100次event後,即使時間還有剩,也停止運行
- 默認值爲0,則表示不限event次數
- 相同event次數,比較的是誰用時更少
壓測案例
# 素數上限2萬,默認10秒,6個線程
sysbench cpu --cpu-max-prime=20000 --threads=6 run
結果分析
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)
Running the test with following options:
Number of threads: 6 // 指定線程數爲6
Initializing random number generator from current time
Prime numbers limit: 20000 // 每個線程產生的素數上限均爲2萬個
Initializing worker threads...
Threads started!
CPU speed:
events per second: 1290.56 // 所有線程每秒完成了1290.56次event
General statistics:
total time: 10.0038s // 共耗時10秒
total number of events: 12913 // 10秒內所有線程一共完成了12913次event
Latency (ms):
min: 3.07 // 完成1次event的最少耗時3.07毫秒
avg: 4.64 // 所有event的平均耗時4.64毫秒
max: 52.02 // 完成1次event的最多耗時52.02毫秒
95th percentile: 6.21 // 95%次event在6.21毫秒內完成
sum: 59974 //每個線程耗時10秒,6個線程疊加耗時總計將近60秒
Threads fairness:
events (avg/stddev): 2152.1667/61.46
//平均每個線程完成2152.1667次event,標準差爲61.46
execution time (avg/stddev): 9.9958/0.00
// 每個線程平均耗時10秒,標準差爲0
2.2、MySQL數據庫壓測
2.2.1、選項說明
lua腳本
腳本默認位置/usr/share/sysbench,一般使用oltp_read_write.lua這個腳本。
sysbench的參數有很多,常用的如下:
--mysql-host:MySQL服務器主機名,默認localhost。
--mysql-port:MySQL服務器端口,默認3306
--mysql-user:用戶名
--mysql-password:密碼
--oltp-test-mode:執行模式,包括simple、nontrx和complex,默認是complex。simple模式下只測試簡單的查詢;nontrx不僅測試查詢,還測試插入更新等,但是不使用事務;complex模式下測試最全面,會測試增刪改查,而且會使用事務。
--oltp-tables-count:測試的表數量,根據實際情況選擇
--oltp-table-size:測試的表的大小,根據實際情況選擇
--threads:客戶端的併發連接數
--time:測試執行的時間,單位是秒,該值不要太短,可以選擇120
--report-interval:生成報告的時間間隔,單位是秒,如10
2.2.2、壓測步驟
數據準備
sysbench /usr/share/sysbench/oltp_read_write.lua \
--mysql-host=1.1.3.111 \
--mysql-port=3309 \
--mysql-user=root \
--mysql-password=1234.C0m \
--mysql-socket=/usr/local/mysql57/mysql.sock \
--mysql-db=otpstest \
--tables=10 \
--table-size=1000 \
--report-interval=3 \
--threads=10 prepare
注意:–mysql-db指定的庫必須先存在,不會幫你創建數據庫
開始壓測
sysbench /usr/share/sysbench/oltp_read_write.lua \
--mysql-host=1.1.3.111 \
--mysql-port=3309 \
--mysql-user=root \
--mysql-password=1234.Com \
--mysql-socket=/usr/local/mysql57/mysql.sock \
--mysql-db=otpstest \
--tables=10 \
--table-size=1000 \
--report-interval=3 \
--threads=128 \
--max-time=600 run
清理壓測數據
sysbench /usr/share/sysbench/oltp_read_write.lua \
--mysql-host=1.1.3.111 \
--mysql-port=3309 \
--mysql-user=root \
--mysql-password=1234.C0m \
--mysql-socket=/usr/local/mysql57/mysql.sock \
--mysql-db=otpstest \
--tables=10 \
--report-interval=3 cleanup
注意:需要指定對應的表個數–tables=,對應創建數據時指定的表個數。
壓測時注意事項:
1.可以調整 --threads併發數,觀察不同併發下的tps(每秒的事務執行數量)。一般來說隨着併發數的增加,tps會增加達到最大值,之後趨於穩定,也可能因爲過大的併發導致性能下降。
2.儘量讓 sysbench 與 MySQL 在不同的服務器上,避免相互影響。
3.基準測試要進行多次纔有意義。