MySQL從5.1.4版開始帶有一個壓力測試工具mysqlslap ,經過模擬多個併發客戶端訪問mysql來執行測試,使用起來很是的簡單。經過mysqlslap –help能夠得到可用的選項,這裏列一些主要的參數,更詳細的說明參考官方手冊 。html
–auto-generate-sql, -a
自動生成測試表和數據java–auto-generate-sql-load-type=type
測試語句的類型。取值包括:read,key,write,update和mixed(默認)。mysql–number-char-cols=N, -x N
自動生成的測試表中包含多少個字符類型的列,默認1sql–number-int-cols=N, -y N
自動生成的測試表中包含多少個數字類型的列,默認1數據庫–number-of-queries=N
總的測試查詢次數服務器–query=name,-q
使用自定義腳本執行測試,例如能夠調用自定義的一個存儲過程或者sql語句來執行測試。網絡–create-schema
測試的schema,MySQL中schema也就是database併發–commint=N
多少條DML後提交一次ide–compress, -C
若是服務器和客戶端支持都壓縮,則壓縮信息傳遞工具–concurrency=N, -c N
併發量,也就是模擬多少個客戶端同時執行select。可指定多個值,以逗號或者–delimiter參數指定的值作爲分隔符–engine=engine_name, -e engine_name
建立測試表所使用的存儲引擎,可指定多個–iterations=N, -i N
測試執行的迭代次數–detach=N
執行N條語句後斷開重連–debug-info, -T
打印內存和CPU的信息–only-print
只打印測試語句而不實際執行
測試的過程須要生成測試表,插入測試數據,這個mysqlslap能夠自動生成,默認生成一個mysqlslap的schema,若是已經存在則先 刪除,這裏要注意了,不要用–create-schema指定已經存在的庫,不然後果可能很嚴重。能夠用–only-print來打印實際的測試過程:
DROP SCHEMA IF EXISTS `mysqlslap`;
CREATE SCHEMA `mysqlslap`;
use mysqlslap;
CREATE TABLE `t1` (intcol1 INT(32) ,charcol1 VARCHAR(128));
INSERT INTO t1 VALUES (1804289383,'mxvtvmC9127qJNm06sGB8R92q2j7vTiiITRDGXM9ZLzkdekbWtmXKwZ2qG1llkRw5m9DHOFilEREk3q7oce8O3BEJC0woJsm6uzFAEynLH2xCsw1KQ1lT4zg9rdxBL');
...
SELECT intcol1,charcol1 FROM t1;
INSERT INTO t1 VALUES (364531492,'qMa5SuKo4M5OM7ldvisSc6WK9rsG9E8sSixocHdgfa5uiiNTGFxkDJ4EAwWC2e4NL1BpAgWiFRcp1zIH6F1BayPdmwphatwnmzdwgzWnQ6SRxmcvtd6JRYwEKdvuWr');
DROP SCHEMA IF EXISTS `mysqlslap`;
能夠看到最後由刪除一開始建立的schema的動做,整個測試完成後不會在數據庫中留下痕跡。假如咱們執行一次測試,分別50和100個併發,執行1000次總查詢,那麼:
Benchmark
Average number of seconds to run all queries: 0.375 seconds
Minimum number of seconds to run all queries: 0.375 seconds
Maximum number of seconds to run all queries: 0.375 seconds
Number of clients running queries: 50
Average number of queries per client: 20
Benchmark
Average number of seconds to run all queries: 0.453 seconds
Minimum number of seconds to run all queries: 0.453 seconds
Maximum number of seconds to run all queries: 0.453 seconds
Number of clients running queries: 100
Average number of queries per client: 10
User time 0.29, System time 0.11
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 4032, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 7319, Involuntary context switches 681
上結果能夠看出,50和100個併發分別獲得一次測試結果(Benchmark),併發數越多,執行完全部查詢的時間越長。爲了準確起見,能夠多迭代測試幾回:
Benchmark
Average number of seconds to run all queries: 0.380 seconds
Minimum number of seconds to run all queries: 0.377 seconds
Maximum number of seconds to run all queries: 0.385 seconds
Number of clients running queries: 50
Average number of queries per client: 20
Benchmark
Average number of seconds to run all queries: 0.447 seconds
Minimum number of seconds to run all queries: 0.444 seconds
Maximum number of seconds to run all queries: 0.451 seconds
Number of clients running queries: 100
Average number of queries per client: 10
User time 1.44, System time 0.67
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 17922, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 36796, Involuntary context switches 4093
測試同時不一樣的存儲引擎的性能進行對比:
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.200 seconds
Minimum number of seconds to run all queries: 0.188 seconds
Maximum number of seconds to run all queries: 0.210 seconds
Number of clients running queries: 50
Average number of queries per client: 20
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.238 seconds
Minimum number of seconds to run all queries: 0.228 seconds
Maximum number of seconds to run all queries: 0.251 seconds
Number of clients running queries: 100
Average number of queries per client: 10
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.375 seconds
Minimum number of seconds to run all queries: 0.370 seconds
Maximum number of seconds to run all queries: 0.379 seconds
Number of clients running queries: 50
Average number of queries per client: 20
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.443 seconds
Minimum number of seconds to run all queries: 0.440 seconds
Maximum number of seconds to run all queries: 0.447 seconds
Number of clients running queries: 100
Average number of queries per client: 10
User time 2.83, System time 1.66
Maximum resident set size 0, Integral resident set size 0
Non-physical pagefaults 34692, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 87306, Involuntary context switches 10326