首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Sysbench MySQL 壓力測試完整教學:安裝、參數詳解、OLTP 基準測試與結果分析

     編輯:MySQL 資料庫相關知識

Sysbench 介紹:專業資料庫效能基準測試工具

Sysbench 是開源多執行緒效能測試工具,支援 CPU、記憶體、I/O、執行緒、資料庫 (MySQL/PostgreSQL/Oracle) 等多維度測試。其資料庫 OLTP 測試模組是 MySQL 效能基準測試的業界標準工具之一。

一、Sysbench 安裝與部署 (CentOS 6.7 / MySQL 5.6)

1. 編譯安裝步驟

# 安裝編譯依賴
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

2. 多資料庫支援編譯選項

  • PostgreSQL:--with-pgsql
  • Oracle:--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:純讀取 (Read-Only) 測試

# 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。

場景 2:混合讀寫 (Read-Write) 測試

./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 會失敗中止。

場景 3:純更新 (Update-Only) 測試

./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]

專門測試索引欄位更新效能,適合對比不同配置下的寫入能力。

四、預設 OLTP.lua 事務內容與權重調整

單一事務預設操作比例:

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-updates

五、測試結果解讀關鍵指標

sysbench 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 Time95% 分位延遲--percentile=95 可調體驗端延遲,生產環境核心 SLA
Errors錯誤率ignored_errors / total必須為 0 或極低

六、進階技巧與避坑指南

  1. 關閉 Query Cache:uniform 分布下 Query Cache 會嚴重鎖爭用,對比測試建議關閉
  2. 中間件路由問題:Atlas/MyCat 等中間件預設路由事務至主庫,唯讀測試必加 --oltp-skip-trx=on
  3. 主鍵衝突處理:高並發寫入必加 --mysql-ignore-errors=1062,0.5 版新增功能替代舊補丁
  4. 分階段壓測:--num-threads 遞增 (16→32→64→128→256) 觀察擴展性拐點
  5. 參數對比實驗:固定壓測參數,僅調整 my.cnf 單一參數對比效果

七、總結

Sysbench 雖測試模型簡單 (單表 OLTP),但參數豐富、結果可重現、支援多引擎,是 MySQL 配置調優、硬體選型、版本升級驗證、中間件效能評估 的利器。掌握其核心參數與結果解讀,能讓資料庫效能優化有據可依、可量化比較。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved