用mysql自帶工具mysqlslap對數據庫進行壓力測試

mysqlslap是mysql自帶的工具,不需要單獨安裝:

參數:

-concurrency 代表併發數量,多個可以用逗號隔開,concurrency=10,50,100, 併發連接線程數分別是10、50、100個併發。

--engines 代表要測試的引擎,可以有多個,用分隔符隔開。

--iterations 代表要運行這些測試多少次。

--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語句是什麼,可以用這個選項。

1,簡單用法

[root@Linux_Aolens_01 /home/aolens]# mysqlslap --user=root --password=password --auto-generate-sql
 
Benchmark
 
Average number of seconds to run all queries: 0.002 seconds
 
Minimum number of seconds to run all queries: 0.002 seconds
 
Maximum number of seconds to run all queries: 0.002 seconds
 
Number of clients running queries: 1
 
Average number of queries per client: 0


結果中各項含義:

  • Average number of ... 運行所有語句的平均秒數

  • Minimum number of ... 運行所有語句的最小秒數

  • Maximum number of ... 運行所有語句的最大秒數

  • Number of clients ... 客戶端數量

  • Average number of queries per client 每個客戶端運行查詢的平均數

 

2,添加併發

[root@Linux_Aolens_01 /home/aolens]# mysqlslap --user=root --password=password --auto-generate-sql --concurrency=100 --number-of-queries=1000
Benchmark
Average number of seconds to run all queries: 0.316 seconds
Minimum number of seconds to run all queries: 0.316 seconds
Maximum number of seconds to run all queries: 0.316 seconds
Number of clients running queries: 100
Average number of queries per client: 10

3,使用自己測試庫和測試語句

[root@Linux_Aolens_01 /home/aolens]# mysqlslap --user=root --password=password --concurrency=10 --number-of-queries=100 --create-schema=wordpress --query="SELECT * FROM wordpress.wp_posts;"
Benchmark
Average number of seconds to run all queries: 4.255 seconds
Minimum number of seconds to run all queries: 4.255 seconds
Maximum number of seconds to run all queries: 4.255 seconds
Number of clients running queries: 10
Average number of queries per client: 10

4,結合實際,對網站首頁所請求的數據庫連接做壓力測試

數據庫Mariadb 10.0.14

首先給數據庫安裝審計插件,並啓用

MariaDB [(none)]> show variables like '%audit%'
-> ;
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| server_audit_events | |
| server_audit_excl_users | |
| server_audit_file_path | server_audit.log |
| server_audit_file_rotate_now | OFF |
| server_audit_file_rotate_size | 1000000 |
| server_audit_file_rotations | 9 |
| server_audit_incl_users | |
| server_audit_logging | OFF |
| server_audit_mode | 0 |
| server_audit_output_type | file |
| server_audit_syslog_facility | LOG_USER |
| server_audit_syslog_ident | mysql-server_auditing |
| server_audit_syslog_info | |
| server_audit_syslog_priority | LOG_INFO |
+-------------------------------+-----------------------+
14 rows in set (0.00 sec)

發現已經安裝了,沒有安裝的MariaDB [(none)]> INSTALL PLUGIN server_audit SONAME 'server_audit.so';

命令行啓動審計功能:

       命令行啓用audit ,重啓後失效

MariaDB [(none)]> set global server_audit_file_rotate_size=1024*1024*1024;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> set global server_audit_events='query,table';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> set global server_audit_file_rotate_now=on;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> set global server_audit_logging='ON';
Query OK, 0 rows affected (0.00 sec)


刷新一下首頁查看審計日誌裏都有哪些SQL操作,對這些SQL進行壓測:

mysqlslap --user=root --password=password --concurrency=20 --number-of-queries=1000 --create-schema=wordpress --query=" \
SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'; \
SELECT option_value FROM wp_options WHERE option_name = 'a3_lz_google_api_key' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'a3_lz_google_api_key_enable' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = '_transient_timeout_a3_lz_google_api_key_status' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = '_transient_a3_lz_google_api_key_status' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'wordpress_api_key' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'onp_license_clipboard-p_w_picpaths' LIMIT 1; \
SELECT autoload FROM wp_options WHERE option_name = 'onp_license_clipboard-p_w_picpaths'; \
SELECT option_value FROM wp_options WHERE option_name = 'onp_version_check_clipboard-p_w_picpaths' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'ossdl_https' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'uninstall_plugins' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'a3_lazy_load_just_installed' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'akismet_comment_nonce' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'preload_cache_counter' LIMIT 1; \
SELECT option_value FROM wp_options WHERE option_name = 'rewrite_rules' LIMIT 1; \
......"
Benchmark
Average number of seconds to run all queries: 40.931 seconds
Minimum number of seconds to run all queries: 40.931 seconds
Maximum number of seconds to run all queries: 40.931 seconds
Number of clients running queries: 20
Average number of queries per client: 50


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章