1、安裝壓測工具
curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bash yum -y install sysbench
2、執行壓測指令
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable prepare
指令解釋:
- --db-driver=mysql:這個很簡單,就是說他基于mysql的驅動去連接配接mysql資料庫,你要是oracle,或者sqlserver,那 自然就是其他的資料庫的驅動了
- --time=300:這個就是說連續通路300秒
- --threads=10:這個就是說用10個線程模拟并發通路
- --report-interval=1:這個就是說每隔1秒輸出一下壓測情況
- --mysql-host=127.0.0.1 --mysql-port=3306 --mysql-user=test_user --mysql-password=test_user:這一大串,就 是說連接配接到哪台機器的哪個端口上的MySQL庫,他的使用者名和密碼是什麼
- --mysql-db=test_db --tables=20 --table_size=1000000:這一串的意思,就是說在test_db這個庫裡,構造20個測試 表,每個測試表裡構造100萬條測試資料,測試表的名字會是類似于sbtest1,sbtest2這個樣子的
- oltp_read_write:這個就是說,執行oltp資料庫的讀寫測試
- --db-ps-mode=disable:這個就是禁止ps模式 最後有一個prepare,意思是參照這個指令的設定去構造出來我們需要的資料庫裡的資料,他會自動建立20個測試表,每個表 裡建立100萬條測試資料,是以這個工具是非常的友善的。
測試資料庫的綜合讀寫TPS,使用的是oltp_read_write模式(大家看指令中最後不是prepare,是run了,就是運作壓測):
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable run
mysql壓測實戰
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISPrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdsATOfd3bkFGazxCMx8VesATMfhHLlN3XnxCMwEzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iZ5gDZwIDNjRWO3Y2YhFmYzITOmBDOmFGOjhTZkhjM08CXzIzLcJTMwIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjL1M3Lc9CX6MHc0RHaiojIsJye.png)
對測試輸出日志進行解釋[ 300s ] thds: 10 tps: 291.54 qps: 5792.65 (r/w/o: 4051.45/1158.13/583.07) lat (ms,95%): 77.19 err/s: 0.00 reconn/s: 0.00
[300s] ,整個壓測過程第三百秒
thds:10 , 這個意思就是有10個線程在壓測
tps:291.54, 這個意思就是每秒執行了291.54 個事務
qps: 5792.65 ,這個意思就是每秒可以執行5792.65個請
(r/w/o: 4051.45/1158.13/583.07) ,這個意思就是說,在每秒5792.65個請求中,有4051.45個請求是讀請求,1158.13個請 求是寫請求,583.07個請求是其他的請求,就是對QPS進行了拆解
lat (ms,95%): 77.19 ,這個意思就是說,95%的請求的延遲都在77.19毫秒以下
err/s: 0.00 reconn/s: 0.00 這兩個的意思就是說,每秒有0個請求是失敗的,發生了0次網絡重連
這個壓測結果會根據每個人的機器的性能不同有很大的差距(真機 i5 9300H 1T SSD 16GB RAM,目前壓測虛拟機配置 4核4GB RAM 40GB ROM配置),你要是機器性能特别高,那你可以開很多的并發線程去壓測,比 如100個線程,此時可能會發現資料庫每秒的TPS有上千個,如果你的機器性能很低,可能壓測出來你的TPS才二三十個,QPS 才幾百個,這都是有可能的。
另外在完成壓測之後,最後會顯示一個總的壓測報告,我把解釋寫在下面了:
SQL statistics:
queries performed:
read: 1181852 // 這就是說在300s的壓測期間執行了118萬多次的讀請求
write: 337672 // 這是說在壓測期間執行了33萬多次的寫請求
other: 168836 // 這是說在壓測期間執行了16萬多次的其他請求
total: 1688360 // 這是說一共執行了168萬多次的請求
transactions: 84418 (281.37 per sec.) // 這是說一共執行了8.4萬多個事務,每秒執行281.37多個事務
queries: 1688360 (5627.36 per sec.) // 這是說一共執行了168萬多次的請求,每秒執行5600+請求
ignored errors: 0 (0.00 per sec.)
reconnects: 0 (0.00 per sec.)
//下面就是說,一共執行了300s的壓測,執行了8.4萬+的事務
General statistics:
total time: 300.0253s
total number of events: 84418
Latency (ms):
min: 6.90 // 請求中延遲最小的是6.9ms
avg: 35.54 // 請求中延遲平均是35.54ms
max: 287.21 //請求中延遲最大是287.21ms
95th percentile: 77.19 //95%的請求延遲都在77.19ms以内
sum: 2999805.01
Threads fairness:
events (avg/stddev): 8441.8000/46.41
execution time (avg/stddev): 299.9805/0.01
測試資料庫的隻讀性能,使用的是oltp_read_only模式(大家看指令中的oltp_read_write已經變為oltp_read_only了):
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_only --db-ps-mode=disable run
測試資料庫的删除性能,使用的是oltp_delete模式:
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_delete --db-ps-mode=disable run
測試資料庫的更新索引字段的性能,使用的是oltp_update_index模式:
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_update_index --db-ps-mode=disable run
測試資料庫的更新非索引字段的性能,使用的是oltp_update_non_index模式
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_update_non_index --db-ps-mode=disable run
測試資料庫的插入性能,使用的是oltp_insert模式
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_insert --db-ps-mode=disable run
測試資料庫的寫入性能,使用的是oltp_write_only模式:
sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.115.101 --mysql-port=3306 --mysql-user=test_user --mysql-password=123456 --mysql-db=test_db --tables=20 --table_size=1000000 oltp_write_only --db-ps-mode=disable run