MySQL數據庫基准壓力測試工具之MySQLSlap使用實例 一、Mysqlslap介紹 mysqlslap是MySQL5.1之後自帶的benchmark基准測試工具,類似Apache Bench負載產生工具,生成schema,裝載數據,執行benckmark和查詢數據,語法簡單,靈活,容易使用。該工具可以模擬多個客戶端同時並發的向服務器發出查詢更新,給出了性能測試數據而且提供了多種引擎的性能比較。mysqlslap為mysql性能優化前後提供了直觀的驗證依據,筆者建議系統運維人員應該掌握一些常見的壓力測試工具,這樣才能較為准確的掌握線上系統能夠支撐的用戶流量上限及其抗壓性等問題。 www.2cto.com 二、使用方法介紹 可以使用mysqlslap --help來顯示使用方法: 1) --concurrency代表並發數量,多個可以用逗號隔開,例如:concurrency=10,50,100, 並發連接線程數分別是10、50、100個並發。 2) --engines代表要測試的引擎,可以有多個,用分隔符隔開。 3) --iterations代表要運行這些測試多少次。 4) --auto-generate-sql 代表用系統自己生成的SQL腳本來測試。 5)--auto-generate-sql-load-type 代表要測試的是讀還是寫還是兩者混合的(read,write,update,mixed) 6) --number-of-queries 代表總共要運行多少次查詢。每個客戶端運行的查詢數量可以用查詢總數/並發數來計算。 7) --debug-info 代表要額外輸出CPU以及內存的相關信息。 8) --number-int-cols :創建測試表的 int 型字段數量 9) --auto-generate-sql-add-autoincrement : 代表對生成的表自動添加auto_increment列,從5.1.18版本開始 10) --number-char-cols 創建測試表的 char 型字段數量。 11) --create-schema 測試的schema,MySQL中schema也就是database。 12) --query 使用自定義腳本執行測試,例如可以調用自定義的一個存儲過程或者sql語句來執行測試。 13) --only-print 如果只想打印看看SQL語句是什麼,可以用這個選項。 三、Demo實例 下面我們使用幾個demo實例來進行測試 1、Demo1: [root@localhost ~]# mysqlslap -uroot -p123abc --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate-sql-load-type=mixed --auto-generate-sql-add-autoincrement --engine=myisam --number-of-queries=10 --debug-info #備注本次測試以100個並發線程、測試1次,自動生成SQL測試腳本、讀、寫、更新混合測試、自增長字段、測試引擎為myisam、共運行10次查詢,輸出cpu資源信息 返回信息如下所示: rement --engine=myisam --number-of-queries=10 --debug-info Benchmark Running for engine myisam Average number of seconds to run all queries: 0.129 seconds Minimum number of seconds to run all queries: 0.107 seconds Maximum number of seconds to run all queries: 0.264 seconds Number of clients running queries: 100 Average number of queries per client: 0 User time 0.16, System time 0.25 Maximum resident set size 4624, Integral resident set size 0 Non-physical pagefaults 7346, Physical pagefaults 0, Swaps 0 Blocks in 0 out 0, Messages in 0 out 0, Signals 0 Voluntary context switches 27221, Involuntary context switches 4241 2、Demo2:指定數據庫和sql語句 mysqlslap -h192.168.202.84 -P3309 --concurrency=100 --iterations=1 --create-schema='mms_sdmtv' --query='select * from role;' --number-of-queries=10 --debug-info -uroot -p123abc #備注使用mysqlslap指定sql語句進行測試 3、Demo3:測試用例 [root@localhost /]# mysqlslap --concurrency=50,100,200 --iterations=20 --number-int-cols=4 --number-char-cols=35 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=read --engine=myisam,innodb --number-of-queries=200 --verbose --socket=/var/lib/mysql/mysql.sock -uroot -p123abc #系統腳本測試,增加int型 4列char 型35列,測試2種引擎myisam,innodb讀的性能,分別用50,100,200個客戶端對服務器進行測試總共200個查詢語句 執行20次查詢 Benchmark Running for engine myisam Average number of seconds to run all queries: 0.666 seconds Minimum number of seconds to run all queries: 0.223 seconds Maximum number of seconds to run all queries: 4.889 seconds Number of clients running queries: 50 Average number of queries per client: 4 Benchmark Running for engine myisam Average number of seconds to run all queries: 0.620 seconds Minimum number of seconds to run all queries: 0.231 seconds Maximum number of seconds to run all queries: 4.898 seconds Number of clients running queries: 100 Average number of queries per client: 2 Benchmark Running for engine myisam Average number of seconds to run all queries: 0.503 seconds Minimum number of seconds to run all queries: 0.257 seconds Maximum number of seconds to run all queries: 4.269 seconds Number of clients running queries: 200 Average number of queries per client: 1 Benchmark Running for engine innodb Average number of seconds to run all queries: 1.049 seconds Minimum number of seconds to run all queries: 0.244 seconds Maximum number of seconds to run all queries: 5.292 seconds Number of clients running queries: 50 Average number of queries per client: 4 Benchmark Running for engine innodb Average number of seconds to run all queries: 0.712 seconds Minimum number of seconds to run all queries: 0.246 seconds Maximum number of seconds to run all queries: 6.585 seconds Number of clients running queries: 100 Average number of queries per client: 2 Benchmark Running for engine innodb Average number of seconds to run all queries: 0.269 seconds Minimum number of seconds to run all queries: 0.175 seconds Maximum number of seconds to run all queries: 0.328 seconds Number of clients running queries: 200 Average number of queries per client: 1 4、自建SQL測試用例 mysqlslap --create=/yourpath/Test1.sql --query=/yourpath/Test2.sql --concurrency=50,100,200 --iterations=20 --engine=myisam,innodb -u root -p123abc #在設定的yourpath目錄下創建你的測試sql文Test1及Test2並進行50、100及200的模擬並發測試