Sysbench 是一款開源、多執行緒的基準測試工具,廣泛用於評估 CPU、記憶體、I/O、線程調度及資料庫效能。支援 MySQL、Oracle、PostgreSQL 等主流資料庫。本文記錄一個基於 OLTP(線上交易處理)混合負載 的 MySQL 壓測實戰過程,包含測試參數解析、多執行緒場景執行與結果分析。
sysbench --test=oltp \
--test=/usr/local/sysbench/tests/db/oltp.lua \
--db-driver=mysql \
--oltp-table-size=10000000 \
--mysql-db=sysbench \
--mysql-user=sysbench \
--mysql-password=lookback \
--mysql-host=10.10.231.149 \
--mysql-port=3317 \
--max-time=60 \
--max-requests=0 \
--report-interval=10 \
--oltp-reconnect-mode=random \
--num-threads=$i run
| 參數 | 說明 |
|---|---|
--oltp-table-size | 測試表資料量(本例 1000 萬筆) |
--max-time | 測試持續時間(秒),本例 60 秒 |
--max-requests=0 | 總請求數無限制,以時間為準 |
--report-interval | 中間報告輸出間隔(秒) |
--oltp-reconnect-mode=random | 隨機重連模式,模擬真實連線池行為 |
--num-threads | 併發執行緒數(模擬客戶端連線數) |
使用 Shell 迴圈自動化執行多線程測試,每組測試間隔 10 秒散熱:
for i in 1 4 8 16 32 64; do
sysbench --test=oltp --test=/usr/local/sysbench/tests/db/oltp.lua \
--db-driver=mysql --oltp-table-size=10000000 \
--mysql-db=sysbench --mysql-user=sysbench --mysql-password=lookback \
--mysql-host=10.10.231.149 --mysql-port=3317 \
--max-time=60 --max-requests=0 --report-interval=10 \
--oltp-reconnect-mode=random --num-threads=$i run
sleep 10
done
以下為各線程數下的核心效能摘要:
| Threads | TPS (avg) | QPS (Read/Write) | Avg Resp Time (ms) | 95% Resp Time (ms) | Max Resp Time (ms) |
|---|---|---|---|---|---|
| 1 | 81.35 | 1464.38 | 12.29 | 13.42 | 29.47 |
| 4 | 301.16 | 5420.91 | 13.28 | 15.37 | 82.75 |
| 8 | 474.20 | 8535.66 | 16.87 | 23.57 | 176.10 |
| 16 | 707.77 | 12739.77 | 22.60 | 31.21 | 2114.27 |
| 32 | 1028.69 | 18516.37 | 31.10 | 47.93 | 453.06 |
| 64 | 1091.93 | 19654.74 | 58.59 | 102.18 | 767.73 |

測試完成後,務必清理測試資料,避免影響後續測試:
sysbench --test=oltp --test=/usr/local/sysbench/tests/db/oltp.lua \
--mysql-db=sysbench --mysql-user=sysbench --mysql-password=lookback \
--mysql-host=10.10.231.149 --mysql-port=3317 cleanup
為了方便複用,將準備、壓測、清理封裝為一個參數化 Shell 腳本:
#!/bin/bash
# File: mysql_bench.sh
# Usage: bash mysql_bench.sh [user] [pass] [host] [port] [db] [threads] [lua_file]
user=${1:-sysbench}
password=${2:-lookback}
host=${3:-localhost}
port=${4:-3306}
dbname=${5:-sysbench}
threads=(${6:-1 4 8 16 32 64})
luafile=${7:-/usr/local/sysbench/tests/db/oltp.lua}
creattb() {
mysql -u$user -p$password -h$host -P$port -e "DROP DATABASE IF EXISTS $dbname;"
mysql -u$user -p$password -h$host -P$port -e "CREATE DATABASE IF NOT EXISTS $dbname DEFAULT CHARACTER SET utf8;"
sysbench --test=oltp --test=$luafile --db-driver=mysql --oltp-table-size=1000000 \
--mysql-db=$dbname --mysql-user=$user --mysql-password=$password \
--mysql-host=$host --mysql-port=$port prepare
}
benching() {
for i in ${threads[*]}; do
echo -e "\n======\033[31mPerforming test RW-${i}T\033[0m======"
sysbench --test=oltp --test=$luafile --db-driver=mysql --oltp-table-size=1000000 \
--mysql-db=$dbname --mysql-user=$user --mysql-password=$password \
--mysql-host=$host --mysql-port=$port \
--max-time=60 --max-requests=0 --report-interval=10 \
--oltp-reconnect-mode=random --num-threads=$i run
sleep 10
done
}
cleantb() {
sysbench --test=oltp --test=$luafile --db-driver=mysql \
--mysql-db=$dbname --mysql-user=$user --mysql-password=$password \
--mysql-host=$host --mysql-port=$port cleanup
mysql -u$user -p$password -h$host -P$port -e "DROP DATABASE IF EXISTS $dbname;"
}
creattb $@ && benching $@ && cleantb $@
使用範例:
bash mysql_bench.sh sysbench lookback 10.10.231.149 3317 sysbench '1 4 8'
Sysbench 提供了標準化、可重複的 MySQL 效能基準測試流程。透過調整 --num-threads、--oltp-table-size、--max-time 等參數,可模擬不同業務負載場景。關鍵觀測指標包括 TPS(每秒交易數)、QPS(每秒查詢數)、平均/95分位/最大響應時間。實際生產環境調優時,建議結合 innodb_buffer_pool_size、innodb_flush_log_at_trx_commit、sync_binlog 等核心參數進行對比壓測,找出最佳配置平衡點。