標籤:資料庫 update 壓力測試
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,簡單用法
[[email protected]_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,添加並發
[[email protected]_Aolens_01 /home/aolens]# mysqlslap --user=root --password=password --auto-generate-sql --concurrency=100 --number-of-queries=1000BenchmarkAverage number of seconds to run all queries: 0.316 secondsMinimum number of seconds to run all queries: 0.316 secondsMaximum number of seconds to run all queries: 0.316 secondsNumber of clients running queries: 100Average number of queries per client: 10
3,使用自己測試庫和測試語句
[[email protected]_Aolens_01 /home/aolens]# mysqlslap --user=root --password=password --concurrency=10 --number-of-queries=100 --create-schema=wordpress --query="SELECT * FROM wordpress.wp_posts;"BenchmarkAverage number of seconds to run all queries: 4.255 secondsMinimum number of seconds to run all queries: 4.255 secondsMaximum number of seconds to run all queries: 4.255 secondsNumber of clients running queries: 10Average 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-images‘ LIMIT 1; SELECT autoload FROM wp_options WHERE option_name = ‘onp_license_clipboard-images‘; SELECT option_value FROM wp_options WHERE option_name = ‘onp_version_check_clipboard-images‘ 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; ......"BenchmarkAverage number of seconds to run all queries: 40.931 secondsMinimum number of seconds to run all queries: 40.931 secondsMaximum number of seconds to run all queries: 40.931 secondsNumber of clients running queries: 20Average number of queries per client: 50
本文出自 “aolens·程超” 部落格,請務必保留此出處http://aolens.blog.51cto.com/7021142/1901557
用mysql內建工具mysqlslap對資料庫進行壓力測試