經過sysbench工具實現MySQL數據庫的性能測試

1.背景

 sysbench是一款壓力測試工具,能夠測試系統的硬件性能,也能夠用來對數據庫進行基準測試。sysbench 支持的測試有CPU運算性能測試、內存分配及傳輸速度測試、磁盤IO性能測試、POSIX線程性能測試、互斥性測試測試、數據庫性能測試(OLTP基準測試)。目前支持的數據庫主要是MySQL數據庫和PG數據庫。前端

在新服務器上線時,建議對服務器的性能作一次測試,最好與既往的同類型的服務器的性能測試報表作一個橫線比較,發現潛在問題。及新機器上線前,對服務器作一次體檢。mysql

對數據庫而言,咱們能夠經過sysbench工具實現對數據庫的基準測試。在如今的系統架構中,前端都比較容易彈性水平拓展,數據庫相對較難,所以,基準測試對數據庫具備很重要的做用。而對數據庫的基準測試的做用,就是分析在當前的配置下(包括硬件配置、OS、數據庫設置等),數據庫的性能表現,從而找出MySQL的性能閾值,並根據實際系統的要求調整配置。sql

2.sysbench的安裝

1)安裝命令數據庫

yum -y install sysbench

2)查看安裝的版本bash

sysbench --version

3)查看已安裝軟件的信息(主要是通 rpm 命令)。服務器

 查詢sysbench的安裝信息,主要是測試mysql時,須要使用sysbench自帶的lua腳本進行測試。若是使用快速安裝的方式,默認的腳本路徑爲:/usr/share/sysbench。網絡

 若是不在這個命令,咱們咱們執行如下命令查看,查找已安裝在本機Linux系統上面的全部的sysbench軟件的程序:架構

rpm -qa sysbench

列出該軟件全部的文件與目錄所在完整文件名(list):併發

rpm -ql sysbench 

3.sysbench 語法

sysbench --help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:
  --threads=N                     number of threads to use [1]
  --events=N                      limit for total number of events [0]
  --time=N                        limit for total execution time in seconds [10]
  --forced-shutdown=STRING        number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
  --thread-stack-size=SIZE        size of stack per thread [64K]
  --rate=N                        average transactions rate. 0 for unlimited rate [0]
  --report-interval=N             periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
  --report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
  --debug[=on|off]                print more debugging info [off]
  --validate[=on|off]             perform validation checks where possible [off]
  --help[=on|off]                 print help and exit [off]
  --version[=on|off]              print version and exit [off]
  --config-file=FILENAME          File containing command line options
  --tx-rate=N                     deprecated alias for --rate [0]
  --max-requests=N                deprecated alias for --events [0]
  --max-time=N                    deprecated alias for --time [0]
  --num-threads=N                 deprecated alias for --threads [1]

Pseudo-Random Numbers Generator options:
  --rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
  --rand-spec-iter=N number of iterations used for numbers generation [12]
  --rand-spec-pct=N  percentage of values to be treated as 'special' (for special distribution) [1]
  --rand-spec-res=N  percentage of 'special' values to use (for special distribution) [75]
  --rand-seed=N      seed for random number generator. When 0, the current time is used as a RNG seed. [0]
  --rand-pareto-h=N  parameter h for pareto distribution [0.2]

Log options:
  --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]

  --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  --histogram[=on|off] print latency histogram in report [off]

General database options:

  --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers) [mysql]
  --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  --db-debug[=on|off] print database-specific debug information [off]


Compiled-in database drivers:
  mysql - MySQL driver
  pgsql - PostgreSQL driver

mysql options:
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [3306]
  --mysql-socket=[LIST,...]        MySQL socket
  --mysql-user=STRING              MySQL user [sbtest]
  --mysql-password=STRING          MySQL password []
  --mysql-db=STRING                MySQL database name [sbtest]
  --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
  --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
  --mysql-compression[=on|off]     use compression, if available in the client library [off]
  --mysql-debug[=on|off]           trace all client library calls [off]
  --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]

pgsql options:
  --pgsql-host=STRING     PostgreSQL server host [localhost]
  --pgsql-port=N          PostgreSQL server port [5432]
  --pgsql-user=STRING     PostgreSQL user [sbtest]
  --pgsql-password=STRING PostgreSQL password []
  --pgsql-db=STRING       PostgreSQL database name [sbtest]

Compiled-in tests:
  fileio - File I/O test
  cpu - CPU performance test
  memory - Memory functions speed test
  threads - Threads subsystem performance test
  mutex - Mutex performance test

 

基本語法以下:dom

sysbench [options]... [testname] [command]

command 是sysbench要執行的命令,包括prepare、run和cleanup。prepare是爲測試提早準備數據,run是執行正式的測試,cleanup是在測試完成後對數據庫進行清理

testname  指定了要進行的測試,在老版本的sysbench中,能夠經過--test參數指定測試的腳本;而在新版本中,--test參數已經聲明爲廢棄,能夠不使用--test,而是直接指定腳本。測試時使用的腳本爲lua腳本,能夠使用sysbench自帶腳本,也能夠本身開發。

options 關於MySQL的主要包括MySQL 鏈接信息參數 和 MySQL 執行相關的參數。

4 測試 

Step 1 準備壓測數據

sysbench /usr/share/sysbench/oltp_insert.lua  --mysql-host=XXX.XXX.XXX.XXX  --mysql-port=3306 --mysql-user=testsbuser  --mysql-password='textpwd' --mysql-db=tssysbench --db-driver=mysql  --tables=15  --table-size=500000  --report-interval=10 --threads=128   --time=120 prepare

step 2  壓測

sysbench /usr/share/sysbench/oltp_insert.lua  --mysql-host=XXX.XXX.XXX.XXX  --mysql-port=3306 --mysql-user=testsbuser  --mysql-password='textpwd' --mysql-db=tssysbench --db-driver=mysql  --tables=15  --table-size=500000  --report-interval=10 --threads=128   --time=120 run

也能夠將測試結果導出到文件中,便於後續分析。

 sysbench /usr/share/sysbench/oltp_insert.lua  --mysql-host=XXX.XXX.XXX.XXX  --mysql-port=3306 --mysql-user=testsbuser  --mysql-password='testpwd' --mysql-db=tssysbench --db-driver=mysql  --tables=15  --table-size=500000  --report-interval=10 --threads=128   --time=120 run >> ./mysysbench.log

step 3 清理壓測數據

sysbench /usr/share/sysbench/oltp_insert.lua  --mysql-host=XXX.XXX.XXX.XXX  --mysql-port=3306 --mysql-user=testsbuser  --mysql-password='testpwd' --mysql-db=tssysbench --db-driver=mysql  --tables=15  --table-size=500000  --report-interval=10 --threads=128   --time=120 cleanup

補充

OLTP基準測試模擬了一個簡單的事務處理系統的工做負載。若要對數據庫性能進行測試就要使用OLTP相關的腳本。

在/usr/share/sysbench/目錄下有相關的lua腳本:

壓測結果中包含了不少的信息,其中重點須要咱們關注的信息是:
A:總的事務數 (total number of events);
B:每秒事務數;
C:時間統計信息(最小,平均,最大響應時間,以及95%百分比響應時間);
D:線程公平性統計信息,表示負載的公平性(thread-fairness)。

5.注意事項

(1) 測試數據庫須要提早建立,及時測試帳號擁有建立數據庫的權限。

     --mysql-db 參數指定了測試的數據,默認是sbtest。

    不提早建立,報錯信息以下;

FATAL: `sysbench.cmdline.call_command' function failed: /usr/share/sysbench/oltp_common.lua:83: connection creation failed
(last message repeated 3 times)
FATAL: error 1049: Unknown database 'sysbench_db'
FATAL: `sysbench.cmdline.call_command' function failed: /usr/share/sysbench/oltp_common.lua:83: connection creation failed
FATAL: unable to connect to MySQL server on host 'XXX.XXX.XXX.XXX', port 3306, aborting...
(last message repeated 1 times)
FATAL: error 1049: Unknown database 'sysbench_db'
(last message repeated 1 times)

或是(不指定數據庫)

FATAL: `sysbench.cmdline.call_command' function failed: /usr/share/sysbench/oltp_common.lua:83: connection creation failed
FATAL: error 1049: Unknown database 'sbtest'
FATAL: unable to connect to MySQL server on host 'XXX.XXX.XXX.XXX', port 3306, aborting...

(2)不要在MySQL服務器運行的機器上進行測試,一方面可能沒法體現網絡(哪怕是局域網)的影響,另外一方面,sysbench的運行(尤爲是設置的併發數較高時)會影響MySQL服務器的表現.

(3)逐步增長客戶端的併發鏈接數(--thread參數),觀察在鏈接數不一樣狀況下,MySQL服務器的表現。

(4)若是連續進行屢次測試,注意確保以前測試的數據已經被清理乾淨。

(5)若是生成的報告,圖形化分析,能夠經過gnuplot工具進行分析。

相關文章
相關標籤/搜索