使用sysbench對mysql壓力測試
sysbench是一個模塊化的、跨平臺、多線程基準測試工具,主要用於評估測試各種不同系統參數下的數據庫負載情況。關於這個項目的詳細介紹請看:https://github.com/akopytov/sysbench 。
它主要包括以下幾種方式的測試:
cpu性能
磁盤io性能
調度程序性能
內存分配及傳輸速度
POSIX線程性能
數據庫性能(OLTP基準測試)
sysbench的數據庫OLTP測試支持MySQL、PostgreSQL、Oracle,目前主要用於Linux操作系統,開源社區已經將sysbench移植到了Windows,並支持SQL Server的基準測試。
廢話不多說,開始。
1. sysbench安裝
mysql版本: mysql-community-server-5.6.29
OS: CentOS 6.7 X86_64
sysbench 0.5相比0.4版本有一些變化,包括oltp測試結合了lua腳本,還多了一些隱藏選項,本文會涉及得到一部分。
目前許多倉庫裏已編譯好的二進制sysbench還是0.4.x版本,不過現在主流也還是github上的0.5(我這裏使用的是1.0),可以從 這裏下載0.5版本的rpm包直接安裝,不過我選擇自己編譯,因爲只有這個辦法是通用的。
// 先安裝編譯依賴環境
$ sudo yum install gcc gcc-c++ automake make libtool mysql-community-devel mysql-devel $ ./autogen.sh $ ./configure --prefix=/usr/local/sysbench-0.5 $ ./make && make install
// 0.5版本需要oltp.lua測試腳本 // 如果是rpm包方式安裝的,在 /usr/share/doc/sysbench/tests/db/ 下可找到 $ cd /usr/local/sysbench && sudo mkdir -p share/tests/db $ cp /tmp/sysbench/sysbench/tests/db/*.lua share/tests/db/ $ ./bin/sysbench --version sysbench 0.5
如果需要測試PostgreSQL、Oracle,則在configure時需要加上 --with-oracle 或者 --with-pgsql 參數
2. 使用sysbench對mysql壓測
2.1 只讀示例
sysbench \ --test=share/sysbench/oltp.lua \ --mysql-host=10.229.153.175 \ --mysql-port=7001 \ --mysql-user=kp \ --mysql-password=kp123456 \ --mysql-db=conanwang \ --oltp-tables-count=10 \ --oltp-table-size=10000000 \ --report-interval=10 \ --oltp-dist-type=uniform \ --rand-init=on \ --max-requests=0 \ --oltp-test-mode=nontrx \ --oltp-nontrx-mode=select \ --oltp-read-only=on \ --oltp-skip-trx=on \ --max-time=120 \ --num-threads=12 \ [prepare|run|cleanup]
注意最後一行,一項測試開始前需要用prepare
來準備好表和數據,run
執行真正的壓測,cleanup
用來清除數據和表。實際prepare的表結構:
mysql> desc dbtest1a.sbtest1; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | k | int(10) unsigned | NO | MUL | 0 | | | c | char(120) | NO | | | | | pad | char(60) | NO | | | | +-------+------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
上面的測試命令代表的是:對mysql進行oltp基準測試,表數量10,每錶行數約1000w(幾乎delete多少就會insert的多少),並且是非事務的只讀測試,持續60s,併發線程數12。
需要說明的選項:
mysql-db=dbtest1a
:測試使用的目標數據庫,這個庫名要事先創建--oltp-tables-count=10
:產生表的數量--oltp-table-size=10000000
:每個表產生的記錄行數--oltp-dist-type=uniform
:指定隨機取樣類型,可選值有 uniform(均勻分佈), Gaussian(高斯分佈), special(空間分佈)。默認是special--oltp-read-only=off
:表示不止產生只讀SQL,也就是使用oltp.lua時會採用讀寫混合模式。默認 off,如果設置爲on,則不會產生update,delete,insert的sql。--oltp-test-mode=nontrx
:執行模式,這裏是非事務式的。可選值有simple,complex,nontrx。默認是complexsimple:簡單查詢,SELECT c FROM sbtest WHERE id=N
complex (advanced transactional):事務模式在開始和結束事務之前加上begin和commit, 一個事務裏可以有多個語句,如點查詢、範圍查詢、排序查詢、更新、刪除、插入等,並且爲了不破壞測試表的數據,該模式下一條記錄刪除後會在同一個事務裏添加一條相同的記錄。
nontrx (non-transactional):與simple相似,但是可以進行update/insert等操作,所以如果做連續的對比壓測,你可能需要重新cleanup,prepare。
--oltp-skip-trx=[on|off]
:省略begin/commit語句。默認是off--rand-init=on
:是否隨機初始化數據,如果不隨機化那麼初始好的數據每行內容除了主鍵不同外其他完全相同--num-threads=12
: 併發線程數,可以理解爲模擬的客戶端併發連接數--report-interval=10
:表示每10s輸出一次測試進度報告--max-requests=0
:壓力測試產生請求的總數,如果以下面的max-time
來記,這個值設爲0--max-time=120
:壓力測試的持續時間,這裏是2分鐘。
注意,針對不同的選項取值就會有不同的子選項。比如oltp-dist-type=special
,就有比如oltp-dist-pct=1
、oltp-dist-res=50
兩個子選項,代表有50%的查詢落在1%的行(即熱點數據)上,另外50%均勻的(sample uniformly)落在另外99%的記錄行上。
再比如oltp-test-mode=nontrx
時, 就可以有oltp-nontrx-mode
,可選值有select(默認), update_key, update_nokey, insert, delete,代表非事務式模式下使用的測試sql類型。
以上代表的是一個只讀的例子,可以把num-threads
依次遞增(16,36,72,128,256,512),或者調整my.cnf參數,比較效果。另外需要注意的是,大部分mysql中間件對事務的處理,默認都是把sql發到主庫執行,所以只讀測試需要加上oltp-skip-trx=on
來跳過測試中的顯式事務。
ps1: 只讀測試也可以使用share/tests/db/select.lua
進行,但只是簡單的point select。
ps2: 我在用sysbench壓的時候,在mysql後端會話裏有時看到大量的query cache lock,如果使用的是uniform取樣,最好把查詢緩存關掉。當然如果是做兩組性能對比壓測,因爲都受這個因素影響,關心也不大。
2.2 混合讀寫
讀寫測試還是用oltp.lua,只需把--oltp-read-only
等於off
。
./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx--oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 prepare
./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx--oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 run
./bin/sysbench --test=./share/tests/db/oltp.lua --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=10 --rand-init=on --max-requests=0 --oltp-test-mode=nontrx--oltp-nontrx-mode=select --oltp-read-only=off --max-time=120 --num-threads=128 cleanup
然而oltp-test-mode=nontrx
一直沒有跟着我預期的去走,在mysql general log裏面看到的sql記錄與complex
模式相同。所以上面示例中的--oltp-test-mode=nontrx --oltp-nontrx-mode=select
可以刪掉。
update:
sysbench作者 akopytov 對我這個疑問有了回覆:https://github.com/akopytov/sysbench/issues/34 ,原來sysbench 0.5版本去掉了這個選項,因爲作者正在準備1.0版本,所以也就沒有更新0.5版本的doc。網上的博客漫天飛,就沒有一個提出來的,也是沒誰了。
分析一下oltp.lua腳本內容,可以清楚單個事務各操作的默認比例:select:update_key:update_non_key:delete:insert = 14:1:1:1:1,可通過oltp-point-selects
、oltp-simple-ranges
、oltp-sum-ranges
、oltp-order-ranges
、oltp-distinct-ranges
,oltp-index-updates
、oltp-non-index-updates
這些選項去調整讀寫權重。
同只讀測試一樣,在atlas,mycat這類中間件測試中如果不加oltp-skip-trx=on
,那麼所有查詢都會發往主庫,但如果在有寫入的情況下使用--oltp-skip-trx=on
跳過BEGIN和COMMIT,會出現問題:
ALERT: failed to execute MySQL query:
INSERT INTO sbtest4 (id, k, c, pad) VALUES (48228, 47329, '82773802508-44916890724-85859319254-67627358653-96425730419-64102446666-75789993135-91202056934-68463872307-28147315305', '13146850449-23153169696-47584324044-14749610547-34267941374')
:
ALERT: Error 1062 Duplicate entry ‘48228’ for key ‘PRIMARY’
FATAL: failed to execute function `event’: (null)
原因也很容易理解,每個線程將選擇一個隨機的表,不加事務的情況下高併發更新(插入)出現重複key的概率很大,但我們壓測不在乎這些數據,所以需要跳過這個錯誤--mysql-ignore-errors=1062
,這個問題老外有出過打補丁的方案允許--mysql-ignore-duplicates=on
,但作者新加入的忽略錯誤碼這個功能已經取代了它。mysql-ignore-errors
選項是0.5版本加入的,但目前沒有文檔標明,也是我在github上提的 issue 作者回復的。
這裏不得不佩服老外的辦事效率和責任心,提個疑惑能立馬得到回覆,反觀國內,比如在atlas,mycat項目裏提到問題到現在都沒人搭理。。。
2.3 只更新
如果基準測試的時候,你只想比較兩個項目的update(或insert)效率,那可以不使用oltp腳本,而直接改用update_index.lua
:
./bin/sysbench --test=./share/tests/db/update_index.lua \
--mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser \
--mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 \
--report-interval=10 --rand-init=on --max-requests=0 \
--oltp-read-only=off --max-time=120 --num-threads=128 \
[ prepare | run | cleanup ]
此時像oltp-read-only=off
許多參數都失效了。需要說明的是這裏 (非)索引更新,不是where條件根據索引去查找更新,而是更新索引列上的值。
3. 結果解讀
sysbench 0.5: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 128
Report intermediate results every 20 second(s)
Initializing random number generator from timer.
Random number generator seed is 0 and will be ignored
Initializing worker threads...
Threads started!
[ 20s] threads: 128, tps: 2354.54, reads: 33035.89, writes: 9423.39, response time: 66.80ms (95%), errors: 0.00, reconnects: 0.00
[ 40s] threads: 128, tps: 2377.75, reads: 33274.26, writes: 9507.55, response time: 66.88ms (95%), errors: 0.00, reconnects: 0.00
[ 60s] threads: 128, tps: 2401.35, reads: 33615.30, writes: 9607.40, response time: 66.40ms (95%), errors: 0.00, reconnects: 0.00
[ 80s] threads: 128, tps: 2381.20, reads: 33331.50, writes: 9522.55, response time: 67.30ms (95%), errors: 0.00, reconnects: 0.00
[ 100s] threads: 128, tps: 2388.85, reads: 33446.10, writes: 9556.35, response time: 67.00ms (95%), errors: 0.00, reconnects: 0.00
[ 120s] threads: 128, tps: 2386.40, reads: 33421.35, writes: 9545.35, response time: 66.94ms (95%), errors: 0.00, reconnects: 0.00
OLTP test statistics:
queries performed:
read: 4003048 //總select數量
write: 1143728 //總update、insert、delete語句數量
other: 571864 //commit、unlock tables以及其他mutex的數量
total: 5718640
transactions: 285932 (2382.10 per sec.) //通常需要關注的數字(TPS)
read/write requests: 5146776 (42877.85 per sec.)
other operations: 571864 (4764.21 per sec.)
ignored errors: 0 (0.00 per sec.) //忽略的錯誤數
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 120.0334s //即max-time指定的壓測實際
total number of events: 285932 //總的事件數,一般與transactions相同
total time taken by event execution: 15362.6623s
response time:
min: 17.60ms
avg: 53.73ms //95%的語句的平均響應時間
max: 252.90ms
approx. 95 percentile: 66.88ms
Threads fairness:
events (avg/stddev): 2233.8438/9.04
execution time (avg/stddev): 120.0208/0.01
我們一般關注的用於繪圖的指標主要有:
response time avg: 平均響應時間。(後面的95%的大小可以通過
--percentile=98
的方式去更改)transactions: 精確的說是這一項後面的TPS 。但如果使用了
-oltp-skip-trx=on
,這項事務數恆爲0,需要用total number of events
去除以總時間,得到tps(其實還可以分爲讀tps和寫tps)read/write requests: 用它除以總時間,得到吞吐量QPS
當然還有一些系統層面的cpu,io,mem相關指標
sysbench還可以對文件系統IO測試,CPU性能測試,以及內存分配與傳輸速度測試,這裏就不介紹了。
總結起來sysbench的缺點就是,模擬的表結構太簡單,不像tpcc-mysql那樣完整的事務系統。但對於性能壓測對比還是很有用的,因爲sysbench使用的環境參數限制是一樣的。