1. 程式人生 > 資料庫 >MySQL壓力測試工具Mysqlslap的使用

MySQL壓力測試工具Mysqlslap的使用

一、MySQL自帶的壓力測試工具 Mysqlslap

mysqlslap是mysql自帶的基準測試工具,該工具查詢資料,語法簡單,靈活容易使用.該工具可以模擬多個客戶端同時併發的向伺服器發出查詢更新,給出了效能測試資料而且提供了多種引擎的效能比較。mysqlslap為mysql效能優化前後提供了直觀的驗證依據,系統運維和DBA人員應該掌握一些常見的壓力測試工具,才能準確的掌握線上資料庫支撐的使用者流量上限及其抗壓性等問題。

1、更改其預設的最大連線數

在對MySQL進行壓力測試之前,需要更改其預設的最大連線數,如下:

[root@mysql~]#vim/etc/my.cnf
................
[mysqld]
max_connections=1024
[root@mysql~]#systemctlrestartmysqld
`

檢視最大連線數

`mysql>showvariableslike'max_connections';
+-----------------+-------+
|Variable_name|Value|
+-----------------+-------+
|max_connections|1024|
+-----------------+--------+
1rowinset(0.00sec)

進行壓力測試:

[root@mysql~]#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-p123--verbose

上述命令測試說明:模擬測試兩次讀寫併發,第一次100,第二次200,自動生成SQL指令碼,測試表包含20個init欄位,30個char欄位,每次執行2000查詢請求。測試引擎分別是myisam,innodb。(上述選項中有很多都是預設值,可以省略,如果想要了解各個選項的解釋,可以使用mysqlslap --help進行查詢)。

測試結果說明:

  • Myisam第一次100客戶端同時發起增查用0.557/s,第二次200客戶端同時發起增查用0.522/s
  • Innodb第一次100客戶端同時發起增查用0.256/s,第二次200客戶端同時發起增查用0.303/s

可以根據實際需求,一點點的加大併發數量進行壓力測試。

MySQL壓力測試工具Mysqlslap的使用

二、使用第三方sysbench工具進行壓力測試

1、安裝sysbench工具

[root@mysql~]#yum-yinstallepel-release#安裝第三方epel源
[root@mysql~]#yum-yinstallsysbench#安裝sysbench工具
[root@mysql~]#sysbench--version#確定工具已安裝
sysbench1.0.17

sysbench可以進行以下測試:

  • CPU 運算效能測試
  • 磁碟 IO 效能測試
  • 排程程式效能測試
  • 記憶體分配及傳輸速度測試
  • POSIX 執行緒效能測試

資料庫效能測試(OLTP 基準測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 指令碼執行,例如 oltp_read_only.lua 指令碼執行只讀測試)。

sysbench 還可以通過執行命令時指定自己的 Lua 指令碼來自定義測試。

2、檢視sysbench工具的幫助選項

[root@mysql~]#sysbench--help
Usage:
sysbench[options]...[testname][command]
Commandsimplementedbymosttests:prepareruncleanuphelp#可用的命令,四個
Generaloptions:#通用選項
--threads=N要使用的執行緒數,預設1個[1]
--events=N最大允許的事件個數[0]
--time=N最大的總執行時間,以秒為單位[10]
--forced-shutdown=STRING在--time時間限制到達後,強制關閉之前等待的秒數,預設“off”禁用(numberofsecondstowaitafterthe--timelimitbeforeforcingshutdown,or'off'todisable)[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-RandomNumbersGeneratoroptions:#偽隨機數發生器選項
--rand-type=STRINGrandomnumbersdistribution{uniform,gaussian,special,pareto}[special]
--rand-spec-iter=Nnumberofiterationsusedfornumbersgeneration[12]
--rand-spec-pct=Npercentageofvaluestobetreatedas'special'(forspecialdistribution)[1]
--rand-spec-res=Npercentageof'special'valuestouse(forspecialdistribution)[75]
--rand-seed=Nseedforrandomnumbergenerator.When0,thecurrenttimeisusedasaRNGseed.[0]
--rand-pareto-h=Nparameterhforparetodistribution[0.2]
Logoptions:#日誌選項
--verbosity=Nverbositylevel{5-debug,0-onlycriticalmessages}[3]
--percentile=Npercentiletocalculateinlatencystatistics(1-100).Usethespecialvalueof0todisablepercentilecalculations[95]
--histogram[=on|off]printlatencyhistograminreport[off]
Generaldatabaseoptions:#通用的資料庫選項
--db-driver=STRING指定要使用的資料庫驅動程式('help'togetlistofavailabledrivers)
--db-ps-mode=STRINGpreparedstatementsusagemode{auto,disable}[auto]
--db-debug[=on|off]printdatabase-specificdebuginformation[off]
Compiled-indatabasedrivers:#內建的資料庫驅動程式,預設支援MySQL和PostgreSQL
mysql-MySQLdriver
pgsql-PostgreSQLdriver
mysqloptions:#MySQL資料庫專用選項
--mysql-host=[LIST,...]MySQLserverhost[localhost]
--mysql-port=[LIST,...]MySQLserverport[3306]
--mysql-socket=[LIST,...]MySQLsocket
--mysql-user=STRINGMySQLuser[sbtest]
--mysql-password=STRINGMySQLpassword[]
--mysql-db=STRINGMySQLdatabasename[sbtest]
--mysql-ssl[=on|off]useSSLconnections,ifavailableintheclientlibrary[off]
--mysql-ssl-cipher=STRINGusespecificcipherforSSLconnections[]
--mysql-compression[=on|off]usecompression,ifavailableintheclientlibrary[off]
--mysql-debug[=on|off]traceallclientlibrarycalls[off]
--mysql-ignore-errors=[LIST,...]listoferrorstoignore,or"all"[1213,1020,1205]
--mysql-dry-run[=on|off]Dryrun,pretendthatallMySQLclientAPIcallsaresuccessfulwithoutexecutingthem[off]
pgsqloptions:#PostgreSQL資料庫專用選項
--pgsql-host=STRINGPostgreSQLserverhost[localhost]
--pgsql-port=NPostgreSQLserverport[5432]
--pgsql-user=STRINGPostgreSQLuser[sbtest]
--pgsql-password=STRINGPostgreSQLpassword[]
--pgsql-db=STRINGPostgreSQLdatabasename[sbtest]
Compiled-intests:#內建測試型別
fileio-FileI/Otest
cpu-CPUperformancetest
memory-Memoryfunctionsspeedtest
threads-Threadssubsystemperformancetest
mutex-Mutexperformancetest
See'sysbench<testname>help'foralistofoptionsforeachtest.

3、sysbench測試MySQL資料庫效能

1)準備測試資料

#檢視sysbench自帶的lua指令碼使用方法
[root@mysql~]#sysbench/usr/share/sysbench/oltp_common.luahelp
#必須建立sbtest庫,sbtest是sysbench預設使用的庫名
[root@mysql~]#mysqladmin-uroot-p123createsbtest;
#然後,準備測試所用的表,這些測試表放在測試庫sbtest中。這裡使用的lua指令碼為/usr/share/sysbench/oltp_common.lua。
[root@mysql~]#sysbench--mysql-host=127.0.0.1
--mysql-port=3306
--mysql-user=root
--mysql-password=123
/usr/share/sysbench/oltp_common.lua
--tables=10
--table_size=100000
prepare
#其中--tables=10表示建立10個測試表,
#--table_size=100000表示每個表中插入10W行資料,
#prepare表示這是準備數的過程。

2)確認測試資料以存在

[root@mysql~]#mysql-uroot-p123sbtest;#登入到sbtest庫
mysql>showtables;#檢視相應的表
+------------------+
|Tables_in_sbtest|
+------------------+
|sbtest1|
|sbtest10|
|sbtest2|
|sbtest3|
|sbtest4|
|sbtest5|
|sbtest6|
|sbtest7|
|sbtest8|
|sbtest9|
+------------------+
10rowsinset(0.00sec)
mysql>selectcount(*)fromsbtest1;#隨機選擇一個表,確認其有100000條資料
+----------+
|count(*)|
+----------+
|100000|
+----------+
1rowinset(0.01sec)

3)資料庫測試和結果分析

稍微修改下之前準備資料的語句,就可以拿來測試了。需要注意的是,之前使用的lua指令碼為oltp_common.lua,它是一個通用指令碼,是被其它lua指令碼呼叫的,它不能直接拿來測試。

所以,我這裡用oltp_read_write.lua指令碼來做讀、寫測試。還有很多其它型別的測試,比如只讀測試、只寫測試、刪除測試、大批量插入測試等等。可找到對應的lua指令碼進行呼叫即可。

#執行測試命令如下:
[root@mysql~]#sysbench--threads=4
--time=20
--report-interval=5
--mysql-host=127.0.0.1
--mysql-port=3306
--mysql-user=root
--mysql-password=123
/usr/share/sysbench/oltp_read_write.lua
--tables=10
--table_size=100000
run

上述命令返回的結果如下:

[root@mysql~]#sysbench--threads=4--time=20--report-interval=5--mysql-host=127.0.0.1--mysql-port=3306--mysql-user=root--mysql-password=123/usr/share/sysbench/oltp_read_write.lua--tables=10--table_size=100000run
sysbench1.0.17(usingsystemLuaJIT2.0.4)
Runningthetestwithfollowingoptions:
Numberofthreads:4
Reportintermediateresultsevery5second(s)
Initializingrandomnumbergeneratorfromcurrenttime
Initializingworkerthreads...
Threadsstarted!
#以下是每5秒返回一次的結果,統計的指標包括:
#執行緒數、tps(每秒事務數)、qps(每秒查詢數)、
#每秒的讀/寫/其它次數、延遲、每秒錯誤數、每秒重連次數
[5s]thds:4tps:1040.21qps:20815.65(r/w/o:14573.17/4161.25/2081.22)lat(ms,95%):7.17err/s:0.00reconn/s:0.00
[10s]thds:4tps:1083.34qps:21667.15(r/w/o:15165.93/4334.55/2166.68)lat(ms,95%):6.55err/s:0.00reconn/s:0.00
[15s]thds:4tps:1121.57qps:22429.09(r/w/o:15700.64/4485.30/2243.15)lat(ms,95%):6.55err/s:0.00reconn/s:0.00
[20s]thds:4tps:1141.69qps:22831.98(r/w/o:15982.65/4566.16/2283.18)lat(ms,95%):6.09err/s:0.00reconn/s:0.00
SQLstatistics:
queriesperformed:
read:307146#執行的讀運算元量
write:87756#執行的寫運算元量
other:43878#執行的其它運算元量
total:438780
transactions:21939(1096.57persec.)#執行事務的平均速率
queries:438780(21931.37persec.)#平均每秒能執行多少次查詢
ignorederrors:0(0.00persec.)
reconnects:0(0.00persec.)
Generalstatistics:
totaltime:20.0055s#總消耗時間
totalnumberofevents:21939#總請求數量(讀、寫、其它)
Latency(ms):
min:1.39
avg:3.64
max:192.05
95thpercentile:6.67#取樣計算的平均延遲
sum:79964.26
Threadsfairness:
events(avg/stddev):5484.7500/15.12
executiontime(avg/stddev):19.9911/0.00

4、cpu/io/記憶體等測試

sysbench內建的幾個測試指標如下:

[root@mysql~]#sysbench--help
..........#省略部分內容
Compiled-intests:
fileio-FileI/Otest
cpu-CPUperformancetest
memory-Memoryfunctionsspeedtest
threads-Threadssubsystemperformancetest
mutex-Mutexperformancetest

可以直接help輸出測試方法,例如,fileio測試:

[root@mysql~]#sysbenchfileiohelp
sysbench1.0.17(usingsystemLuaJIT2.0.4)
fileiooptions:
--file-num=Nnumberoffilestocreate[128]
--file-block-size=NblocksizetouseinallIOoperations[16384]
--file-total-size=SIZEtotalsizeoffilestocreate[2G]
--file-test-mode=STRINGtestmode{seqwr,seqrewr,seqrd,rndrd,rndwr,rndrw}
--file-io-mode=STRINGfileoperationsmode{sync,async,mmap}[sync]
--file-async-backlog=Nnumberofasynchronousoperatonstoqueueperthread[128]
--file-extra-flags=[LIST,...]listofadditionalflagstousetoopenfiles{sync,dsync,direct}[]
--file-fsync-freq=Ndofsync()afterthisnumberofrequests(0-don'tusefsync())[100]
--file-fsync-all[=on|off]dofsync()aftereachwriteoperation[off]
--file-fsync-end[=on|off]dofsync()attheendoftest[on]
--file-fsync-mode=STRINGwhichmethodtouseforsynchronization{fsync,fdatasync}[fsync]
--file-merged-requests=NmergeatmostthisnumberofIOrequestsifpossible(0-don'tmerge)[0]
--file-rw-ratio=Nreads/writesratioforcombinedtest[1.5]

1)測試io效能

例如,建立5個檔案,總共2G,每個檔案大概400M。

[root@mysql~]#sysbenchfileio--file-num=5--file-total-size=2Gprepare
[root@mysql~]#ll-lhtest*
-rw-------1rootroot410MMay2616:05test_file.0
-rw-------1rootroot410MMay2616:05test_file.1
-rw-------1rootroot410MMay2616:05test_file.2
-rw-------1rootroot410MMay2616:05test_file.3
-rw-------1rootroot410MMay2616:05test_file.4

然後執行測試:

[root@mysql~]#sysbench--events=5000
--threads=16
fileio
--file-num=5
--file-total-size=2G
--file-test-mode=rndrw
--file-fsync-freq=0
--file-block-size=16384
run

返回的結果如下:

Runningthetestwithfollowingoptions:
Numberofthreads:16
Initializingrandomnumbergeneratorfromcurrenttime
Extrafileopenflags:(none)
5files,409.6MiBeach
2GiBtotalfilesize
Blocksize16KiB
NumberofIOrequests:5000
Read/WriteratioforcombinedrandomIOtest:1.50
Callingfsync()attheendoftest,Enabled.
UsingsynchronousI/Omode
Doingrandomr/wtest
Initializingworkerthreads...
Threadsstarted!
Fileoperations:
reads/s:9899.03
writes/s:6621.38
fsyncs/s:264.33
Throughput:#吞吐量
read,MiB/s:154.66#表示讀頻寬
written,MiB/s:103.46#表示寫的頻寬
Generalstatistics:
totaltime:0.3014s
totalnumberofevents:5000
Latency(ms):
min:0.00
avg:0.81
max:53.56
95thpercentile:4.10
sum:4030.48
Threadsfairness:
events(avg/stddev):312.5000/27.64
executiontime(avg/stddev):0.2519/0.02

2)測試cpu效能

[root@mysql~]#sysbenchcpu--threads=40--events=10000--cpu-max-prime=20000run

壓測時,還請小心謹慎!!!!

以上就是MySQL壓力測試工具Mysqlslap的使用的詳細內容,更多關於Mysqlslap的使用的資料請關注我們其它相關文章!