開通讀寫分離功能後,事務會預設全部路由至主執行個體上執行。本文將以常用的MySQL壓測工具Sysbench 0.5版本為例,介紹如何正確配置其參數來進行讀寫分離效能的測試。

前提條件

注意事項

  • 建議測試讀寫分離的負載平衡不要用帶prepare或者帶事務的case。
  • 避免因寫壓力過大而造成的主從延遲時間超過設定的監控檢查閾值。
  • 推薦使用如下Sysbench指令碼,您可以實際情況構造具體的SQL。
    function thread_init(thread_id)
          db_connect()
      end
      function event(thread_id)
          rs =  db_query("select 1")
      end

設定Sysbench的參數

Sysbench oltp.lua指令碼測試預設使用事務,若使用預設參數,所有SQL都會在事務中執行,即使是唯讀SQL也會全部路由至主執行個體執行。所以,使用Sysbench壓測讀寫分離的效能時,必鬚根據需求設定Sysbench的參數。例如,您可以通過設定oltp-skip-trx參數可以使Sysbench運行SQL時不在事務中執行。

設定常用參數

請根據您的實際業務情況,設定如下參數值。

名稱 描述
test 指定測試檔案路徑。
mysql-host MySQL伺服器位址。
mysql-port MySQL伺服器連接埠。
mysql-user 使用者名。
mysql-password 密碼。
mysql-db 測試使用資料庫,需提前建立。
oltp-tables-count 建立表的個數。
oltp-table-size 每個表產生的記錄數量。
rand-init 是否隨機初始化資料。
max-time 壓測期間。
max-requests 壓測期間請求總數。
num-threads 並發線程數量。
report-interval 作業記錄列印間隔。

設定事務及讀寫SQL相關參數

如下參數會影響事務及讀寫SQL,在進行讀寫分離效能測試時按照實際需求設定參數值。

名稱 描述
oltp-test-mode 測試類型,但在Sysbench 0.5版本中此參數沒有生效,可以忽略。選擇性參數值如下:
  • complex:預設值,事務測試。
  • simple:簡單唯讀SQL測試。
  • nontrx:非事務測試。
  • sp:預存程序。
oltp-skip-trx 是否跳過SQL語句開頭的begin和結尾的commit。選擇性參數值如下:
  • off:預設值,執行的SQL全部在事務中。
  • on:非事務模式,若執行連續的對比壓測,需要重新準備資料(prepare)和清除資料(cleanup)。
说明 在壓測讀寫分離效能時,參數值需選擇on,SQL語句前後不需要begin/commint。
oltp-read-only 是否產生唯讀SQL。選擇性參數值如下:
  • off:預設值,執行oltp.lua的讀寫混合SQL。
  • on:只產生唯讀SQL,不會產生update、delete和insert類型的SQL。
说明 請根據需求選擇參數值,進行唯讀或讀寫測試。

壓測樣本

測試讀寫效能
  1. 執行如下命令,準備資料。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=off --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 prepare;
  2. 執行如下命令,運行測試。
    说明 非事務的讀寫測試更新資料時容易出現類似ALERT: Error 1062 Duplicate entry 'xxx' for key 'PRIMARY'的錯誤,所以需要增加參數--mysql-ignore-errors=1062來跳過這個錯誤。若參數mysql-ignore-errors沒有生效,則說明Sysbench版本較低,需將其升級至最新的0.5版本。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=off --mysql-ignore-errors=1062 --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 run;
  3. 執行如下命令,清除資料。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=off --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 cleanup;
測試唯讀效能
  1. 執行如下命令,準備資料。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 prepare;
  2. 執行如下命令,運行測試。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 run;
  3. 執行如下命令,清除資料。
    sysbench --test=./tests/db/oltp.lua --mysql-host=127.0.0.1 --mysql-port=3001 --mysql-user=abc --mysql-password=abc123456 --mysql-db=testdb --oltp-tables-count=10 --oltp-table-size=500000 --report-interval=5 --oltp-skip-trx=on --oltp-read-only=on --rand-init=on --max-requests=0 --max-time=300 --num-threads=100 cleanup;