MySQL数据库之压力测试
目錄
引言
一、MySQL自帶的壓力測試工具——Mysqlslap
1、更改其默認的最大連接數
2、進行壓力測試
二、使用第三方工具sysbench進行壓力測試
1、簡介
2、查看sysbench工具的幫助選項?
3、sysbench測試MySQL數據庫性能
①準備測試數據
引言
常用的mysql壓測工具有兩個
1、Mysql自帶的壓力測試工具——Mysqlslap
2、第三方工具sysbench進行壓力測試
一、MySQL自帶的壓力測試工具——Mysqlslap
mysqlslap是mysql自帶的基準測試工具,該工具查詢數據,語法簡單,靈活容易使用.該工具可以模擬多個客戶端同時并發的向服務器發出查詢更新,給出了性能測試數據而且提供了多種引擎的性能比較。mysqlslap為mysql性能優化前后提供了直觀的驗證依據,系統運維和DBA人員應該掌握一些常見的壓力測試工具,才能準確的掌握線上數據庫支撐的用戶流量上限及其抗壓性等問題。
1、更改其默認的最大連接數
再對mysql進行壓力測試之前,需要更改其默認的最大連接數
vim /etc/my.cnf systemctl restart mysqld.service進入mysql中查看最大連接數
2、進行壓力測試
注意:mysqlslap好像會與default-character-set=utf8沖突
建議測試前先my.cnf文件中的default-character-set=utf8注釋掉
mysqlslap --defaults-file=/etc/my.cnf --concurrency=100,200 --iterations=1 --number-int-cols=20 --number-char-cols=30 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=2000 -uroot -pabc123 --verbose
#模擬測試兩次讀寫并發,第一次100,第二次200,自動生成SQL腳本,測試表包含20個init字段,30個char字段,每次執行2000查詢請求。測試引擎分別是myisam,innodb。
–defaults-file=/etc/my.cnf 從/etc/my.cnf文件中讀取默認選項
–concurrency=100,200 測試并發的線程數/客戶端數,第一次100,第二次200
–iterations=1 指定測試重復次數1次
–number-int-cols=20 指定測試表中int列的數量
–number-char-cols=30 指定測試表中varchar列的數量
–auto-generate-sql 自動生成 SQL 語句
–auto-generate-sql-add-autoincrement 在自動生成的表中添加自增列
–auto-generate-sql-load-type=mixed 測試的負載模型,包括 mixed, update, write, key,read,默認是 mix
–engine=myisam,innodb 指定建表時的存儲引擎
–number-of-queries=2000 指定每個線程執行的 SQL 語句數量上限(不精確)
–verbose 更詳細的輸出
Average number of seconds to run all queries #運行所有查詢的平均秒數
Minimum number of seconds to run all queries #運行所有查詢所需的最小秒數
Maximum number of seconds to run all queries #運行所有查詢的最大秒數
Number of clients running queries #運行查詢的客戶端數量
Average number of queries per client #每個客戶端的平均查詢數
看來這點壓力不是問題,壓力測試大膽一點
mysqlslap --defaults-file=/etc/my.cnf --concurrency=200,400 --iterations=1 --number-int-cols=50 --number-char-cols=60 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=mixed --engine=myisam,innodb --number-of-queries=80000 -uroot -pabc123 --verbose
壓力調大測試,發現明顯現在查詢時間明顯變慢了
壓力也不要調太大,會出現下面的場景
二、使用第三方工具sysbench進行壓力測試
1、簡介
系統平臺/自述文件在碩士 ·阿科皮托夫/系統臺 ·吉特哈布 (github.com)
https://github.com/akopytov/sysbench/blob/master/README.md#general-command-line-options
sysbench]是一款開源的多線程性能測試工具,可以執行CPU/內存/線程/IO/數據庫等方面的性能測試。數據庫目前支持MySQL/Oracle/PostgreSQL。本文只是簡單演示一下幾種測試的用法,后續準備利用sysbench來對MySQL進行一系列的測試。具體的一些參數設置,需要根據不同的測試要求來進行調整。
CPU 運算性能測試
磁盤 IO 性能測試
調度程序性能測試
內存分配及傳輸速度測試
POSIX 線程性能測試
數據庫性能測試(OLTP 基準測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 腳本執行,例如 oltp_read_only.lua 腳本執行只讀測試)
sysbench 還可以通過運行命令時指定自己的 Lua 腳本來自定義測試。
默認支持mysql
yum -y install epel-release #安裝第三方epel源
yum -y install sysbench #安裝sysbench工具
sysbench --version #確定工具已安裝
sysbench 1.0.17
2、查看sysbench工具的幫助選項?
testname 是內置測試的可選名稱(例如 ,,等),或捆綁的 Lua 腳本之一的名稱(例如),或自定義 Lua 腳本的路徑。如果在命令行上沒有指定測試名稱(因此,也沒有命令,因為在這種情況下,它將被解析為測試名稱),或者測試名稱是破折號(“”),則 sysbench 期望 Lua 腳本在其標準輸入上執行。
command 是一個可選參數,它將通過系統平臺傳遞到使用測試名稱指定的內置測試或腳本。命令定義測試必須執行的操作。可用命令的列表取決于特定的測試。某些測試還實現自己的自定義命令。
以下是對典型測試命令及其用途的描述:
prepare:為那些需要它們的測試執行準備操作,例如,在磁盤上為測試創建必要的文件,或為數據庫基準測試填充測試數據庫。
run:運行使用測試名稱參數指定的實際測試。此命令由所有測試提供。
cleanup:在創建臨時數據的測試中,刪除測試運行后的臨時數據。
help:顯示使用測試名參數指定的測試的用法信息。這包括測試提供的命令的完整列表,因此應使用它來獲取可用命令。
optiond是以 開頭的零個或多個命令行選項的列表。與命令一樣,該命令應用于描述特定測試提供的可用選項。
有關 sysbench 本身提供的常規選項的說明,請參閱常規命令行選項。
可用于顯示常規命令行語法和選項。sysbench --help
[root@liji ~]sysbench --help Usage:sysbench [options]... [testname] [command]Commands implemented by most tests: prepare run cleanup helpGeneral options: #普通功能--threads=N 要使用的線程數,默認 1 個 [1]--events=N 最大允許的事件個數 [0]--time=N 最大的總執行時間,以秒為單位 [10]--forced-shutdown=STRING 在 --time 時間限制到達后,強制關閉之前等待的秒數,默認“off”禁用--thread-stack-size=SIZE 每個線程的堆棧大小 [64K]--rate=N 平均傳輸速率。0 則無限制 [0]--report-interval=N 以秒為單位定期報告具有指定間隔的中間統計信息 0 禁用中間報告 [0]--report-checkpoints=[LIST,...] 轉儲完整的統計信息并在指定的時間點重置所有計數器。參數是一個逗號分隔的值列表,表示從測試開始經過這個時間量時必須執行報告檢查點(以秒為單位)。報告檢查點默認關閉。 []--debug[=on|off] 打印更多 debug 信息 [off]--validate[=on|off] 盡可能執行驗證檢查 [off]--help[=on|off] 顯示幫助信息并退出 [off]--version[=on|off] 顯示版本信息并退出 [off]--config-file=FILENAME 包含命令行選項的文件--tx-rate=N 已棄用——rate [0]的別名--max-requests=N 已棄用 --events [0]的別名--max-time=N 已棄用 --time [0]的別名--num-threads=N 已棄用 --threads [1]的別名Pseudo-Random Numbers Generator options: #偽隨機數生成器選項--rand-type=STRING 隨機數分布 {uniform,gaussian,special,pareto} 默認使用[special]--rand-spec-iter=N 生成數字的迭代次數[12]--rand-spec-pct=N 被視為“特殊”(用于特殊分布)的值的百分比[1]--rand-spec-res=N 使用的“特殊”值的百分比(用于特殊分布)[75]--rand-seed=N 隨機數生成器的種子。0時,當前時間作為RNG的種子。[0]--rand-pareto-h=N pareto分布的參數h [0.2]Log options: #日志選項--verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]--percentile=N percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]--histogram[=on|off] print latency histogram in report [off]General database options:--db-driver=STRING specifies database driver to use ('help' to get list of available drivers) [mysql]--db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]--db-debug[=on|off] print database-specific debug information [off]Compiled-in database drivers:mysql - MySQL driverpgsql - PostgreSQL drivermysql options:--mysql-host=[LIST,...] MySQL server host [localhost]--mysql-port=[LIST,...] MySQL server port [3306]--mysql-socket=[LIST,...] MySQL socket--mysql-user=STRING MySQL user [sbtest]--mysql-password=STRING MySQL password []--mysql-db=STRING MySQL database name [sbtest]--mysql-ssl[=on|off] use SSL connections, if available in the client library [off]--mysql-ssl-cipher=STRING use specific cipher for SSL connections []--mysql-compression[=on|off] use compression, if available in the client library [off]--mysql-debug[=on|off] trace all client library calls [off]--mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]--mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off]pgsql options:--pgsql-host=STRING PostgreSQL server host [localhost]--pgsql-port=N PostgreSQL server port [5432]--pgsql-user=STRING PostgreSQL user [sbtest]--pgsql-password=STRING PostgreSQL password []--pgsql-db=STRING PostgreSQL database name [sbtest]Compiled-in tests:fileio - File I/O testcpu - CPU performance testmemory - Memory functions speed testthreads - Threads subsystem performance testmutex - Mutex performance testSee 'sysbench <testname> help' for a list of options for each test.mysql options:
–mysql-host=[LIST,…] MySQL server host [localhost] #MySQL服務器主機
–mysql-port=[LIST,…] MySQL server port [3306] #MySQL服務器端口
–mysql-socket=[LIST,…] MySQL socket #MySQL的套接字
–mysql-user=STRING MySQL user [sbtest] #MySQL用戶
–mysql-password=STRING MySQL password [] #MySQL密碼
–mysql-db=STRING MySQL database name [sbtest] #MySQL數據庫名稱
–mysql-ssl[=on|off] use SSL connections, if available in the client library [off] #是否開啟ssl連接
–mysql-ssl-cipher=STRING use specific cipher for SSL connections [] #對SSL連接使用特定的密碼
–mysql-compression[=on|off] use compression, if available in the client library [off] #是否開啟壓縮功能
–mysql-debug[=on|off] trace all client library calls [off] #是否跟蹤所有客戶端庫調用
–mysql-ignore-errors=[LIST,…] list of errors to ignore, or “all” [1213,1020,1205] #要忽略或“全部”的錯誤列表 --mysql-dry-run[=on|off] Dry run, pretend that all MySQL client API calls are successful without executing them [off] #模擬運行,假設所有的MySQL客戶端API調用都是成功的,但沒有執行它們。
3、sysbench測試MySQL數據庫性能
①準備測試數據
#查看sysbench自帶的lua腳本使用方法 sysbench /usr/share/sysbench/oltp_common.lua help #必須創建sbtest庫,sbtest事sysbench默認使用的庫名 mysqladmin -uroot -pabc123 create sbtest; #然后,準備測試所用的表,這些測試表放在測試庫sbtest中。這里使用的lua腳本為/usr/share/sysbench/oltp_common.lua sysbench --mysql-host=127.0.0.1 \ --mysql-port=3306 \ --mysql-user=root \ --mysql-password=abc123 \ /usr/share/sysbench/oltp_common.lua \ --tables=10 \ --table_size=100000 \ prepare #其中--tables=10表示創建10個測試表, #--table_size=100000表示每個表中插入10W行數據, #prepare表示這是準備數的過程。 --auto_inc[=on|off] id列默認自增,默認[on] --create_secondary[=on|off] 索引不是主鍵索引而是二級索引,默認[off] --delete_inserts=N 每個事務的delete、insert的組合數,默認[1] --distinct_ranges=N 單個事務中執行范圍distinct查詢的數量,默認[1] --index_updates=N 單個事務中執行索引更新的操作的數量,默認[1] --mysql_storage_engine=STRING mysql的存儲引擎,默認是[innodb] --non_index_updates=N 單個事務中執行索引更新的操作的數量,默認[1] --order_ranges=N 單個事務中執行范圍order by查詢的數量,默認[1] --pgsql_variant=STRING 在運行pgsql的驅動程序時,使用了pgsql的變體,當前僅僅支持的variant是'redshift',當啟動時,將自動禁用create_secondary為不可用,并將delete_insert設置為0 --point_selects=N 單個事務中select查詢的數量,默認 [10] --range_selects[=on|off] 啟用/禁用所有SELECT查詢[on] --secondary[=on|off] 使用二級索引代替逐漸,默認[off] --range_size=N 查詢范圍。默認是100 --simple_ranges=N 每個事務的簡單查詢范圍select,默認[1] --skip_trx[=on|off] 語句以bigin/commit開始結尾,默認[off] --sum_ranges=N 每個事務的select sum的查詢總量,默認[1] --table_size=N 指定表的記錄大小(行的數量),默認[10000] --tables=N 創建的表數量,默認[1] sysbench --threads=4 \ --time=20 \ --report-interval=5 \ --mysql-host=127.0.0.1 \ --mysql-port=3306 \ --mysql-user=root \ --mysql-password=abc123 \ /usr/share/sysbench/oltp_read_write.lua \ --tables=100 \ --table_size=1000000 \ run [root@liji ~]# sysbench --threads=4 \ #使用4個線程 > --time=20 \ #最大的總執行時間20秒 > --report-interval=5 \ # > --mysql-host=127.0.0.1 \ > --mysql-port=3306 \ > --mysql-user=root \ > --mysql-password=abc123 \ > /usr/share/sysbench/oltp_read_write.lua \ > --tables=100 \ > --table_size=1000000 \ > run sysbench 1.0.17 (using system LuaJIT 2.0.4)Running the test with following options: Number of threads: 4 #并發數4 Report intermediate results every 5 second(s) #每5秒報告一次測試結果 Initializing random number generator from current timeInitializing worker threads...Threads started! #下面是每5秒返回一次的結果,統計的指針包括: 線程數 thds 4 tps(每秒事務數) qos(每秒查詢數) 每秒的讀/寫/其他次數、延遲、每秒錯誤數、每秒重連次數 [ 5s ] thds: 4 tps: 253.12 qps: 5066.89 (r/w/o: 3547.34/1012.50/507.05) lat (ms,95%): 34.33 err/s: 0.00 reconn/s: 0 [ 10s ] thds: 4 tps: 335.20 qps: 6705.37 (r/w/o: 4693.58/1341.59/670.20) lat (ms,95%): 28.67 err/s: 0.00 reconn/s: [ 15s ] thds: 4 tps: 343.27 qps: 6869.17 (r/w/o: 4809.56/1372.87/686.74) lat (ms,95%): 28.16 err/s: 0.00 reconn/s: [ 20s ] thds: 4 tps: 336.91 qps: 6738.97 (r/w/o: 4716.72/1348.44/673.82) lat (ms,95%): 28.16 err/s: 0.00 reconn/s: SQL statistics:queries performed:read: 88858 #執行的讀操作數量 write: 25388 #執行的寫操作數量other: 12694 #執行的其它操作數量total: 126940 #總執行的操作數量transactions: 6347 (316.93 per sec.)#總事務數(每秒事務數)queries: 126940 (6338.58 per sec.)#查詢總數ignored errors: 0 (0.00 per sec.)#忽略錯誤數reconnects: 0 (0.00 per sec.)#重新連接數General statistics:total time: 20.0254s# 總消耗時間total number of events: 6347 # 總請求數量(讀、寫、其它)Latency (ms): #等待時間min: 3.92 #最小耗時avg: 12.60 #平均耗時max: 61.54 #最長耗時95th percentile: 29.72 #超過99%平均耗時sum: 80002.95 #總時間Threads fairness: #并發統計events (avg/stddev): 1586.7500/6.26 #總處理事件數/標準偏差execution time (avg/stddev): 20.0007/0.01 #平均處理事件時間/標準偏差sysbench --threads=4
–time=20
–report-interval=5
–mysql-host=127.0.0.1
–mysql-port=3306
–mysql-user=root
–mysql-password=abc123
/usr/share/sysbench/oltp_read_write.lua
–tables=10
–table_size=100000
cleanup #刪除剛剛的數據
總結
以上是生活随笔為你收集整理的MySQL数据库之压力测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 装修小常识(200条)
- 下一篇: POI 实现Excel导入数据库