MYSQL之压测
MYSQL壓測
常用的mysql壓測工具有兩個
1、Mysql自帶的壓力測試工具——Mysqlslap
2、第三方工具sysbench進(jìn)行壓力測試
一、MySQL自帶的壓力測試工具——Mysqlslap
mysqlslap是mysql自帶的基準(zhǔn)測試工具,該工具查詢數(shù)據(jù),語法簡單,靈活容易使用.該工具可以模擬多個客戶端同時并發(fā)的向服務(wù)器發(fā)出查詢更新,給出了性能測試數(shù)據(jù)而且提供了多種引擎的性能比較。mysqlslap為mysql性能優(yōu)化前后提供了直觀的驗證依據(jù),系統(tǒng)運維和DBA人員應(yīng)該掌握一些常見的壓力測試工具,才能準(zhǔn)確的掌握線上數(shù)據(jù)庫支撐的用戶流量上限及其抗壓性等問題。
1、更改其默認(rèn)的最大連接數(shù)
再對mysql進(jìn)行壓力測試之前,需要更改其默認(rèn)的最大連接數(shù),
vim /etc/my.cnf
systemctl restart mysqld.service
進(jìn)入mysql中查看最大連接數(shù)
進(jìn)行壓力測試
注意: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
#模擬測試兩次讀寫并發(fā),第一次100,第二次200,自動生成SQL腳本,測試表包含20個init字段,30個char字段,每次執(zhí)行2000查詢請求。測試引擎分別是myisam,innodb。
–defaults-file=/etc/my.cnf 從/etc/my.cnf文件中讀取默認(rèn)選項
–concurrency=100,200 測試并發(fā)的線程數(shù)/客戶端數(shù),第一次100,第二次200
–iterations=1 指定測試重復(fù)次數(shù)1次
–number-int-cols=20 指定測試表中int列的數(shù)量
–number-char-cols=30 指定測試表中varchar列的數(shù)量
–auto-generate-sql 自動生成 SQL 語句
–auto-generate-sql-add-autoincrement 在自動生成的表中添加自增列
–auto-generate-sql-load-type=mixed 測試的負(fù)載模型,包括 mixed, update, write, key,read,默認(rèn)是 mix
–engine=myisam,innodb 指定建表時的存儲引擎
–number-of-queries=2000 指定每個線程執(zhí)行的 SQL 語句數(shù)量上限(不精確)
–verbose 更詳細(xì)的輸出
Average number of seconds to run all queries #運行所有查詢的平均秒數(shù)
Minimum number of seconds to run all queries #運行所有查詢所需的最小秒數(shù)
Maximum number of seconds to run all queries #運行所有查詢的最大秒數(shù)
Number of clients running queries #運行查詢的客戶端數(shù)量
Average number of queries per client #每個客戶端的平均查詢數(shù)
看來這點壓力不是問題,壓力測試大膽一點
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
壓力調(diào)大測試,發(fā)現(xiàn)明顯現(xiàn)在查詢時間明顯變慢了
壓力也不要調(diào)太大,會出現(xiàn)下面的場景
使用第三方工具sysbench進(jìn)行壓力測試
簡介
系統(tǒng)平臺/自述文件在碩士 ·阿科皮托夫/系統(tǒng)臺 ·吉特哈布 (github.com)
https://github.com/akopytov/sysbench/blob/master/README.md#general-command-line-options
sysbench]是一款開源的多線程性能測試工具,可以執(zhí)行CPU/內(nèi)存/線程/IO/數(shù)據(jù)庫等方面的性能測試。數(shù)據(jù)庫目前支持MySQL/Oracle/PostgreSQL。本文只是簡單演示一下幾種測試的用法,后續(xù)準(zhǔn)備利用sysbench來對MySQL進(jìn)行一系列的測試。具體的一些參數(shù)設(shè)置,需要根據(jù)不同的測試要求來進(jìn)行調(diào)整。
- CPU 運算性能測試
- 磁盤 IO 性能測試
- 調(diào)度程序性能測試
- 內(nèi)存分配及傳輸速度測試
- POSIX 線程性能測試
- 數(shù)據(jù)庫性能測試(OLTP 基準(zhǔn)測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 腳本執(zhí)行,例如 oltp_read_only.lua 腳本執(zhí)行只讀測試)
- sysbench 還可以通過運行命令時指定自己的 Lua 腳本來自定義測試。
默認(rèn)支持mysql
yum -y install epel-release #安裝第三方epel源
yum -y install sysbench #安裝sysbench工具
sysbench --version #確定工具已安裝
sysbench 1.0.17
查看sysbench工具的幫助選項
-
testname 是內(nèi)置測試的可選名稱(例如 ,,等),或捆綁的 Lua 腳本之一的名稱(例如),或自定義 Lua 腳本的路徑。如果在命令行上沒有指定測試名稱(因此,也沒有命令,因為在這種情況下,它將被解析為測試名稱),或者測試名稱是破折號(“”),則 sysbench 期望 Lua 腳本在其標(biāo)準(zhǔn)輸入上執(zhí)行。
-
command 是一個可選參數(shù),它將通過系統(tǒng)平臺傳遞到使用測試名稱指定的內(nèi)置測試或腳本。命令定義測試必須執(zhí)行的操作。可用命令的列表取決于特定的測試。某些測試還實現(xiàn)自己的自定義命令。
以下是對典型測試命令及其用途的描述:
- prepare:為那些需要它們的測試執(zhí)行準(zhǔn)備操作,例如,在磁盤上為測試創(chuàng)建必要的文件,或為數(shù)據(jù)庫基準(zhǔn)測試填充測試數(shù)據(jù)庫。
- run:運行使用測試名稱參數(shù)指定的實際測試。此命令由所有測試提供。
- cleanup:在創(chuàng)建臨時數(shù)據(jù)的測試中,刪除測試運行后的臨時數(shù)據(jù)。
- help:顯示使用測試名參數(shù)指定的測試的用法信息。這包括測試提供的命令的完整列表,因此應(yīng)使用它來獲取可用命令。
-
optiond是以 開頭的零個或多個命令行選項的列表。與命令一樣,該命令應(yīng)用于描述特定測試提供的可用選項。
有關(guān) sysbench 本身提供的常規(guī)選項的說明,請參閱常規(guī)命令行選項。
可用于顯示常規(guī)命令行語法和選項。sysbench --help
[root@liuxu1 ~]# sysbench --help Usage:sysbench [options]... [testname] [command]Commands implemented by most tests: prepare run cleanup helpGeneral options: #普通功能--threads=N 要使用的線程數(shù),默認(rèn) 1 個 [1]--events=N 最大允許的事件個數(shù) [0]--time=N 最大的總執(zhí)行時間,以秒為單位 [10]--forced-shutdown=STRING 在 --time 時間限制到達(dá)后,強制關(guān)閉之前等待的秒數(shù),默認(rèn)“off”禁用--thread-stack-size=SIZE 每個線程的堆棧大小 [64K]--rate=N 平均傳輸速率。0 則無限制 [0]--report-interval=N 以秒為單位定期報告具有指定間隔的中間統(tǒng)計信息 0 禁用中間報告 [0]--report-checkpoints=[LIST,...] 轉(zhuǎn)儲完整的統(tǒng)計信息并在指定的時間點重置所有計數(shù)器。參數(shù)是一個逗號分隔的值列表,表示從測試開始經(jīng)過這個時間量時必須執(zhí)行報告檢查點(以秒為單位)。報告檢查點默認(rèn)關(guān)閉。 []--debug[=on|off] 打印更多 debug 信息 [off]--validate[=on|off] 盡可能執(zhí)行驗證檢查 [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: #偽隨機數(shù)生成器選項--rand-type=STRING 隨機數(shù)分布 {uniform,gaussian,special,pareto} 默認(rèn)使用[special]--rand-spec-iter=N 生成數(shù)字的迭代次數(shù)[12]--rand-spec-pct=N 被視為“特殊”(用于特殊分布)的值的百分比[1]--rand-spec-res=N 使用的“特殊”值的百分比(用于特殊分布)[75]--rand-seed=N 隨機數(shù)生成器的種子。0時,當(dāng)前時間作為RNG的種子。[0]--rand-pareto-h=N pareto分布的參數(shù)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服務(wù)器主機
–mysql-port=[LIST,…] MySQL server port [3306] #MySQL服務(wù)器端口
–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數(shù)據(jù)庫名稱
–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] #是否跟蹤所有客戶端庫調(diào)用
–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] #模擬運行,假設(shè)所有的MySQL客戶端API調(diào)用都是成功的,但沒有執(zhí)行它們
sysbench測試MySQL數(shù)據(jù)庫性能
1、準(zhǔn)備測試數(shù)據(jù)
#查看sysbench自帶的lua腳本使用方法 sysbench /usr/share/sysbench/oltp_common.lua help #必須創(chuàng)建sbtest庫,sbtest事sysbench默認(rèn)使用的庫名 mysqladmin -uroot -pabc123 create sbtest; #然后,準(zhǔn)備測試所用的表,這些測試表放在測試庫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表示創(chuàng)建10個測試表, #--table_size=100000表示每個表中插入10W行數(shù)據(jù), #prepare表示這是準(zhǔn)備數(shù)的過程。 --auto_inc[=on|off] id列默認(rèn)自增,默認(rèn)[on] --create_secondary[=on|off] 索引不是主鍵索引而是二級索引,默認(rèn)[off] --delete_inserts=N 每個事務(wù)的delete、insert的組合數(shù),默認(rèn)[1] --distinct_ranges=N 單個事務(wù)中執(zhí)行范圍distinct查詢的數(shù)量,默認(rèn)[1] --index_updates=N 單個事務(wù)中執(zhí)行索引更新的操作的數(shù)量,默認(rèn)[1] --mysql_storage_engine=STRING mysql的存儲引擎,默認(rèn)是[innodb] --non_index_updates=N 單個事務(wù)中執(zhí)行索引更新的操作的數(shù)量,默認(rèn)[1] --order_ranges=N 單個事務(wù)中執(zhí)行范圍order by查詢的數(shù)量,默認(rèn)[1] --pgsql_variant=STRING 在運行pgsql的驅(qū)動程序時,使用了pgsql的變體,當(dāng)前僅僅支持的variant是'redshift',當(dāng)啟動時,將自動禁用create_secondary為不可用,并將delete_insert設(shè)置為0 --point_selects=N 單個事務(wù)中select查詢的數(shù)量,默認(rèn) [10] --range_selects[=on|off] 啟用/禁用所有SELECT查詢[on] --secondary[=on|off] 使用二級索引代替逐漸,默認(rèn)[off] --range_size=N 查詢范圍。默認(rèn)是100 --simple_ranges=N 每個事務(wù)的簡單查詢范圍select,默認(rèn)[1] --skip_trx[=on|off] 語句以bigin/commit開始結(jié)尾,默認(rèn)[off] --sum_ranges=N 每個事務(wù)的select sum的查詢總量,默認(rèn)[1] --table_size=N 指定表的記錄大小(行的數(shù)量),默認(rèn)[10000] --tables=N 創(chuàng)建的表數(shù)量,默認(rè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=10
–table_size=100000
cleanup #刪除剛剛的數(shù)據(jù)
總結(jié)
- 上一篇: ECSTORE验证码优化
- 下一篇: Excel导入数据库回滚