mysql 壓力測試
一、測試工具:sysbench
sysbench支持的測試模式:
1、CPU運算性能 2、磁盤IO性能 3、調度程序性能 4、內存分配及傳輸速度 5、POSIX線程性能 6、數據庫性能(OLTP基準測試) 目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等幾種數據庫。
二、編譯安裝sysbench
下載鏈接:http://imysql.com/wp-content/uploads/2014/09/sysbench-0.4.12-1.1.tgz
[root@test package]# tar zxvf sysbench-0.4.12-1.1.tgz
[root@test sysbench-0.4.12-1.1]# ./autogen.sh
[root@test sysbench-0.4.12-1.1]# ./configure
[root@test sysbench-0.4.12-1.1]# make
[root@test sysbench-0.4.12-1.1]# make install
三、OLTP測試環境
初始化測試庫環境,對已經準備好的數據庫進行操作
首先需要在準備好的數據庫中創建一個名爲,sbtest的庫,不可更改必須叫這個名字,能看得懂lua腳本可以隨意更改,這個是個大前提
1、需要切到這個目錄下進行操作,如果不切目錄也可以使用絕對路徑,oltp.lua腳本路徑
[root@test db]# cd /data/package/sysbench-0.4.12-1.1/sysbench/tests/db
2、在sbtest庫中,創建10個測試表,每個測試表中100000條記錄(行),填充隨機生成的數據
[root@test db]# sysbench --mysql-host=192.168.4.100 --mysql-user=admin --mysql-password=FJRUDKEISLWO --test=oltp.lua --oltp_tables_count=10 --oltp-table-size=100000 --rand-init=on prepare
參數解釋:
--test=tests/db/oltp.lua 表示調用 ./tsysbench-0.4.12-1.1/sysbench/tests/db/oltp.lua 腳本進行 oltp模式測試 --oltp_tables_count=10 表示會生成 10 個測試表 --oltp-table-size=100000 表示每個測試表填充數據量爲 100000 --rand-init=on 表示每個測試表都是用隨機數據來填充的
他人經驗:
真實測試場景中,數據表建議不低於10個,單表數據量不低於500萬行,當然了,要視服務器硬件配置而定。如果是配備了SSD或者PCIE SSD這種高IOPS設備的話,則建議單表數據量最少不低於1億行。
四、進行OLTP測試
[root@test db]# sysbench --mysql-host=10.10.95.235 --mysql-user=admin --mysql-password=FJRUDKEISLWO --test=oltp.lua --oltp_tables_count=10 --oltp-table-size=10000000 --num-threads=10 --oltp-read-only=off --report-interval=10 --rand-type=uniform --max-time=120 --max-requests=0 --percentile=99 run >/data/log/sysbench.log
參數解釋:
--num-threads=8 # 表示發起 8個併發連接 --oltp-read-only=off #表示不要進行只讀測試,也就是會採用讀寫混合模式測試 --report-interval=10 #表示每10秒輸出一次測試進度報告 --rand-type=uniform #表示隨機類型爲固定模式,其他幾個可選隨機模式:uniform(固 定),gaussian(高斯),special(特定的),pareto(帕累託) --max-time=120 #表示最大執行時長爲 120秒 --max-requests=0 #表示總請求數爲 0,因爲上面已經定義了總執行時長,所以總請求數可以設定爲 0;也可以只設定總請求數,不設定最大執行時長 --percentile=99 #表示設定採樣比例,默認是 95%,即丟棄1%的長請求,在剩餘的99%裏取最大值
他人經驗:
真實測試場景中,建議持續壓測時長不小於30分鐘,否則測試數據可能不具參考意義。
五、測試結果解讀
sysbench 0.5: multi-threaded system evaluation benchmark Running the test with following options: Number of threads: 8 Report intermediate results every 10 second(s) Random number generator seed is 0 and will be ignored Threads started!-- 每10秒鐘報告一次測試結果,tps、每秒讀、每秒寫、99%以上的響應時長統計 [ 10s] threads: 8, tps: 1111.51, reads/s: 15568.42, writes/s: 4446.13, response time: 9.95ms (99%) [ 20s] threads: 8, tps: 1121.90, reads/s: 15709.62, writes/s: 4487.80, response time: 9.78ms (99%) [ 30s] threads: 8, tps: 1120.00, reads/s: 15679.10, writes/s: 4480.20, response time: 9.84ms (99%) [ 40s] threads: 8, tps: 1114.20, reads/s: 15599.39, writes/s: 4456.30, response time: 9.90ms (99%) [ 50s] threads: 8, tps: 1114.00, reads/s: 15593.60, writes/s: 4456.70, response time: 9.84ms (99%) [ 60s] threads: 8, tps: 1119.30, reads/s: 15671.60, writes/s: 4476.50, response time: 9.99ms (99%)OLTP test statistics: queries performed: read: 938224 -- 讀總數 write: 268064 -- 寫總數 other: 134032 -- 其他操作總數(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等) total: 1340320 -- 全部總數 transactions: 67016 (1116.83 per sec.) -- 總事務數(每秒事務數) deadlocks: 0 (0.00 per sec.) -- 發生死鎖總數 read/write requests: 1206288 (20103.01 per sec.) -- 讀寫總數(每秒讀寫次數) other operations: 134032 (2233.67 per sec.) -- 其他操作總數(每秒其他操作次數) General statistics: -- 一些統計結果 total time: 60.0053s -- 總耗時 total number of events: 67016 -- 共發生多少事務數 total time taken by event execution: 479.8171s -- 所有事務耗時相加(不考慮並行因素) response time: -- 響應時長統計 min: 4.27ms -- 最小耗時 avg: 7.16ms -- 平均耗時 max: 13.80ms -- 最長耗時 approx. 99 percentile: 9.88ms -- 超過99%平均耗時 Threads fairness: events (avg/stddev): 8377.0000/44.33 execution time (avg/stddev): 59.9771/0.00
最重要的參數指標:
總的事物數,每秒事務數,時間統計信息(最大、最小、平均、99%以上語句響應時間)
上述測試方式是在雲RDS(UDB),數據庫實例驗證的,我測試的時候是採用了迅達雲和ucloud雲進行的對比性測試,更容易拿出數據。
在測試環境,也就是自建的數據庫,會發生各種情況,只能依靠個人排查能力了。