使用Sysbench對滴滴雲MySQL進行基準測試

Sysbench是一款開源的多線程、跨平臺的基準測試工具,可以執行CPU/內存/線程/IO/數據庫等方面的性能測試。本文使用Sysbench針對滴滴雲的MySQL進行基準測試。

本文使用的MySQL數據庫爲一臺4G內存,50G存儲容量的滴滴雲MySQL數據庫,由於滴滴雲MySQL默認不能通過公網訪問,此數據庫通過一臺滴滴雲虛擬機(DC2)連接,滴滴雲MySQL的使用可以參考以下鏈接,此處不再贅述。

https://help.didiyun.com/hc/kb/article/1143410/

其中關鍵的一步是爲了雲主機可以正常訪問MySQL,需要創建用戶,此處爲方便測試,建議創建高權限賬號。 img 常用的基準測試指標包括

TPS/QPS:衡量吞吐量。

響應時間:包括平均響應時間、最小響應時間、最大響應時間、時間百分比等,其中時間百分比參考意義較大,如前95%的請求的最大響應時間。

併發量:同時處理的查詢請求的數量。

1.在雲主機上安裝MySQL5.7客戶端:

[root@dc2-user]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
[root@dc2-user]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
[root@dc2-user]# yum install mysql-community-client

2. 在雲主機上安裝sysbench

不同版本的sysbench語法有一定差別,本文使用的版本是sysbench1.0。

下載解壓:

[root@dc2-user]# wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbench-1.0.zip"
[root@dc2-user]# unzip sysbench-1.0.zip
[root@dc2-user]# cd sysbench-1.0

安裝依賴:

[root@dc2-user]# yum install automake libtool mysql-devel –y

安裝,確認當前目錄爲sysbench目錄。

[root@dc2-user]# ./autogen.sh
[root@dc2-user]# ./configure
[root@dc2-user]# make && make install

默認sysbench命令在這個路徑下,/usr/local/bin/sysbench,如果未添加/usr/local/bin爲環境變量,爲方便使用,可以爲sysbench命令添加軟鏈。

[root@dc2-user]# ln -s  /usr/local/bin/sysbench /usr/bin/sysbench

驗證命令可用性:

[root@dc2-user]#sysbench --version
sysbench 1.0.20

3. Sysbench命令用法

使用sysbench --help查看sysbench的命令用法,此處只介紹本文用到的一些命令選項和參數。

[root@dc2-user]# sysbench --help
Usage:
  sysbench [options]... [testname] [command]

本例中用到的[options]參數如下: --mysql-host:MySQL服務器主機名,默認localhost;如果在本機上使用localhost報錯,提示無法連接MySQL服務器,改成本機的IP地址應該就可以了。 --mysql-port:MySQL服務器端口,默認3306 --mysql-user:用戶名 --mysql-password:MySQL服務器密碼 --oltp-test-mode:執行模式,包括simple、nontrx和complex,默認是complex。simple模式下只測試簡單的查詢;nontrx不僅測試查詢,還測試插入更新等,但是不使用事務;complex模式下測試最全面,會測試增刪改查,而且會使用事務,本例使用complex。 --oltp-tables-count:測試的表數量,本例選擇10張表。 --oltp-table-size:測試的表的大小,本例選擇100000條數據。 --threads:客戶端的併發連接數,本例選用10。 --time:測試執行的時間,單位是秒,本例選擇120秒。 --report-interval:生成報告的時間間隔,單位是秒,本例選擇10秒。

[testname]指定用什麼lua腳本進行測試,可以是自己編寫的,也可以用sysbench自帶的,自帶腳本位於解壓後的sysbench目錄中tests/include/oltp_legacy下,本例使用oltp.lua腳本,可以模擬併發、建表、插入數據、刪除數據等常規OLTP操作。 [command]選項包括prepare、run和cleanup,即數據準備、測試執行和測試後數據清理。

4. 測試舉例

首先在滴滴雲頁面創建sbtest數據庫。

img

(1)準備數據

[root@dc2-user]# sysbench /home/dc2-user/sysbench-1.0/tests/include/oltp_legacy/oltp.lua --mysql-host=10.254.170.17 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 prepare

sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Creating table 'sbtest1'...
Inserting 100000 records into 'sbtest1'
Creating secondary indexes on 'sbtest1'...
Creating table 'sbtest2'...
Inserting 100000 records into 'sbtest2'
Creating secondary indexes on 'sbtest2'...
Creating table 'sbtest3'...
Inserting 100000 records into 'sbtest3'
Creating secondary indexes on 'sbtest3'...
Creating table 'sbtest4'...
Inserting 100000 records into 'sbtest4'
Creating secondary indexes on 'sbtest4'...
Creating table 'sbtest5'...
Inserting 100000 records into 'sbtest5'
Creating secondary indexes on 'sbtest5'...
Creating table 'sbtest6'...
Inserting 100000 records into 'sbtest6'
Creating secondary indexes on 'sbtest6'...
Creating table 'sbtest7'...
Inserting 100000 records into 'sbtest7'
Creating secondary indexes on 'sbtest7'...
Creating table 'sbtest8'...
Inserting 100000 records into 'sbtest8'
Creating secondary indexes on 'sbtest8'...
Creating table 'sbtest9'...
Inserting 100000 records into 'sbtest9'
Creating secondary indexes on 'sbtest9'...
Creating table 'sbtest10'...
Inserting 100000 records into 'sbtest10'
Creating secondary indexes on 'sbtest10'...

(2)執行測試 可以將執行結果保存在文件中便於後續分析。

[root@dc2-user]# sysbench ./sysbench-1.0/tests/include/oltp_legacy/oltp.lua --mysql-host=10.254.170.17 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --oltp-test-mode=complex --oltp-tables-count=10 --oltp-table-size=100000 --threads=10 --time=120 --report-interval=10 run >> /home/dc2-user/mysysbench.log

(3)清理數據

[root@dc2-user]# sysbench ./sysbench-1.0/tests/include/oltp_legacy/oltp.lua --mysql-host=10.254.170.17 --mysql-port=3306 --mysql-user=root --mysql-password=123456  cleanup

sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Dropping table 'sbtest1'...

##解讀測試結果## 打開所保存的測試結果。

[root@dc2-user]# cat mysysbench.log 
sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 10
Report intermediate results every 10 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 10s ] thds: 10 tps: 358.81 qps: 7186.31 (r/w/o: 5032.37/1435.32/718.61) lat (ms,95%): 39.65 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 10 tps: 342.69 qps: 6850.20 (r/w/o: 4794.16/1370.66/685.38) lat (ms,95%): 44.98 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 10 tps: 364.58 qps: 7296.91 (r/w/o: 5108.46/1459.40/729.05) lat (ms,95%): 38.25 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 10 tps: 371.62 qps: 7428.24 (r/w/o: 5199.11/1485.79/743.34) lat (ms,95%): 38.94 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 10 tps: 355.44 qps: 7112.96 (r/w/o: 4980.30/1421.77/710.89) lat (ms,95%): 43.39 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 10 tps: 377.68 qps: 7551.83 (r/w/o: 5285.47/1511.01/755.35) lat (ms,95%): 36.89 err/s: 0.00 reconn/s: 0.00
[ 70s ] thds: 10 tps: 378.59 qps: 7572.99 (r/w/o: 5301.26/1514.56/757.18) lat (ms,95%): 38.25 err/s: 0.00 reconn/s: 0.00
[ 80s ] thds: 10 tps: 365.19 qps: 7303.79 (r/w/o: 5113.05/1460.46/730.28) lat (ms,95%): 41.10 err/s: 0.00 reconn/s: 0.00
[ 90s ] thds: 10 tps: 354.24 qps: 7084.59 (r/w/o: 4959.65/1416.36/708.58) lat (ms,95%): 40.37 err/s: 0.00 reconn/s: 0.00
[ 100s ] thds: 10 tps: 372.75 qps: 7453.38 (r/w/o: 5216.16/1491.82/745.41) lat (ms,95%): 38.94 err/s: 0.00 reconn/s: 0.00
[ 110s ] thds: 10 tps: 349.95 qps: 7002.81 (r/w/o: 4902.60/1400.20/700.00) lat (ms,95%): 43.39 err/s: 0.00 reconn/s: 0.00
[ 120s ] thds: 10 tps: 371.82 qps: 7432.30 (r/w/o: 5202.68/1486.08/743.54) lat (ms,95%): 37.56 err/s: 0.00 reconn/s: 0.00
SQL statistics:
    queries performed:
        read:                            611030
        write:                           174580
        other:                           87290
        total:                           872900
    transactions:                        43645  (363.59 per sec.)
    queries:                             872900 (7271.87 per sec.)
    ignored errors:                      0      (0.00 per sec.)
    reconnects:                          0      (0.00 per sec.)

General statistics:
    total time:                          120.0362s
    total number of events:              43645

Latency (ms):
         min:                                   18.29
         avg:                                   27.49
         max:                                  677.90
         95th percentile:                       40.37
         sum:                              1199972.75

Threads fairness:
    events (avg/stddev):           4364.5000/72.30
    execution time (avg/stddev):   119.9973/0.01

這裏一般我們關注這兩個指標。

queries:                             872900 (7271.87 per sec.)

95th percentile:                       40.37

我們可以看到QPS爲7271.87,95%請求的最大延遲爲40.37毫秒,對於當前配置的mysql來說,測試結果比較理想。

作者:賀子一【滴滴出行資深系統工程師】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章