sysbench
是一個開源的、模塊化的、跨平臺的多線程性能測試工具,能夠用來進行CPU、內存、磁盤I/O
、線程、數據庫的性能測試。目前支持的數據庫有MySQL
、Oracle
和PostgreSQL
。如下操做都將以支持MySQL
數據庫爲例進行。yum
/apt
等來安裝,另外一種本身下載源碼包來安裝,筆者這裏採用的是centos
,採用yum
安裝sysbench
的源碼包下載地址: http://sourceforge.net/projects/sysbench ,採用源碼安裝的可轉這裏yum
安裝以下:
yum repo
倉庫curl -s https://packagecloud.io/install/repositories/akopytov/sysbench/script.rpm.sh | sudo bashmysql
sudo yum -y install sysbenchlinux
sysbench --versionsql
MySQL
,首先咱們在數據庫建立一個專門用來測試的庫 test_db
sysbench
建立20個測試表,每一個表中建立100萬數據,再經過10個線程對測試庫發起訪問,持續5分鐘,對其進行壓測sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable prepare數據庫
--db-driver=mysql
: 表示數據庫的驅動類型,咱們使用的是 MySQL
因此填mysql
,若是使用 Oracle
則填寫相應的oracle
--time=300
: 這個參數表示持續訪問的時間 300秒--threads=10
: 表示使用10個線程模擬併發訪問--report-interval=1
: 表示每隔一秒輸出以此壓測狀況 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root
: 這一塊的配置,就是基本的數據庫連接信息,指定數據庫IP
,端口,帳號密碼--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
: 表示按照命令設置去構建出咱們的數據,也就是對前面全部命令的執行方案TPS
,使用 oltp_read_write
模式sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable runcentos
注意:命令最後再也不是 以前的 prepare,這裏是 run ,表示運行壓測,前面的是準備數據bash
| tee /tmp/mysysbench.log
整個命令以下:sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable run | tee /tmp/mysysbench.log服務器
oltp_read_only
模式,命令以下:sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --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.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --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.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --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.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --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.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --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.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=test_db --tables=20 --table_size=1000000 oltp_write_only --db-ps-mode=disable run
cleanup
命令來清除構建的測試數據sysbench --db-driver=mysql --time=300 --threads=10 --report-interval=1 --mysql-host=192.168.56.10 --mysql-port=3306 --mysql-user=root --mysql-password=root --mysql-db=test_db --tables=20 --table_size=1000000 oltp_read_write --db-ps-mode=disable cleanup
[ 31s ] thds: 10 tps: 350.35 qps: 5575.50 (r/w/o: 4874.81/0.00/700.69) lat (ms,95%): 36.89 err/s: 0.00 reconn/s: 0.00
咱們來解釋一下其中的含義,首先[31s]
表示這是在第31秒的時候輸出的一段壓測統計結果,其餘字段以下:
thds:10
: 表示當前有10
個線程正在壓測tps:350.35
: 表示當前每秒執行了 350.35
個事務qps:5575.50
: 表示當前每秒能夠執行 5575.50
個請求(r/w/o: 4874.81/0.00/700.69)
: 表示在每秒 5575.50
個請求中,有 4874.81
個讀請求,0.00
個寫請求,700.69
個其餘請求,其實就是對 qps
的細化lat (ms,95%): 36.89
:表示 95% 的請求的延遲都在36.89 毫秒如下err/s: 0.00 reconn/s: 0.00
:表示有0個請求失敗,發生了0次的網絡重連在壓測結束後會輸出一個總的壓測結果,以下:
CPU
、磁盤I/O
、網絡負載等信息,一旦發現任何一個超出必定負荷,則不適合再繼續加大線程數來壓測了sysbench
的線程數,不關係其餘指標,這樣即便機器每秒抗住了很高的qps
,可是這時候的機器已是滿負荷運行了,內存,cpu
,網絡帶寬等都幾乎被打滿,這種狀況機器隨時可能掛掉,這時候的測壓就沒有什麼表明性了,由於你不可能在生產者讓它達到這樣的負荷Linux
下最多見的命令就是top
命令,能夠輸出詳細的狀況,以下:top - 16:24:21 up 13:41, 4 users, load average: 0.11, 3.10, 3.72
16:24:21
: 表示當前的時間up 13:41
:表示已經運行了多長時間4 users
:當前機器有幾個用戶在使用load average: 0.11, 3.10, 3.72
:這是核心。表示 CPU
在 一分鐘、五分鐘、十五分鐘 內的平均負載狀況,簡單解釋一下,假設咱們是一個四核的CPU
,此時若是咱們的CPU
負載是 0.15
,那麼表示這四個核心中連一個核心都沒使用滿,代表總體比較空閒;若是整個負載是1,則表示四個核中有一個已經使用的比較繁忙了,可是其餘三個仍是比較空閒的;若是負載是4,則說明當前四個核心都處於跑滿的狀態,若是負載大於四,假設6,那說明四核的CPU
被超負荷使用也沒法處理完當前的任務,有不少線程與要等待CPU
資源KiB Mem : 498684 total, 6216 free, 366620 used, 125848 buff/cache
498684 total
: 總的內存,差很少 490M
6216 free
:當前可用內存,6M
366620 used
:已使用內存125848 buff/cache
:用於IO
緩衝的內存CPU
和內存使用的同時,咱們還須要檢測磁盤的使用情況,包括IO
吞吐量,IOPS
等信息dstat
命令來查看,若是系統沒有裝能夠經過 yum install dstat
進行安裝dstat -d
命令,能夠看到以下:上面顯示的內存能夠看到存儲的IO吞吐量的詳細數據,每秒讀
19M
數據,每秒寫6521K
的數據,這個吞吐量並非很高,通常普通的機械硬盤能夠到達上百MB
的讀寫量
dstat -r
來查看IOPS
的狀況,以下:如上面結果能夠看到,讀寫分別的
IOPS
的值,也就是說隨即磁盤讀取/寫入每秒多少次,一般來講磁盤的讀寫每秒在兩三百次是可接受的
dstat -n
來查看網卡的具體流量狀況,以下:能夠看到上面所示,網卡每秒接收和發送的數據量。一般來講若是咱們電腦是千兆網卡,那麼每秒鐘網卡的總流量也就在
100M
左右,甚至更低
本文由AnonyStar 發佈,可轉載但需聲明原文出處。
歡迎關注微信公帳號 :雲棲簡碼 獲取更多優質文章
更多文章關注筆者博客 :雲棲簡碼 i-code.online