1. 測試目的
對mysql資料庫進行基準測試,各性能名額進行定量的、可複現的、可對比的測試。
基準測試可以了解為針對系統的一種壓力測試。但基準測試不關心業務邏輯,更加簡單、直接、易于測試,
資料可以由工具生成,不要求真實;而壓力測試一般考慮業務邏輯(如購物車業務),要求真實的資料。
2. 測試環境
2.1 軟體配置

2.1 硬體配置
3. 測試工具
3.1 工具介紹sysbench
本次測試采用通用工具SysBench,是跨平台的基準測試工具,支援多線程,支援多種資料庫;
對資料庫的基準測試的作用,就是分析在目前的配置下(包括硬體配置、OS、資料庫設定等) 資料庫的性能表現,
進而找出MySQL的性能門檻值,并根據實際系統的要求調整硬體配置。
3.2 測試名額
TPS:Transaction Per Second,事務數/秒
一台資料庫伺服器在機關時間内處理事務的個數,每個事務包含18條SQL語句。
QPS:Query Per Second, 查詢量/秒
每秒執行的查詢次數,是對伺服器在規定時間内所處理查詢量多少的衡量标準,即資料庫每秒執行的SQL數,包含insert、select、update、delete等。
響應時間:包括平均響應時間、最小響應時間、最大響應時間、時間百分比等,其中時間百分比參考意義較大,如前95%的請求的最大響應時間。
并發量:同時處理的查詢請求的數量。
4. 安裝步驟
1 #cd /opt
2 #下載下傳sysbench包
3 #wget -c https://github.com/akopytov/sysbench/archive/1.0.12.zip -O "sysbench-1.0.12.zip"
4 #安裝依賴項
5 #yum install autoconf libtool mysql mysql-devel vim unzip
6 #解壓檔案包
7 #unzip sysbench-1.0.12.zip
8 #編譯
9 #cd sysbench-1.0.12
10 #./autogen.sh
11 #./configure
12 #make
13 #make install
上述指令依次執行,安裝完成。查找測試腳本所在路徑:
#find / -name \'*oltp.lua*\'
例如:/opt/sysbench-1.0.12/tests/include/oltp_legacy/oltp.lua
然後進入 cd /opt/sysbench-1.0.12,開始測試。
5. 參數說明
sysbenh測試工具指令,根據測試需要調整參數sysbench [options]... [testname] [command]
sysbench --help 檢視指令的基本參數
表5-1
選項[options] | 備注 |
--test | 腳本路徑 oltp.lua |
--mysql-db | 測試庫名稱 |
--mysql-host | 資料庫IP位址 |
--mysql-port | 端口号 預設3306 |
--mysql-user | 資料庫使用者名,一般是root |
--mysql-password | 資料庫密碼 |
在老版本的sysbench中,可以通過--test參數指定測試的腳本;
而在新版本中,--test參數已經聲明為廢棄,可以不使用--test,而是直接指定腳本。如下兩種結果相同
表5-2
測試項[testname] | 備注 |
--oltp-tables-count | 建表數量 |
--oltp-table-size | 每張表的資料量,預設[10000] |
--time=N | 限制的總執行時間,預設為10s |
--threads=N | 需要使用的線程數,預設為1 |
--report-interval=N | 指定間隔(秒)報告統計資訊,預設為0,禁用 |
--oltp-test-mode=complex | 測試模式,預設complex |
--oltp-test-mode 運作模式包括:
simple 模式下隻測試簡單的查詢;
nontrx 不僅測試查詢,還測試插入更新等,但是不使用事務;
complex 模式下測試最全面,會測試增删改查,而且會使用事務。
備注:如果需要特别測試伺服器隻讀性能,或不使用事務時的性能,可以
選擇simple模式或nontrx模式。
表5-3
指令[command] | 備注 |
prepare | 準備測試資料 |
run | 執行測試 |
cleanup | 清理資料 |
6. 測試方法
6.1 準備測試資料
先登陸mysql資料庫:
#mysql -uroot -p密碼 -h 主機ip
執行show global variables統計各參數配置:show global status
建立測試庫sbtest:
>create database sbtest;
檢視所有庫名:
>show databases;
exit退出mysql,進入目錄 cd /opt/sysbench-1.0.12
開始測試前,首先要生成測試資料,
執行以下指令:
#sysbench --test=/opt/sysbench-1.0.12/tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=ip --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --oltp-tables-count=200 --oltp-table-size=100000 --report-interval=2 prepare
其中,prepare表示準備資料,在的sbtest庫生成測試資料,
建立200張表,每張表10萬條資料,每2秒顯示一次報告。
注意:這裡不需要設定運作時間(time),執行時間受建表數量影響。
運作過程如下圖所示:
6.2 建表語句
資料準備階段建表結構如下:
CREATE TABLE `sbtest` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`k` INTEGER UNSIGNED DEFAULT \'0\' NOT NULL,
`c` CHAR(120) DEFAULT \'\' NOT NULL,
`pad` CHAR(60) DEFAULT \'\' NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
說明:以上SQL無需執行建表,在下面介紹測試方法,資料準備階段,執行指令時工具自動安照上面描述格式建立表。
6.3 執行測試方法
測試指令如下:
#sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=ip --mysql-port=3306 --mysql-user=root --mysql-password=密碼
--oltp-test-mode=complex --time=60 --threads=1 --report-interval=2 run
其中, run表示運作測試,單線程 運作60秒。
--oltp-tables-count(表個數)
--oltp-table-size(表資料量)
這兩個參數用于準備階段建立表的大小,在運作階段不受影響,故run階段可以取消這兩個參數,結果不受影響。
輸出結果如下圖:
由上圖可知,單線程下TPS為33,QPS為660;
6.3.1 測試事務包含的SQL
Sysbench自帶測試腳本,路徑如下/opt/sysbench-1.0.12/tests/include/oltp_legacy/oltp.lua
預設送出的事務中包含18條SQL語句:
主鍵SELECT語句,10條:
SELECT c FROM ${rand_table_name} where id=${rand_id};
範圍SELECT語句,4條:
SELECT c FROM ${rand_table_name} WHERE id BETWEEN ${rand_id_start} AND ${rand_id_end};
SELECT SUM(K) FROM ${rand_table_name} WHERE id BETWEEN ${rand_id_start} AND ${rand_id_end};
SELECT c FROM ${rand_table_name} WHERE id BETWEEN ${rand_id_start} AND ${rand_id_end} ORDER BY c;
SELECT DISTINCT c FROM ${rand_table_name} WHERE id BETWEEN ${rand_id_start} AND ${rand_id_end} ORDER BY c;
UPDATE語句,2條:
UPDATE ${rand_table_name} SET k=k+1 WHERE id=${rand_id}
UPDATE ${rand_table_name} SET c=${rand_str} WHERE id=${rand_id}
DELETE語句,1條:
DELETE FROM ${rand_table_name} WHERE id=${rand_id}
INSERT語句,1條:
INSERT INTO ${rand_table_name} (id, k, c, pad) VALUES (${rand_id},${rand_k},${rand_str_c},${rand_str_pad});
6.3.2 清理測試資料
#sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=ip
--mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-tables-count=600 cleanup
其中,cleanup表示清理資料,這裡不需要設定運作時間(time),隻需設定表的數量即可,
此時需表的數量>=準備階段表的數量,才能全部清除測試資料,以免對下次結果産生影響;
7. 測試用例
7.1 測試用例(一)
測試用例1:建立6張表每表預置1000萬行資料測試 | |
測試目的 | 對大表進行增删改查場景下的QPS和TPS |
前置條件 | 7張表,每張表1000萬行資料,每次修改并發數 |
步驟 | 1.準備測試資料(8張表會提示空間不足,因為存儲隻有40G) sysbench --test=/opt/sysbench1.0.12/tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=ip --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --oltp-tables-count=7 --oltp-table-size=10000000 --report-interval=2 prepare 2.運作測試 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --time=600 --threads=32 --report-interval=2 --oltp-tables-count=64 run 3.記錄結果,并依次增加線程數threads分别記錄線程數為 1/ 16/ 32/ 64/ 128 ……情況下的名額及資源情況 4.清除測試資料 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-tables-count=7 cleanup |
參數化變量 | --threads:線程數,增加線程數觀察瓶頸 |
擷取名額 | 1、TPS 2、QPS 3、總事務數 4、前95%延遲時間 |
7.2 測試用例(二)
測試用例2:建立6張表每表預置100萬行資料測試 | |
測試目的 | 與用例1進行對比,觀察表資料量的多少,對結果是否有影響 |
前置條件 | 7張表,每張表10萬行資料,每次修改并發數 |
步驟 | 1.準備測試資料 sysbench --test=/opt/sysbench-1.0.12/tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --oltp-tables-count=7 --oltp-table-size=100000 --report-interval=2 prepare 2.運作測試 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --time=600--threads=32 --report-interval=2 --oltp-tables-count=64 run 3.記錄結果,并依次增加線程數threads分别記錄線程數為 1/ 16/ 32/ 64/ 128 ……情況下的名額及資源情況 4.清除測試資料 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-tables-count=7 cleanup |
參數化變量 | --threads:線程數,增加線程數觀察瓶頸 |
擷取名額 | 1、TPS 2、QPS 3、總事務數 4、前95%延遲時間 |
7.3 測試用例(三)
測試用例3:建立200張表每表預置100萬行資料 | |
測試目的 | 與用例2相比,單表數量一緻,增加表的數量進行觀察 |
前置條件 | 200張表,每張表10萬行資料,每次修改并發數 |
步驟 | 1.準備測試資料 sysbench --test=/opt/sysbench-1.0.12/tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --oltp-tables-count=200 --oltp-table-size=100000 --report-interval=2 prepare 2.運作測試 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-test-mode=complex --time=600 --threads=128 --report-interval=2 --oltp-tables-count=64 run 3.記錄結果,并依次增加線程數threads分别記錄線程數為 1/ 16/ 32/ 64/ 128 ……情況下的名額及資源情況 4.清除測試資料 sysbench ./tests/include/oltp_legacy/oltp.lua --mysql-db=sbtest --mysql-host=IP --mysql-port=3306 --mysql-user=root --mysql-password=密碼 --oltp-tables-count=200 cleanup |
參數化變量 | --threads 線程數,增加線程數觀察瓶頸 |
擷取名額 | 1、TPS 2、QPS 3、總事務數 4、前95%延遲時間 |
8. 測試結果分析
8.1 6張表每表預置1000萬行資料測試
8.1.1 測試結果
線程數 | 1 | 16 | 32 | 64 | 96 | 128 | 160 | 256 | 512 |
TPS(/s) | |||||||||
QPS(/s) | |||||||||
事務總數(萬) | |||||||||
前95%延遲時間(ms) |
8.1.2 測試過程記錄
1)資料準備階段:
說明:準備完成後,表空間約13G;
2)執行測試階段:
①連接配接數1:
8.1.3 資源情況
檢視雲服務自帶監控;
8.1.4 結果分析
根據測試結果進行描述;
後
續
省
略
。。。。
9. 測試結果總結
總結:目前mysql測試環境,在并發線程數達到160左右時,TPS約900/s左右,QPS約1.8萬/s左右,95%事務平均延遲達到300ms,
再增加并發資料後,QPS變化不大,但延遲時間明顯加長。測試過程整體CPU使用率較高,在達到最大TPS及QPS時此時CPU使用率達到93%以上。
特别說明:此總結隻适用于本次測試。