前言
在對網(wǎng)站整體性能進(jìn)行benchmark時(shí),可以使用多種工具,比如大名鼎鼎的ab(Apache bench),http_load等工具。這里我們不關(guān)注他們的使用,如果你想了解,可以自行在網(wǎng)上找到答案。
重點(diǎn)來說mysql的基準(zhǔn)測試如何進(jìn)行,也有很多種工具來供我們選擇,比如mysqlslap、sysbench、Super Smack等,其中mysqlslap的使用MySQL官網(wǎng)給出了介紹,Super Smack是服務(wù)器壓力測試強(qiáng)有力的工具,那么sysbench便是我們進(jìn)行MySQL基準(zhǔn)測試的很漂亮的工具了。
sysbench
sysbench是一款開源的多線程性能測試工具,可以執(zhí)行CPU/內(nèi)存/線程/IO/數(shù)據(jù)庫等方面的性能測試。數(shù)據(jù)庫目前支持MySQL/Oracle/PostgreSQL;
它主要包括以下幾種方式的測試:
???? 1、cpu性能
???? 2、磁盤io性能
???? 3、調(diào)度程序性能
???? 4、內(nèi)存分配及傳輸速度
???? 5、POSIX線程性能
?????6、數(shù)據(jù)庫性能(OLTP基準(zhǔn)測試)
sysbench的數(shù)據(jù)庫OLTP測試支持MySQL、PostgreSQL、Oracle,目前主要用于Linux操作系統(tǒng),開源社區(qū)已經(jīng)將sysbench移植到了Windows,并支持SQL Server的基準(zhǔn)測試。
廢話不多說,開始。
一、sysbench安裝
mysql版本: mysql-community-server-5.6.29
OS: CentOS 6.7 X86_64
sysbench 0.5相比0.4版本有一些變化,包括oltp測試結(jié)合了lua腳本,還多了一些隱藏選項(xiàng),本文會(huì)涉及得到一部分。
// 先安裝編譯依賴環(huán)境 $ sudo yum install gcc gcc-c++ automake make libtool mysql-community-devel $ cd /tmp && git clone https://github.com/akopytov/sysbench.git $ cd /tmp/sysbench && ./autogen.sh $ ./configure --prefix=/usr/local/sysbench-0.5 $ ./make && sudo 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時(shí)需要加上 –with-oracle 或者 –with-pgsql 參數(shù)
二、使用sysbench對mysql壓測
2.1 只讀示例
./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 --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]
注意最后一行,一項(xiàng)測試開始前需要用prepare來準(zhǔn)備好表和數(shù)據(jù),run執(zhí)行真正的壓測,cleanup用來清除數(shù)據(jù)和表。實(shí)際prepare的表結(jié)構(gòu):
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進(jìn)行oltp基準(zhǔn)測試,表數(shù)量10,每表行數(shù)約50w(幾乎delete多少就會(huì)insert的多少),并且是非事務(wù)的只讀測試,持續(xù)60s,并發(fā)線程數(shù)12。
需要說明的選項(xiàng):
???? mysql-db=dbtest1a:測試使用的目標(biāo)數(shù)據(jù)庫,這個(gè)庫名要事先創(chuàng)建
???? –oltp-tables-count=10:產(chǎn)生表的數(shù)量
???? –oltp-table-size=500000:每個(gè)表產(chǎn)生的記錄行數(shù)
???? –oltp-dist-type=uniform:指定隨機(jī)取樣類型,可選值有 uniform(均勻分布), Gaussian(高斯分布), special(空間分布)。默認(rèn)是special
???? –oltp-read-only=off:表示不止產(chǎn)生只讀SQL,也就是使用oltp.lua時(shí)會(huì)采用讀寫混合模式。默認(rèn) off,如果設(shè)置為on,則不會(huì)產(chǎn)生update,delete,insert的sql。
???? –oltp-test-mode=nontrx:執(zhí)行模式,這里是非事務(wù)式的。可選值有simple,complex,nontrx。默認(rèn)是complex
???????????? simple:簡單查詢,SELECT c FROM sbtest WHERE id=N
???????????? complex (advanced transactional) :事務(wù)模式在開始和結(jié)束事務(wù)之前加上begin和commit, 一個(gè)事務(wù)里可以有多個(gè)語句,如點(diǎn)查詢、范圍查詢、排序查詢、更新、刪除、插入等,并且為了不破壞測試表的數(shù)據(jù),該模式下一條記錄刪除后會(huì)在同一個(gè)事務(wù)里添加一條相同的記錄。
???????????? nontrx (non-transactional) :與simple相似,但是可以進(jìn)行update/insert等操作,所以如果做連續(xù)的對比壓測,你可能需要重新cleanup,prepare。
???? –oltp-skip-trx=[on|off] :省略begin/commit語句。默認(rèn)是off
?????–rand-init=on:是否隨機(jī)初始化數(shù)據(jù),如果不隨機(jī)化那么初始好的數(shù)據(jù)每行內(nèi)容除了主鍵不同外其他完全相同
???? –num-threads=12: 并發(fā)線程數(shù),可以理解為模擬的客戶端并發(fā)連接數(shù)
???? –report-interval=10:表示每10s輸出一次測試進(jìn)度報(bào)告
???? –max-requests=0:壓力測試產(chǎn)生請求的總數(shù),如果以下面的max-time來記,這個(gè)值設(shè)為0
???? –max-time=120:壓力測試的持續(xù)時(shí)間,這里是2分鐘。
注意,針對不同的選項(xiàng)取值就會(huì)有不同的子選項(xiàng)。比如oltp-dist-type=special,就有比如oltp-dist-pct=1、oltp-dist-res=50兩個(gè)子選項(xiàng),代表有50%的查詢落在1%的行(即熱點(diǎn)數(shù)據(jù))上,另外50%均勻的(sample uniformly)落在另外99%的記錄行上。
再比如oltp-test-mode=nontrx時(shí), 就可以有oltp-nontrx-mode,可選值有select(默認(rèn)), update_key, update_nokey, insert, delete,代表非事務(wù)式模式下使用的測試sql類型。
以上代表的是一個(gè)只讀的例子,可以把num-threads依次遞增(16,36,72,128,256,512),或者調(diào)整my.cnf參數(shù),比較效果。另外需要注意的是,大部分mysql中間件對事務(wù)的處理,默認(rèn)都是把sql發(fā)到主庫執(zhí)行,所以只讀測試需要加上oltp-skip-trx=on來跳過測試中的顯式事務(wù)。
ps1: 只讀測試也可以使用share/tests/db/select.lua進(jìn)行,但只是簡單的point select。
ps2: 我在用sysbench壓的時(shí)候,在mysql后端會(huì)話里有時(shí)看到大量的query cache lock,如果使用的是uniform取樣,最好把查詢緩存關(guān)掉。當(dāng)然如果是做兩組性能對比壓測,因?yàn)槎际苓@個(gè)因素影響,關(guān)心也不大。
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一直沒有跟著我預(yù)期的去走,在mysql general log里面看到的sql記錄與complex模式相同。所以上面示例中的–oltp-test-mode=nontrx –oltp-nontrx-mode=select可以刪掉。
update:
原來sysbench 0.5版本去掉了這個(gè)選項(xiàng),因?yàn)樽髡哒跍?zhǔn)備1.0版本,所以也就沒有更新0.5版本的doc。網(wǎng)上的博客漫天飛,就沒有一個(gè)提出來的,也是沒誰了。
分析一下oltp.lua腳本內(nèi)容,可以清楚單個(gè)事務(wù)各操作的默認(rèn)比例: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這些選項(xiàng)去調(diào)整讀寫權(quán)重。
同只讀測試一樣,在atlas,mycat這類中間件測試中如果不加oltp-skip-trx=on,那么所有查詢都會(huì)發(fā)往主庫,但如果在有寫入的情況下使用–oltp-skip-trx=on跳過BEGIN和COMMIT,會(huì)出現(xiàn)問題:
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)
原因也很容易理解,每個(gè)線程將選擇一個(gè)隨機(jī)的表,不加事務(wù)的情況下高并發(fā)更新(插入)出現(xiàn)重復(fù)key的概率很大,但我們壓測不在乎這些數(shù)據(jù),所以需要跳過這個(gè)錯(cuò)誤–mysql-ignore-errors=1062,這個(gè)問題老外有出過打補(bǔ)丁的方案允許–mysql-ignore-duplicates=on,但作者新加入的忽略錯(cuò)誤碼這個(gè)功能已經(jīng)取代了它。mysql-ignore-errors選項(xiàng)是0.5版本加入的,但目前沒有文檔標(biāo)明。
這里不得不佩服老外的辦事效率和責(zé)任心,提個(gè)疑惑能立馬得到回復(fù),反觀國內(nèi),比如在atlas,mycat項(xiàng)目里提到問題到現(xiàn)在都沒人搭理。。。
2.3 只更新
如果基準(zhǔn)測試的時(shí)候,你只想比較兩個(gè)項(xiàng)目的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 ]
此時(shí)像oltp-read-only=off許多參數(shù)都失效了。需要說明的是這里 (非)索引更新,不是where條件根據(jù)索引去查找更新,而是更新索引列上的值。
三. 結(jié)果解讀
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數(shù)量 write: 1143728 //總update、insert、delete語句數(shù)量 other: 571864 //commit、unlock tables以及其他mutex的數(shù)量 total: 5718640 transactions: 285932 (2382.10 per sec.) //通常需要關(guān)注的數(shù)字(TPS) read/write requests: 5146776 (42877.85 per sec.) other operations: 571864 (4764.21 per sec.) ignored errors: 0 (0.00 per sec.) //忽略的錯(cuò)誤數(shù) reconnects: 0 (0.00 per sec.) General statistics: total time: 120.0334s //即max-time指定的壓測實(shí)際 total number of events: 285932 //總的事件數(shù),一般與transactions相同 total time taken by event execution: 15362.6623s response time: min: 17.60ms avg: 53.73ms //95%的語句的平均響應(yīng)時(shí)間 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
我們一般關(guān)注的用于繪圖的指標(biāo)主要有:
???? response time avg: 平均響應(yīng)時(shí)間。(后面的95%的大小可以通過–percentile=98的方式去更改)
???? transactions: 精確的說是這一項(xiàng)后面的TPS 。但如果使用了-oltp-skip-trx=on,這項(xiàng)事務(wù)數(shù)恒為0,需要用total number of events 去除以總時(shí)間,得到tps(其實(shí)還可以分為讀tps和寫tps)
???? read/write requests: 用它除以總時(shí)間,得到吞吐量QPS
???? 當(dāng)然還有一些系統(tǒng)層面的cpu,io,mem相關(guān)指標(biāo)
sysbench還可以對文件系統(tǒng)IO測試,CPU性能測試,以及內(nèi)存分配與傳輸速度測試,這里就不介紹了。
總結(jié)
sysbench的缺點(diǎn)就是,模擬的表結(jié)構(gòu)太簡單,不像tpcc-mysql那樣完整的事務(wù)系統(tǒng)。但對于性能壓測對比還是很有用的,因?yàn)閟ysbench使用的環(huán)境參數(shù)限制是一樣的。以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。