https://blog.csdn.net/everything1209/article/details/54232900
什麼是基準測試
定義
基準測試是一種測量和評估軟件性能指標的活動,用於建立某個時刻的性能基準,以便當系統發生軟硬件變化時重新進行基準測試以評估變化對性能的影響
基準測試是針對系統設置的一種壓力測試,但是和壓力測試還是有區別的
- 基準測試:直接、簡單,易於比較,用於評估服務器的處理能力
- 基準測試:可能不關心業務邏輯,所使用的查詢和業務的真實性可以和業務環境沒有關係
- 壓力測試:對真實的業務數據進行測試,獲得真實系統所能承受的壓力
- 壓力測試:需要針對不同的應用場景,所使用的數據和查詢也是真實用到的
如何進行基準測試
基準測試的目的
- 建立Mysql服務器的性能基準線
- 模擬比當前系統更高的負載,以找出系統的擴張瓶頸,增加數據庫的併發,觀察QPS、TPS變化,確定併發量與性能最優的關係
- 測試不同硬件、軟件、操作系統配置(mysql版本,磁盤配置等)
- 證明新的硬件設備是否配置正確
如何進行基準測試
對整個系統進行基準測試
- 優點:能夠測試整個系統性能,包括web服務器緩存、數據庫等,能反映出系統中各個組件接口間的性能問題,體現真實性能狀況
- 缺點:測試設計複雜,消耗時間長
- 從系統入口進行測試(如網站web前端,手機APP前端)
單獨對Mysql進行基準測試
- 優點:測試設計簡單,所需耗費時間短
- 缺點:無法全面瞭解整個系統的性能基準線
mysql基準測試的性能指標
- 單位時間內所處理的事務數(TPS)
- 單位時間內所處理的查詢數(QPS)
- 響應時間(平均響應時間,最小響應時間,最大響應時間,各時間所佔百分比)
- 併發量:同時處理的查詢請求的數量(併發量不等於連接數)
基準測試的步驟
計劃和設計基準測試
- 對整個系統還是某一組件
- 使用什麼樣的數據(比如使用生產環境的數據庫備份)
- 準備基準測試和數據收集腳本(CPU使用率、IO、網絡流量、狀態和計數器信息等)
- 運行基準測試
- 保存和分析基準測試結果
基準測試中容易忽略的問題
- 使用生產環境數據時,只使用了部分數據(應使用全部備份數據)
- 在多用戶場景中,只做了單用戶的測試
- 在單服務器上測試分佈式應用(推薦使用相同架構進行測試)
- 反覆執行同一查詢(容易緩存命中,無法反應真實查詢性能)
常用的基準測試工具介紹
Mysql基準測試工具之mysqlslap
mysql5.1版本後自帶的基準測試工具,隨Mysql一起安裝
特點:
- 可以模擬服務器負載,並輸出相關統計信息
- 可以指定也可以自動生成查詢語句
# mysqlslap --help 查看幫助信息
--auto-generate-sql #由系統自動生成SQL腳本進行測試
--auto-generate-sql-add-autoincrement #在生成的表中增加自增的ID
--auto-generate-sql-load-type #指定測試中使用的查詢類型
--auto-generate-sql-write-number #指定初始化數據時生成的數據量
--concurrency #指定併發線程的數量
--engine #指定測試表的存儲引擎,可以用逗號分隔多個表引擎
--no-drop #指定不清理測試數據
--iterations #指定測試運行的次數
--number-of-queries #指定每一個線程執行的查詢數量
-number-int-cols #指定測試表中包含int類型列的數量
--number-char-cols #指定測試表包含varchar類型列的數量
--create-schema #指定了用於測試的數據庫的名字
--query #用於指定自定義SQL的腳本
--only-print #並不運行測試腳本,而是把生成的腳本打印出來
#開始測試
mysqlslap --concurrency=1,50,100,200 --iterations=3 --number-int-cols=5 --number-char-cols=10 --auto-generate-sql --auto-generate-sql-add-autoincrement --engine=myisam,innodb --number-of-queries=10 --create-schema=sqltest -p
#上述命令基準測試結果,可以看出innodb在併發時比myisam快很多倍
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 0.015 seconds
Minimum number of seconds to run all queries: 0.015 seconds
Maximum number of seconds to run all queries: 0.016 seconds
Number of clients running queries: 1
Average number of queries per client: 10
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 1.237 seconds
Minimum number of seconds to run all queries: 1.061 seconds
Maximum number of seconds to run all queries: 1.533 seconds
Number of clients running queries: 50
Average number of queries per client: 0
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 2.559 seconds
Minimum number of seconds to run all queries: 2.525 seconds
Maximum number of seconds to run all queries: 2.598 seconds
Number of clients running queries: 100
Average number of queries per client: 0
Benchmark
Running for engine myisam
Average number of seconds to run all queries: 5.552 seconds
Minimum number of seconds to run all queries: 5.510 seconds
Maximum number of seconds to run all queries: 5.619 seconds
Number of clients running queries: 200
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.026 seconds
Minimum number of seconds to run all queries: 0.026 seconds
Maximum number of seconds to run all queries: 0.028 seconds
Number of clients running queries: 1
Average number of queries per client: 10
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.124 seconds
Minimum number of seconds to run all queries: 0.107 seconds
Maximum number of seconds to run all queries: 0.155 seconds
Number of clients running queries: 50
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.136 seconds
Minimum number of seconds to run all queries: 0.122 seconds
Maximum number of seconds to run all queries: 0.153 seconds
Number of clients running queries: 100
Average number of queries per client: 0
Benchmark
Running for engine innodb
Average number of seconds to run all queries: 0.279 seconds
Minimum number of seconds to run all queries: 0.259 seconds
Maximum number of seconds to run all queries: 0.313 seconds
Number of clients running queries: 200
Average number of queries per client: 0
Mysql基準測試工具之sysbench
sysbench的安裝
#源碼安裝的方式
wget https://github.com/akopytov/sysbench/archive/0.5.zip
unzip 0.5.zip
cd sysbench-0.5/
#指定mysql的include目錄和libs目錄,這裏需要根據實際環境。
#可以通過whereis mysql查看
#[root@fu020 mysql]# whereis mysql
#mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql #/usr/share/man/man1/mysql.1.gz
./configure --with-mysql-includes=/usr/include/mysql --with-mysql-libs=/usr/lib64/mysql
make && make install
sysbench --help
#centos可以用yum
yum install sysbench
sysbench常用參數
--test #用於指定所要執行的測試類型,支持以下參數
Fileio #文件系統I/O性能測試
cpu #cpu性能測試
memory #內存性能測試
oltp #測試要指定具體的lua腳本
#lua腳本位於 sysbench-0.5/sysbench/tests/db
--mysql-db #用於指定執行基準測試的數據庫名
--mysql-table-engine #用於指定所使用的存儲引擎
--oltp-tables-count #執行測試的表數量
--oltp-table-size #指定每個表中的數據行數
--max-time #指定最大的測試時間
--report-interval #指定間隔多長時間輸出一次統計信息
--mysql-user #指定執行測試的mysql用戶
--mysql-password #指定執行測試的mysql用戶的密碼
prepare #用於準備測試數據
run #用於實際執行測試
cleanup #用於清理測試數據
sysbench測試
1.測試CPU
sysbench --test=cpu --cpu-max-prime=10000 run #只計算了單核
2.測試I/O
#生成測試I/O的文件,這裏是生成了2G
cd /tmp
sysbench --test=fileio --file-total-size=2G prepare
#執行I/O測試
#以下是採用20個進程,隨機讀寫,1秒生成測試報告
sysbench --test=fileio --num-threads=20 --init-rng=on --file-total-size=2G --file-test-mode=rndrw --report-interval=1 run
#I/O測試數據
[root@fu020 tmp]# sysbench --test=fileio --num-threads=20 --init-rng=on --file-total-size=2G --file-test-mode=rndrw --report-interval=1 run
sysbench 0.5: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 20
Report intermediate results every 1 second(s)
Random number generator seed is 0 and will be ignored
Extra file open flags: 0
128 files, 16Mb each
2Gb total file size
Block size 16Kb
Number of IO requests: 10000
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads...
Threads started!
[ 1s] reads: 17.94 MB/s writes: 11.97 MB/s fsyncs: 2413.70/s response time: 16.267ms (95%)
[ 2s] reads: 17.84 MB/s writes: 11.89 MB/s fsyncs: 2434.03/s response time: 16.786ms (95%)
[ 3s] reads: 17.23 MB/s writes: 11.48 MB/s fsyncs: 2320.05/s response time: 19.386ms (95%)
[ 4s] reads: 16.41 MB/s writes: 10.81 MB/s fsyncs: 2204.98/s response time: 16.973ms (95%)
[ 5s] reads: 16.02 MB/s writes: 10.83 MB/s fsyncs: 2272.00/s response time: 20.533ms (95%)
Operations performed: 6008 reads, 3992 writes, 12800 Other = 22800 Total
Read 93.875Mb Written 62.375Mb Total transferred 156.25Mb (28.462Mb/sec)
1821.55 Requests/sec executed
General statistics:
total time: 5.4898s
total number of events: 10000
total time taken by event execution: 58.1353s
response time:
min: 0.00ms
avg: 5.81ms
max: 69.19ms
approx. 95 percentile: 17.93ms
Threads fairness:
events (avg/stddev): 500.0000/34.00
execution time (avg/stddev): 2.9068/0.09
3.測試數據庫
測試數據庫,需要先建立測試用的數據庫,和測試用的用戶
mysql> create database sysbenchtest;
#以下可以省略用現有用戶
mysql> grant all privileges on *.* to sbtest@'localhost' identified by '$3F4286';
#進入lua腳本測試目錄 /sysbench-0.5/sysbench/tests/db
cd /hotdata/soft/sysbench-0.5/sysbench/tests/db
//======================================================================
//下方爲sysbench0.4.12的使用方式,這個版本不能一次生成多張表,不能使用--oltp-tables-count參數
#準備數據庫測試數據,注意如果是使用yum安裝的,需要指定--db-driver,還有就是密碼最好用單引號包住
#在sysbenchtest測試庫中生成了一個100W條數據的innodb表
sysbench --test=oltp --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --db-driver=mysql prepare
//======================================================================
//下方爲sysbench0.5的使用方式,這個版本可以一次生成多張表,使用--oltp-tables-count參數
//以下生成10張100W數據的表
sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock prepare
#啓用數據庫信息收集
bash get_test_info.sh #下方有貼出代碼
#執行測試
sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock run
#使用下方提供的analysis.sh腳本分析收集的數據
./analysis.sh /hotdata/temp/benchmarks/5-sec-status-2017-01-08_08-status
//基準測試
[root@fu020 db]# sysbench --test=./oltp.lua --mysql-table-engine=innodb --mysql-db=sysbenchtest --oltp-table-size=1000000 --mysql-user=root --mysql-password='youpassword' --oltp-tables-count=10 --mysql-socket=/hotdata/database/data/mysql/mysql.sock run
sysbench 0.5: multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 1
Random number generator seed is 0 and will be ignored
Initializing worker threads...
Threads started!
OLTP test statistics:
queries performed:
read: 140000
write: 40000
other: 20000
total: 200000
transactions: 10000 (30.35 per sec.)
read/write requests: 180000 (546.34 per sec.)
other operations: 20000 (60.70 per sec.)
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
General statistics:
total time: 329.4640s
total number of events: 10000
total time taken by event execution: 329.4293s
response time:
min: 5.30ms
avg: 32.94ms
max: 769.65ms
approx. 95 percentile: 120.47ms
Threads fairness:
events (avg/stddev): 10000.0000/0.00
execution time (avg/stddev): 329.4293/0.00
#get_test_info.sh
#!/bin/bash
INTERVAL=5 #腳本運行間隔
PREFIX=/hotdata/temp/benchmarks/$INTERVAL-sec-status #狀態信息記錄到什麼位置
RUNFILE=/hotdata/temp/benchmarks/running #運行標識
echo "1" > $RUNFILE #生成運行標識
MYSQL=/usr/bin/mysql #mysql命令的路徑
$MYSQL -e "show global variables" >> mysql-variables #mysql的設置信息
while test -e $RUNFILE; do
file=$(date +%F_%I)
sleep=$(date +%s.%N | awk '{print 5 - ($1 % 5)}')
sleep $sleep #腳本的運行時間
ts="$(date +"TS %s.%N %F %T")"
loadavg="$(uptime)" #收集腳本的負載情況
echo "$ts $loadavg" >> $PREFIX-${file}-status #記錄到文件中
$MYSQL -e "show global status" >> $PREFIX-${file}-status & #收集mysql全局狀態信息並記錄到文件
echo "$ts $loadavg" >> $PREFIX-${file}-innodbstatus
$MYSQL -e "show engine innodb status" >> $PREFIX-${file}-innodbstatus & #收集innodb狀態信息
echo "$ts $loadavg" >> $PREFIX-${file}-processlist
$MYSQL -e "show full processlist\G" >> $PREFIX-${file}-processlist &
echo $ts
done
echo Exiting because $RUNFILE does not exists
#Analysis.sh
#!/bin/bash
awk '
BEGIN {
printf "#ts date time load QPS";
fmt=" %.2f";
}
/^TS/ {
ts = substr($2,1,index($2,".")-1);
load = NF -2;
diff = ts - prev_ts;
printf "\n%s %s %s %s",ts,$3,$4,substr($load,1,length($load)-1);
prev_ts=ts;
}
/Queries/{
printf fmt,($2-Queries)/diff;
Queries=$2
}
' "$@"
參考鏈接:
mysqlslap 使用總結
sysbench安裝、使用、結果解讀
sysbench 安裝、使用和測試
mysql benchmark基準測試