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

Sysbench 多線程性能測試工具完整使用指南

     編輯:MySQL 資料庫相關知識

Sysbench 簡介

多線程基準測試工具,支援 CPU、內存、I/O、線程、互斥鎖、數據庫(OLTP)等測試。數據庫測試為核心用例。

版本差異關鍵

特性0.4 版本0.5+ 版本
數據庫測試模式--test=oltpLua 腳本 --test=oltp.lua
多表支援單表多表
實時統計無支援
自定義邏輯硬編碼Lua 腳本可擴展

核心參數詳解 (以 0.4 為例,0.5 兼容大部分)

OLTP 專用參數

--oltp-test-mode=complex          # simple/complex/nontrx/sp
--oltp-table-size=10000           # 測試表行數
--oltp-table-name=sbtest          # 表名
--oltp-range-size=100             # 範圍查詢大小
--oltp-point-selects=10           # 點查詢數
--oltp-simple-ranges=1            # 簡單範圍查詢
--oltp-sum-ranges=1               # SUM 範圍查詢
--oltp-order-ranges=1             # ORDER BY 範圍
--oltp-distinct-ranges=1          # DISTINCT 範圍
--oltp-index-updates=1            # 索引更新數
--oltp-non-index-updates=1        # 非索引更新數
--oltp-auto-inc=on                # 使用自增主鍵
--oltp-connect-delay=10000        # 連接後延遲(微秒)
--oltp-user-delay-min=0           # 請求間最小延遲
--oltp-user-delay-max=0           # 請求間最大延遲

數據庫連接參數

--db-driver=mysql
--mysql-host=localhost
--mysql-port=3306
--mysql-user=sbtest
--mysql-password=
--mysql-db=sbtest
--mysql-table-engine=innodb      # myisam/innodb/bdb/heap/ndbcluster/federated
--mysql-engine-trx=auto          # yes/no/auto
--mysql-ssl=off

通用壓測參數

--num-threads=16                 # 併發線程數
--max-requests=0                 # 總請求數上限(0=無限)
--max-time=300                   # 最大運行時間(秒)
--report-interval=10             # 中間報告間隔(秒)
--forced-shutdown=off            # 超時後強制關閉等待時間
--rand-type=special              # 隨機分布: uniform/gaussian/special/pareto

完整測試流程範例

1. 準備數據

sysbench --test=oltp --oltp-table-size=100000 \
  --mysql-host=192.168.1.100 --mysql-user=root --mysql-password=xxx \
  --mysql-db=test --num-threads=16 prepare

2. 運行壓測

sysbench --test=oltp --oltp-table-size=100000 \
  --mysql-host=192.168.1.100 --mysql-user=root --mysql-password=xxx \
  --mysql-db=test --num-threads=16 --max-time=300 --report-interval=10 run

3. 清理數據

sysbench --test=oltp --mysql-host=192.168.1.100 --mysql-user=root --mysql-password=xxx --mysql-db=test cleanup

關鍵指標解讀

  • TPS (transactions/sec):核心吞吐指標
  • QPS (queries/sec):SQL 執行頻率
  • Latency (avg/95%/99%):響應時間分布
  • Errors:錯誤率(應為 0)
  • Reconnects:重連次數(反映穩定性)

進階用法

  • 自定義 Lua 腳本實現業務邏輯壓測
  • --report-checkpoints 定時導出完整統計
  • 對比不同引擎/參數/硬件配置下的性能基線

注意事項

  • 測試數據量需超過內存(>1000 行且總量 > 內存)結果才有參考價值
  • 預熱階段預留時間,避免冷啟動干擾
  • 生產環境嚴禁直接壓測,使用備庫/克隆環境
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved