MySQL壓力測試

Html代碼  收藏代碼
  1. MySQL5.1地的確提供了好多有力的工具來幫助我們DBA進行數據庫管理。  
  2. 現在看一下這個壓力測試工具mysqlslap  
  3. 關於他的選項手冊上以及--help介紹的很詳細。  
  4. 我解釋一下一些常用的選項。  
  5. --concurrency 併發量,也就是模擬多少個客戶端同時執行select。可指定多個值,以逗號或者–delimiter參數指定的值做爲分隔符。  
  6. --engines 代表要測試的引擎,可以有多個,用分隔符隔開。  
  7. --iterations 代表要運行這些測試多少次。  
  8. --auto-generate-sql 代表用系統自己生成的SQL腳本來測試。  
  9. --auto-generate-sql-load-type 代表要測試的是讀還是寫還是兩者混合的(read,write,update,mixed(默認))  
  10. --number-of-queries 代表總共要運行多少次查詢。每個客戶運行的查詢數量可以用查詢總數/併發數來計算。比如倒數第二個結果2=200/100。  
  11. --debug-info 代表要輸出一些調試相關信息。  
  12. --number-int-cols 自動生成的測試表中包含多少個數字類型的列,默認1。  
  13. --number-char-cols 自動生成的測試表中包含多少個字符類型的列,默認1。  
  14. --create-schema  測試的schema,MySQL中schema也就是database。  
  15. --query 使用自定義腳本執行測試,例如可以調用自定義的一個存儲過程或者sql語句來執行測試。  
  16. --only-print 如果只想打印看看SQL語句是什麼,可以用這個選項。  
  17. commint=N  多少條DML後提交一次。  
  18. –compress, -C 如果服務器和客戶端支持都壓縮,則壓縮信息傳遞。  
  19. detach=N 執行N條語句後斷開重連。  
  20.   
  21.   
  22. 現在來看一些我測試的例子。  
  23.   
  24. 1、用自帶的SQL腳本來測試。  
  25. MySQL版本爲5.1.23  
  26. [root@localhost ~]# mysqlslap --defaults-file=/usr/local/mysql-maria/my.cnf --concurrency=50,100,200 --iterations=1 --number-int-cols=4 --number-char-cols=35 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=200 --debug-info -uroot -p1 -S/tmp/mysql_3310.sock  
  27.   
  28. Benchmark  
  29.         Running for engine myisam  
  30.         Average number of seconds to run all queries: 0.063 seconds  
  31.         Minimum number of seconds to run all queries: 0.063 seconds  
  32.         Maximum number of seconds to run all queries: 0.063 second  
  33.         Number of clients running queries: 50  
  34.         Average number of queries per client: 4  
  35.   
  36. Benchmark  
  37.         Running for engine myisam  
  38.         Average number of seconds to run all queries: 0.070 seconds  
  39.         Minimum number of seconds to run all queries: 0.070 seconds  
  40.         Maximum number of seconds to run all queries: 0.070 seconds  
  41.         Number of clients running queries: 100  
  42.         Average number of queries per client: 2  
  43.   
  44. Benchmark  
  45.        Running for engine myisam  
  46.         Average number of seconds to run all queries: 0.092 seconds  
  47.         Minimum number of seconds to run all queries: 0.092 seconds  
  48.         Maximum number of seconds to run all queries: 0.092 seconds  
  49.        Number of clients running queries: 200  
  50.         Average number of queries per client:   
  51.   
  52. Benchmark  
  53.         Running for engine innodb  
  54.         Average number of seconds to run all queries: 0.115 seconds  
  55.         Minimum number of seconds to run all queries: 0.115 seconds  
  56.         Maximum number of seconds to run all queries: 0.115 seconds  
  57.         Number of clients running queries: 50  
  58.         Average number of queries per client: 4  
  59.   
  60. Benchmark  
  61.         Running for engine innodb  
  62.         Average number of seconds to run all queries: 0.134 seconds  
  63.         Minimum number of seconds to run all queries: 0.134 seconds  
  64.         Maximum number of seconds to run all queries: 0.134 seconds  
  65.         Number of clients running queries: 100  
  66.         Average number of queries per client: 2  
  67.   
  68. Benchmark  
  69.         Running for engine innodb  
  70.         Average number of seconds to run all queries: 0.192 seconds  
  71.         Minimum number of seconds to run all queries: 0.192 seconds  
  72.         Maximum number of seconds to run all queries: 0.192 seconds  
  73.         Number of clients running queries: 200  
  74.         Average number of queries per client: 1  
  75.   
  76. User time 0.06, System time 0.15  
  77. Maximum resident set size 0, Integral resident set size 0  
  78. Non-physical pagefaults 5803, Physical pagefaults 0, Swaps 0  
  79. Blocks in 0 out 0, Messages in 0 out 0, Signals 0  
  80. Voluntary context switches 8173, Involuntary context switches 528  
  81.   
  82.   
  83. 我來解釋一下結果的含義。  
  84. 拿每個引擎最後一個Benchmark示例。  
  85. 對於INNODB引擎,200個客戶端同時運行這些SQL語句平均要花0.192秒。相應的MYISAM爲0.092秒。  
  86.   
  87. 2、用我們自己定義的SQL 腳本來測試。  
  88. 這些數據在另外一個MySQL實例上。版本爲5.0.45  
  89. 先看一下這兩個表的相關數據。  
  90. 1)、總記錄數。  
  91. mysql> select table_rows as rows from information_schema.tables where table_schema='t_girl' and table_name='article';  
  92. +--------+  
  93. | rows   |  
  94. +--------+  
  95. | 296693 |  
  96. +--------+  
  97. 1 row in set (0.01 sec)  
  98.   
  99. mysql> select table_rows as rows from information_schema.tables where table_schema='t_girl' and table_name='category';  
  100. +------+  
  101. | rows |  
  102. +------+  
  103. | 113  |  
  104. +------+  
  105. 1 row in set (0.00 sec)  
  106.   
  107.   
  108. 2)、總列數。  
  109. mysql> select count(*) as column_total from information_schema.columns where table_schema = 't_girl' and table_name = 'article';  
  110. +--------------+  
  111. | column_total |  
  112. +--------------+  
  113. | 32           |  
  114. +--------------+  
  115. 1 row in set (0.01 sec)  
  116.   
  117. mysql> select count(*) as column_total from information_schema.columns where table_schema = 't_girl' and table_name = 'category';  
  118. +--------------+  
  119. | column_total |  
  120. +--------------+  
  121. | 9            |   
  122. +--------------+  
  123. 1 row in set (0.01 sec)  
  124.   
  125.   
  126. 3)、調用的存儲過程  
  127. DELIMITER $$  
  128.   
  129. DROP PROCEDURE IF EXISTS `t_girl`.`sp_get_article`$$  
  130.   
  131. CREATE DEFINER=`root`@`%` PROCEDURE `sp_get_article`(IN f_category_id int,  
  132. IN f_page_size int, IN f_page_no int  
  133. )  
  134.   
  135.   set @stmt = 'select a.* from article as a inner join ';  
  136. set @stmt = concat(@stmt,'(select a.aid from article as a ');  
  137. if f_category_id != 0 then  
  138.            set @stmt = concat(@stmt,' inner join (select cid from category where cid = ',f_category_id,' or parent_id = ',f_category_id,') as b on a.category_id = b.cid');  
  139.   end if;  
  140.   if f_page_size >0 && f_page_no > 0 then  
  141.     set @stmt = concat(@stmt,' limit ',(f_page_no-1)*f_page_size,',',f_page_size);  
  142.   
  143.   end if;   
  144.   
  145.   set @stmt = concat(@stmt,') as b on (a.aid = b.aid)');  
  146.   prepare s1 from @stmt;  
  147.   execute s1;  
  148.   deallocate prepare s1;  
  149.   set @stmt = NULL;  
  150. END$$  
  151.   
  152. DELIMITER ;  
  153. 4)、我們用mysqlslap來測試  
  154. 以下得這個例子代表用mysqlslap來測試併發數爲25,50,100的調用存儲過程,並且總共調用5000次。  
  155. [root@localhost ~]# mysqlslap --defaults-file=/usr/local/mysql-maria/my.cnf --concurrency=25,50,100 --iterations=1 --query='call t_girl.sp_get_article(2,10,1);' --number-of-queries=5000 --debug-info -uroot -p -S/tmp/mysql50.sock  
  156. Enter password:   
  157. Benchmark  
  158.         Average number of seconds to run all queries: 3.507 seconds  
  159.         Minimum number of seconds to run all queries: 3.507 seconds  
  160.         Maximum number of seconds to run all queries: 3.507 seconds  
  161.         Number of clients running queries: 25  
  162.         Average number of queries per client: 200  
  163. 平均每個併發運行200個查詢用了3.507秒。  
  164. Benchmark  
  165.         Average number of seconds to run all queries: 3.742 seconds  
  166.         Minimum number of seconds to run all queries: 3.742 seconds  
  167.         Maximum number of seconds to run all queries: 3.742 seconds  
  168.         Number of clients running queries: 50  
  169.         Average number of queries per client: 100  
  170.   
  171. Benchmark  
  172.         Average number of seconds to run all queries: 3.697 seconds  
  173.         Minimum number of seconds to run all queries: 3.697 seconds  
  174.         Maximum number of seconds to run all queries: 3.697 seconds  
  175.         Number of clients running queries: 100  
  176.         Average number of queries per client: 50  
  177.   
  178.   
  179. User time 0.87, System time 0.33  
  180. Maximum resident set size 0, Integral resident set size 0  
  181. Non-physical pagefaults 1877, Physical pagefaults 0, Swaps 0  
  182. Blocks in 0 out 0, Messages in 0 out 0, Signals 0  
  183. Voluntary context switches 27218, Involuntary context switches 3100  
  184.   
  185.   
  186. 看一下SHOW PROCESSLIST 結果  
  187. mysql> show processlist;  
  188. +------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+  
  189. |Id   | User | Host               | db                 | Command | Time | State              |Info                                                                                                |  
  190. +------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+  
  191. …………  
  192. |3177 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  193. |3178 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  194. |3179 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  195. |3181 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  196. |3180 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  197. |3182 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  198. |3183 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  199. |3187 | root | %                  | t_girl             | Query   |     0| removing tmp table | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  200. |3186 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  201. |3194 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  202. |3203 | root | %                  | t_girl             | Query   |     0| NULL               | deallocate prepares1                                                                               |   
  203.   
  204. |3221 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |  
  205. |3222 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  206. |3223 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  207. |3224 | root | %                  | t_girl             | Query   |     0| removing tmp table | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  208. |3225 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  209. |3226 | root | %                  | t_girl             | Query   |     0| NULL               | select a.* from article as a inner join (selecta.aid from article as a  inner join (select cid from |   
  210. +------+------+--------------------+--------------------+---------+-------+--------------------+------------------------------------------------------------------------------------------------------+  
  211. rows in set (0.00 sec)  
  212.   
  213. 上面的測試語句其實也可以這樣寫  
  214. [root@localhost ~]# mysqlslap --defaults-file=/usr/local/mysql-maria/my.cnf --concurrency=25,50,100 --iterations=1 --create-schema='t_girl' --query='call sp_get_article(2,10,1);' --number-of-queries=5000 --debug-info -uroot -p -S/tmp/mysql50.sock  
  215.   
  216. 小總結一下。  
  217. mysqlslap對於模擬多個用戶同時對MySQL發起“進攻”提供了方便。同時詳細的提供了“高負荷攻擊MySQL”的詳細數據報告。  
  218. 而且如果你想對於多個引擎的性能。這個工具再好不過了。  
發佈了166 篇原創文章 · 獲贊 208 · 訪問量 132萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章