MySQL壓力測試工具
阿新 • • 發佈:2020-12-27
一、使用MySQL本身的Mysqlslap工具
mysqlslap是mysql自帶的基準測試工具,該工具查詢資料,語法簡單,靈活容易使用.該工具可以模擬多個客戶端同時併發的向伺服器發出查詢更新,給出了效能測試資料而且提供了多種引擎的效能比較。mysqlslap為mysql效能優化前後提供了直觀的驗證依據,系統運維和DBA人員應該掌握一些常見的壓力測試工具,才能準確的掌握線上資料庫支撐的使用者流量上限及其抗壓性等問題。
1.1 更改其預設的最大連線數
在對MySQL進行壓力測試之前,需要更改其預設的最大連線數,如下:
[root@db01 ~]# vim /etc/my.cnf [mysqld] max_connections=1024 [root@db01 ~]# systemctl restart mysqld [root@db01 ~]# mysql -uroot -p123 mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value | +-----------------+-------+ | max_connections | 1024 | +-----------------+-------+ #驗證修改已經生效 [root@db01 ~]# 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。 Benchmark Running for engine myisam Average number of seconds to run all queries: 0.325 seconds Minimum number of seconds to run all queries: 0.325 seconds Maximum number of seconds to run all queries: 0.325 seconds Number of clients running queries: 100 Average number of queries per client: 20 #Myisam第一次100客戶端同時發起增查用0.325/s Benchmark Running for engine myisam Average number of seconds to run all queries: 0.331 seconds Minimum number of seconds to run all queries: 0.331 seconds Maximum number of seconds to run all queries: 0.331 seconds Number of clients running queries: 200 Average number of queries per client: 10 #Myisam第二次200客戶端同時發起增查用0.331/s Benchmark Running for engine innodb Average number of seconds to run all queries: 0.661 seconds Minimum number of seconds to run all queries: 0.661 seconds Maximum number of seconds to run all queries: 0.661 seconds Number of clients running queries: 100 Average number of queries per client: 20 #Innodb第一次100客戶端同時發起增查用0.661/s Benchmark Running for engine innodb Average number of seconds to run all queries: 0.610 seconds Minimum number of seconds to run all queries: 0.610 seconds Maximum number of seconds to run all queries: 0.610 seconds Number of clients running queries: 200 Average number of queries per client: 10 #Innodb第二次200客戶端同時發起增查用0.610/s
可以根據實際需求,一點點的加大併發數量進行壓力測試!
關於mysqlslap工具各個選項的詳細解釋可以通過mysqlslap --help
進行查詢!
二、使用第三方sysbench工具
2.1 安裝sysbench工具
[root@db01 ~]# yum -y install epel-release
#安裝epel源
[root@db01 ~]# yum -y install sysbench
#安裝sysbench工具
[root@db01 ~]# sysbench --version
sysbench 1.0.17
#查詢sysbench工具的版本資訊,確認sysbench工具已經安裝
sysbench可以進行以下測試:
- CPU運算效能測試;
- 磁碟IO效能測試;
- 排程程式效能測試;
- 記憶體分配及傳輸速度測試;
- POSIX執行緒效能測試;
- 資料庫效能測試(OLTP 基準測試,需要通過 /usr/share/sysbench/ 目錄中的 Lua 指令碼執行,例如 oltp_read_only.lua 指令碼執行只讀測試);
- sysbench還可以通過執行命令時指定自己的Lua指令碼進行自定義測試;
2.2 檢視sysbench工具的幫助選項
[root@mysql ~]# sysbench --help Usage: sysbench [options]... [testname] [command] Commands implemented by most tests: prepare run cleanup help # 可用的命令,四個 General options: # 通用選項 --threads=N 要使用的執行緒數,預設 1 個 [1] --events=N 最大允許的事件個數 [0] --time=N 最大的總執行時間,以秒為單位 [10] --forced-shutdown=STRING 在 --time 時間限制到達後,強制關閉之前等待的秒數,預設“off”禁用(number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable) [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 random numbers distribution {uniform,gaussian,special,pareto} [special] --rand-spec-iter=N number of iterations used for numbers generation [12] --rand-spec-pct=N percentage of values to be treated as 'special' (for special distribution) [1] --rand-spec-res=N percentage of 'special' values to use (for special distribution) [75] --rand-seed=N seed for random number generator. When 0, the current time is used as a RNG seed. [0] --rand-pareto-h=N parameter h for pareto distribution [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 指定要使用的資料庫驅動程式 ('help' to get list of available drivers) --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 和 PostgreSQL mysql - MySQL driver pgsql - PostgreSQL driver mysql options: # MySQL 資料庫專用選項 --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: # PostgreSQL 資料庫專用選項 --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 test cpu - CPU performance test memory - Memory functions speed test threads - Threads subsystem performance test mutex - Mutex performance test See 'sysbench <testname> help' for a list of options for each test.
2.3 sysbench測試MySQL效能
① 準備測試資料
[root@db01 ~]# sysbench /usr/share/sysbench/oltp_common.lua help
#檢視sysbench自帶的lua指令碼使用方法
[root@db01 ~]# mysqladmin -uroot -p123 create sbtest;
#必須建立sbtest庫,sbtest是sysbench預設使用的庫名
[root@db01 ~]# 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
#準備測試所用的表,這些測試表放在測試庫sbtest中。這裡使用的lua指令碼為/usr/share/sysbench/oltp_common.lua
#--tables=10表示建立10個測試表
#--table_size=100000表示每個表中插入10W行資料
#prepare表示這是準備數的過程
② 確認測試資料存在
[root@db01 ~]# mysql -uroot -p123 -e 'show tables from sbtest;'
+------------------+
| Tables_in_sbtest |
+------------------+
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+------------------+
[root@db01 ~]# mysql -uroot -p123 -e 'select count(*) from sbtest.sbtest1;'
+----------+
| count(*) |
+----------+
| 100000 |
+----------+
③ 資料庫測試和結果分析
需要注意的是,之前使用的lua指令碼為oltp_common.lua,它是一個通用指令碼,是被其它lua指令碼呼叫的,它不能直接拿來測試。
所以,我這裡用oltp_read_write.lua指令碼來做讀、寫測試。還有很多其它型別的測試,比如只讀測試、只寫測試、刪除測試、大批量插入測試等等。可找到對應的lua指令碼進行呼叫即可。
[root@db01 ~]# 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
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 4
Report intermediate results every 5 second(s)
Initializing random number generator from current time
Initializing worker threads...
Threads started!
#以下是每5秒返回一次的結果,統計的指標包括:
# 執行緒數、tps(每秒事務數)、qps(每秒查詢數)、
# 每秒的讀/寫/其它次數、延遲、每秒錯誤數、每秒重連次數
[ 5s ] thds: 4 tps: 318.10 qps: 6371.34 (r/w/o: 4461.96/1272.39/636.99) lat (ms,95%): 20.37 err/s: 0.00 reconn/s: 0.00
[ 10s ] thds: 4 tps: 340.15 qps: 6803.43 (r/w/o: 4760.92/1362.21/680.30) lat (ms,95%): 14.21 err/s: 0.00 reconn/s: 0.00
[ 15s ] thds: 4 tps: 380.98 qps: 7622.45 (r/w/o: 5336.16/1524.33/761.97) lat (ms,95%): 13.22 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 4 tps: 387.29 qps: 7741.76 (r/w/o: 5420.03/1547.15/774.58) lat (ms,95%): 12.75 err/s: 0.00 reconn/s: 0.00
SQL statistics:
queries performed:
read: 99918 #執行的讀運算元量
write: 28548 #執行的寫運算元量
other: 14274 #執行的其他運算元量
total: 142740
transactions: 7137 (356.13 per sec.) #執行事務的平均概率
queries: 142740 (7122.70 per sec.) #平均每秒可以進行多少次查詢
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 20.0391s #總消耗時間
total number of events: 7137 #總請求數量(讀、寫、其他)
Latency (ms):
min: 2.58
avg: 11.22
max: 450.05
95th percentile: 15.00 #取樣計算的平均延遲
sum: 80067.39
Threads fairness:
events (avg/stddev): 1784.2500/7.43
execution time (avg/stddev): 20.0168/0.01
④ cpu/io/記憶體等測試
sysbench內建的幾個測試指標如下:
[root@db01 ~]# sysbench --help
.......... # 省略部分內容
Compiled-in tests:
fileio - File I/O test
cpu - CPU performance test
memory - Memory functions speed test
threads - Threads subsystem performance test
mutex - Mutex performance test
可以直接help輸出測試方法,例如,fileio測試:
[root@db01 ~]# sysbench fileio help
sysbench 1.0.17 (using system LuaJIT 2.0.4)
fileio options:
--file-num=N number of files to create [128]
--file-block-size=N block size to use in all IO operations [16384]
--file-total-size=SIZE total size of files to create [2G]
--file-test-mode=STRING test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
--file-io-mode=STRING file operations mode {sync,async,mmap} [sync]
--file-async-backlog=N number of asynchronous operatons to queue per thread [128]
--file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} []
--file-fsync-freq=N do fsync() after this number of requests (0 - don't use fsync()) [100]
--file-fsync-all[=on|off] do fsync() after each write operation [off]
--file-fsync-end[=on|off] do fsync() at the end of test [on]
--file-fsync-mode=STRING which method to use for synchronization {fsync, fdatasync} [fsync]
--file-merged-requests=N merge at most this number of IO requests if possible (0 - don't merge) [0]
--file-rw-ratio=N reads/writes ratio for combined test [1.5]
1)測試IO效能
[root@db01 ~]# sysbench fileio --file-num=5 --file-total-size=2G prepare
[root@db01 ~]# ll
總用量 2097200
-rw------- 1 root root 429506560 4月 15 21:21 test_file.0
-rw------- 1 root root 429506560 4月 15 21:21 test_file.1
-rw------- 1 root root 429506560 4月 15 21:21 test_file.2
-rw------- 1 root root 429506560 4月 15 21:21 test_file.3
-rw------- 1 root root 429506560 4月 15 21:21 test_file.4
[root@db01 ~]# 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
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 16
Initializing random number generator from current time
Extra file open flags: (none)
5 files, 409.6MiB each
2GiB total file size
Block size 16KiB
Number of IO requests: 5000
Read/Write ratio for combined random IO test: 1.50
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads...
Threads started!
File operations:
reads/s: 4590.43
writes/s: 3067.95
fsyncs/s: 122.53
Throughput: #吞吐量
read, MiB/s: 71.73 #表示讀頻寬
written, MiB/s: 47.94 #表示寫頻寬
General statistics:
total time: 0.6517s
total number of events: 5000
Latency (ms):
min: 0.00
avg: 1.61
max: 43.35
95th percentile: 5.47
sum: 8035.39
Threads fairness:
events (avg/stddev): 312.5000/16.19
execution time (avg/stddev): 0.5022/0.01
2)測試CPU效能
[root@db01 ~]# sysbench cpu --threads=40 --events=10000 --cpu-max-prime=20000 run
sysbench 1.0.17 (using system LuaJIT 2.0.4)
Running the test with following options:
Number of threads: 40
Initializing random number generator from current time
Prime numbers limit: 20000
Initializing worker threads...
Threads started!
CPU speed:
events per second: 507.66
General statistics:
total time: 10.0370s
total number of events: 5096
Latency (ms):
min: 1.94
avg: 77.06
max: 775.00
95th percentile: 397.39
sum: 392676.67
Threads fairness:
events (avg/stddev): 127.4000/2.05
execution time (avg/stddev): 9.8169/0.12