Sysbench 是開源多執行緒效能測試工具,支援 CPU、記憶體、I/O、執行緒、資料庫 (MySQL/PostgreSQL/Oracle) 等多維度測試。其資料庫 OLTP 測試模組是 MySQL 效能基準測試的業界標準工具之一。
# 安裝編譯依賴 sudo yum install gcc gcc-c++ automake make libtool mysql-community-devel # 下載原始碼 (0.5 版本含 Lua 腳本) cd /tmp && git clone https://github.com/akopytov/sysbench.git cd /tmp/sysbench && ./autogen.sh ./configure --prefix=/usr/local/sysbench-0.5 make && sudo make install # 部署 Lua 測試腳本 cd /usr/local/sysbench && sudo mkdir -p share/tests/db cp /tmp/sysbench/sysbench/tests/db/*.lua share/tests/db/ # 驗證安裝 ./bin/sysbench --version # sysbench 0.5
--with-pgsql--with-oracle| 參數 | 說明 | 典型值/建議 |
|---|---|---|
--test | 測試腳本路徑 | ./share/tests/db/oltp.lua |
--mysql-host/port/user/password/db | 目標資料庫連線資訊 | 依環境設定 |
--oltp-tables-count | 測試表數量 | 10~100 |
--oltp-table-size | 每表初始資料筆數 | 10000~1000000 |
--oltp-test-mode | 測試模式 | simple/complex/nontrx |
--oltp-read-only | 純讀取測試 | on/off |
--num-threads | 並發執行緒數 (模擬連線數) | 16, 32, 64, 128, 256... |
--max-time | 測試持續秒數 | 60~300 |
--max-requests | 總請求上限 (0=不限,以時間為準) | 0 |
--report-interval | 進度報告間隔秒數 | 10 |
--oltp-dist-type | 隨機分布類型 | uniform/gaussian/special |
--rand-init | 隨機化初始資料 | on (避免快取偏差) |
--mysql-ignore-errors | 忽略特定錯誤碼 (如 1062 主鍵衝突) | 1062 |
# 1. 準備資料 ./bin/sysbench --test=./share/tests/db/oltp.lua \ --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser \ --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 \ --oltp-dist-type=uniform --rand-init=on \ --oltp-read-only=on --oltp-skip-trx=on \ --max-time=120 --num-threads=12 prepare # 2. 執行壓測 ./bin/sysbench ... run # 3. 清理資料 ./bin/sysbench ... cleanup
關鍵參數說明:--oltp-skip-trx=on 跳過 BEGIN/COMMIT,避免中間件將唯讀請求路由至主庫;--oltp-read-only=on 只產生 SELECT。
./bin/sysbench --test=./share/tests/db/oltp.lua \ --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser \ --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 \ --report-interval=10 --rand-init=on --max-requests=0 \ --oltp-read-only=off --max-time=120 --num-threads=128 \ [prepare|run|cleanup]
注意:Sysbench 0.5 移除了 --oltp-test-mode=nontrx 選項,預設使用 complex 事務模式 (select:update:delete:insert = 14:1:1:1)。混合讀寫測試必須加上 --mysql-ignore-errors=1062 忽略主鍵衝突,否則高並發 INSERT 會失敗中止。
./bin/sysbench --test=./share/tests/db/update_index.lua \ --mysql-host=10.0.201.36 --mysql-port=8066 --mysql-user=ecuser --mysql-password=ecuser \ --mysql-db=dbtest1a --oltp-tables-count=10 --oltp-table-size=500000 \ --report-interval=10 --rand-init=on --max-requests=0 \ --oltp-read-only=off --max-time=120 --num-threads=128 \ [prepare|run|cleanup]
專門測試索引欄位更新效能,適合對比不同配置下的寫入能力。
單一事務預設操作比例:
SELECT (point select) : 14 SELECT (simple range) : 1 SELECT (sum range) : 1 SELECT (order range) : 1 SELECT (distinct range) : 1 UPDATE (index column) : 1 UPDATE (non-index column) : 1 DELETE : 1 INSERT : 1 -- 總計:21 個查詢/事務
可透過以下參數調整權重:
--oltp-point-selects--oltp-simple-ranges, --oltp-sum-ranges, --oltp-order-ranges, --oltp-distinct-ranges--oltp-index-updates, --oltp-non-index-updatessysbench 0.5: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 128
...
[ 20s] threads: 128, tps: 2354.54, reads: 33035.89, writes: 9423.39, response time: 66.80ms (95%), errors: 0.00
...
OLTP test statistics:
queries performed:
read: 4003048
write: 1143728
other: 571864
total: 5718640
transactions: 285932 (2382.10 per sec.) <-- 關鍵 TPS
read/write requests: 5146776 (42877.85 per sec.) <-- QPS
ignored errors: 0
General statistics:
total time: 120.0334s
total number of events: 285932
response time:
min: 17.60ms
avg: 53.73ms
max: 252.90ms
approx. 95 percentile: 66.88ms <-- 關鍵 P95 延遲
Threads fairness:
events (avg/stddev): 2233.8438/9.04
execution time (avg/stddev): 120.0208/0.01
| 指標 | 含義 | 計算方式 | 關注重點 |
|---|---|---|---|
| TPS | 每秒事務數 | transactions / total_time | 寫入吞吐核心指標 |
| QPS | 每秒查詢數 | read_write_requests / total_time | 總吞吐能力 |
| Avg Response Time | 平均回應時間 | 內建統計 | 整體延遲水平 |
| P95 Response Time | 95% 分位延遲 | --percentile=95 可調 | 體驗端延遲,生產環境核心 SLA |
| Errors | 錯誤率 | ignored_errors / total | 必須為 0 或極低 |
uniform 分布下 Query Cache 會嚴重鎖爭用,對比測試建議關閉--oltp-skip-trx=on--mysql-ignore-errors=1062,0.5 版新增功能替代舊補丁--num-threads 遞增 (16→32→64→128→256) 觀察擴展性拐點my.cnf 單一參數對比效果Sysbench 雖測試模型簡單 (單表 OLTP),但參數豐富、結果可重現、支援多引擎,是 MySQL 配置調優、硬體選型、版本升級驗證、中間件效能評估 的利器。掌握其核心參數與結果解讀,能讓資料庫效能優化有據可依、可量化比較。