MySQL的壓測工具

前言

    mysqlslap是mysql自帶的一款性能壓測工具,經過模擬多個併發客戶端訪問MySQL來進行壓力測試,同時提供了詳細的數據性能報告。此工具能夠自動生成測試表和數據,而且能夠模擬讀寫、混合讀寫、查詢等不一樣的使用場景,也可以很好的對比多個存儲引擎在相同環境的併發壓力下的性能差別。本文是使用的《分佈式服務架構——原理、設計與實踐》的案例。mysql

單線程測試

λ mysqlslap -a -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 0.141 seconds
        Minimum number of seconds to run all queries: 0.141 seconds
        Maximum number of seconds to run all queries: 0.141 seconds
        Number of clients running queries: 1
        Average number of queries per client: 0

    測試結果顯示單線程鏈接一次服務器須要141ms。sql

多線程測試

λ mysqlslap -a -c 100 -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 5.500 seconds
        Minimum number of seconds to run all queries: 5.500 seconds
        Maximum number of seconds to run all queries: 5.500 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

    能夠看到使用100個線程同時鏈接一次服務器須要5500ms,同單線程比,響應時間大幅度提高,這個不知道是什麼緣由,須要後續查詢下。數據庫

屢次測試求平均值

λ mysqlslap -a -i 10 -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 0.370 seconds
        Minimum number of seconds to run all queries: 0.140 seconds
        Maximum number of seconds to run all queries: 1.203 seconds
        Number of clients running queries: 1
        Average number of queries per client: 0

    最小響應時間和單線程測試同樣,可是平均響應時間仍是有差,個人數據庫可能被下毒了。服務器

讀操做的性能

λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=read -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 0.062 seconds
        Minimum number of seconds to run all queries: 0.062 seconds
        Maximum number of seconds to run all queries: 0.062 seconds
        Number of clients running queries: 10
        Average number of queries per client: 100

    讀操做在併發數是10,進行1000次的讀操做,每次的讀須要62ms。多線程

寫操做的性能

λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=write -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 11.875 seconds
        Minimum number of seconds to run all queries: 11.875 seconds
        Maximum number of seconds to run all queries: 11.875 seconds
        Number of clients running queries: 10
        Average number of queries per client: 100

    毫無疑問,我這臺機子的瓶頸在寫,平均寫時間要花掉11.875秒,蒼天啊,下面用混合的方式印證下。架構

混合操做性能

λ mysqlslap -a -c10 --number-of-queries=1000 --auto-generate-sql-load-type=mixed -uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Average number of seconds to run all queries: 5.844 seconds
        Minimum number of seconds to run all queries: 5.844 seconds
        Maximum number of seconds to run all queries: 5.844 seconds
        Number of clients running queries: 10
        Average number of queries per client: 100

    基本上,在混合模式下的性能和在多線程狀況下的性能是相同,推測在單線程的時候,是隨機讀寫,在多線程下是混合模式操做的,對單線程進行反覆的執行,印證了,單線程是隨機讀寫測試的。併發

不一樣數據庫引擎的性能

λ mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --engine=myisam,innodb --iterations=5
-uroot -proot
mysqlslap: [Warning] Using a password on the command line interface can be insecure.
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.115 seconds
        Minimum number of seconds to run all queries: 0.109 seconds
        Maximum number of seconds to run all queries: 0.125 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.121 seconds
        Minimum number of seconds to run all queries: 0.094 seconds
        Maximum number of seconds to run all queries: 0.156 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: 4.434 seconds
        Minimum number of seconds to run all queries: 3.797 seconds
        Maximum number of seconds to run all queries: 5.421 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: 3.759 seconds
        Minimum number of seconds to run all queries: 2.969 seconds
        Maximum number of seconds to run all queries: 4.250 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10

    上面現實了myisam和innodb兩種引擎的性能,具體差別須要查閱mysql的原理的書籍查找緣由。分佈式

總結

    數據庫做爲應用系統的性能的重要一環,掌握其數據庫的性能測試方法有助於判斷系統的性能瓶頸所在,已經系統架構中,數據庫的部署。工具

附錄

mysqlslap測試的參數的解釋,咱們能夠經過使用mysqlslap --help來顯示使用方法:性能

Default options are read from the following files in the given order:
/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf

--c表明併發數量,多個能夠用逗號隔開,concurrency=10,50,100, 併發鏈接線程數分別是十、50、100個併發。

--engines表明要測試的引擎,能夠有多個,用分隔符隔開。
--iterations表明要運行這些測試多少次。
-- a 指的是auto-generate-sql 表明用系統本身生成的SQL腳原本測試。
--auto-generate-sql-load-type 表明要測試的是讀仍是寫仍是二者混合的(read,write,update,mixed)
--number-of-queries 表明總共要運行多少次查詢。每一個客戶運行的查詢數量能夠用查詢總數/併發數來計算。
--debug-info 表明要額外輸出CPU以及內存的相關信息。
--number-int-cols :建立測試表的 int 型字段數量
--auto-generate-sql-add-autoincrement : 表明對生成的表自動添加auto_increment列,從5.1.18版本開始
--number-char-cols 建立測試表的 char 型字段數量。
--create-schema 測試的schema,MySQL中schema也就是database。
--query  使用自定義腳本執行測試,例如能夠調用自定義的一個存儲過程或者sql語句來執行測試。
--only-print 若是隻想打印看看SQL語句是什麼,能夠用這個選項。

本文最後參考了:https://blog.csdn.net/jjwen/article/details/51569234

相關文章
相關標籤/搜索