對於現在的計算機來講,整個計算機的性能主要受磁盤IO速度的影響,內存、CPU包括主板總線的速度已經很快了。
基礎檢測方法
1、dd命令
dd命令功能很簡單,就是從一個源讀取數據以bit級的形式寫到一個目標地址,通過這種方式我們就可以檢測我們實際磁盤在linux系統中的讀寫性能,不需要經過任何檢測軟件而就是去讀取數據,一般來講用dd來檢測磁盤的性能也被認為是最接近真實情況。
用法:dd if[數據從哪里讀取,一般來講從dev下的zero設備,這個設備不斷返回0作為數據源] of[把讀取的文件寫入哪個文件] bs[block size,每次讀寫基本快的大小] count[總共讀寫多少個bs] conv=fdatasync[在linux中有很強的內存緩存機制,為了提高系統性能,linux會大量使用內存作為硬盤讀寫緩存,所以這里用這個參數來保證數據是直接寫入硬盤]
示例:
dd if=/dev/zero of=testfile bs=1M count=512 conv=fdatasync
在我的虛擬機上結果如下:
[root@localhost ~]# dd if=/dev/zero of=testfile bs=1M count=512 conv=fdatasync 512+0 records in 512+0 records out 536870912 bytes (537 MB) copied, 19.6677 s, 27.3 MB/s
一般建議多次運行這個命令取平均值,在每次執行上面的命令前,建議用下面的命令來清除緩存:
echo 3 > /proc/sys/vm/drop_caches
通過dd命令測試往往不是很嚴謹也不是很科學,因為可能會受cpu使用率和后台服務影響。
2、hdparm命令
hdparm命令專門用來去獲取修改測試磁盤信息。hdparm必須在管理員權限下運行。
用法:hdparm -t 要測試的磁盤
示例:
# hdparm -t /dev/sda
結果:
[root@localhost ~]# hdparm -t /dev/sda /dev/sda: Timing buffered disk reads: 444 MB in 3.01 seconds = 147.35 MB/sec [root@localhost ~]# hdparm -t /dev/sda /dev/sda: Timing buffered disk reads: 808 MB in 3.00 seconds = 269.21 MB/sec
可以看到兩次運行結果差距比較大,所以建議多次運行取平均值。
用這兩種方式測試出來的結果是非常簡單,專業測試磁盤性能時,不僅需要知道讀寫性能,還要區分讀寫數據大小(4k/16k/32k),還要測試是順序讀寫還是隨機讀寫,如果是機械硬盤還要測試內磁道和外磁道的速率差距等等。
高級檢測方法
1、bonnie++
可通過yum安裝(不包含在linux默認的yum源中,建議安裝repoforge源):
yum install -y bonnie++
用法:bonnie++ -u 用戶名 -s 測試讀寫文件大小
示例:
bonnie++ -u root -s 2g
默認情況下,會寫一個4G大小的文件,分為4份,會通過讀寫隨機操作對系統IO進行全面測試,由於寫的文件比較大,所以時間會比較長。
結果:
[root@localhost ~]# bonnie++ -u root -s 2g Using uid:0, gid:0. Writing a byte at a time...done Writing intelligently...done Rewriting...done Reading a byte at a time...done Reading intelligently...done start 'em...done...done...done...done...done... Create files in sequential order...done. Stat files in sequential order...done. Delete files in sequential order...done. Create files in random order...done. Stat files in random order...done. Delete files in random order...done. Version 1.96 ------Sequential Output------ --Sequential Input- --Random- Concurrency 1 -Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks-- Machine Size K/sec %CP K/sec %CP K/sec %CP K/sec %CP K/sec %CP /sec %CP localhost.locald 2G 287 99 31885 20 59035 15 2795 99 514292 64 9491 421 Latency 42230us 2804ms 284ms 8198us 5820us 4819us Version 1.96 ------Sequential Create------ --------Random Create-------- localhost.localdoma -Create-- --Read--- -Delete-- -Create-- --Read--- -Delete-- files /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP /sec %CP 16 20946 92 +++++ +++ +++++ +++ 23169 94 +++++ +++ +++++ +++ Latency 2539us 853us 993us 1675us 284us 1234us 1.96,1.96,localhost.localdomain,1,1414376948,2G,,287,99,31885,20,59035,15,2795,99,514292,64,9491,421,16,,,,,20946,92,+++++,+++,+++++,+++,23169,94,+++++,+++,+++++,+++,42230us,2804ms,284ms,8198us,5820us,4819us,2539us,853us,993us,1675us,284us,1234us
這個格式實在有點亂,不過好在這個軟件還提供了把結果轉換成html表格的工具(用最后一行轉換):
echo 1.96,1.96,localhost.localdomain,1,1414376948,2G,,287,99,31885,20,59035,15,2795,99,514292,64,9491,421,16,,,,,20946,92,+++++,+++,+++++,+++,23169,94,+++++,+++,+++++,+++,42230us,2804ms,284ms,8198us,5820us,4819us,2539us,853us,993us,1675us,284us,1234us | bon_csv2html >> bon_result.html
bon_result.html的顯示如下:
這個就好看多了,簡單解釋一下:
Sequential Output(順序輸出,實際是寫操作)下的 Per Char是值用putc方式寫,毫無疑問,因為cache的line總是大於1字節的,所以不停的騷擾CPU執行putc,看到cpu使用率是99%.寫的速度是0.3MB/s,非常慢了。
Sequential Output下的block是按照block去寫的,明顯CPU使用率就下來了,速度也上去了,是31MB/s,跟上面用dd測試的差不多。
Sequential Input(順序輸入,實際是讀操作)下的Per Char是指用getc的方式讀文件,速度是2.5MB/s,CPU使用率是99%。
Sequential Input下的block是指按照block去讀文件,速度是50MB/s,CPU使用率是64%。
Random Seeks是隨機尋址,每秒尋址9000多次還算可以的。
Sequential Create(順序創建文件)
Random Create(隨機創建文件)
有的結果是很多+號,這表示bonner++認為值不可靠,於是不輸出。一般來說是因為執行的很快,一般來說不是系統瓶頸,所以不用擔心。
2、iozone
iozone提供的信息更全面和精確,所以iozone是系統性能測試中用的最多的工具之一。
iozone使用略復雜,這里只用最常用的一些參數:
-l:最小的進程數量,用於並發測試,不想測多進程可以設置為1。
-u:最大的進程數量,用於並發測試,不想測多進程可以設置為1。
-r:默認讀寫基本單位,如16k,這個值一般跟測試的應用有關,如要測試數據庫,這個值就跟數據庫的塊大小一致。
-s:默認讀寫的大小,建議這個值大一些(一般為2倍內存大小),因為iozone並不會規避低層的緩存,所以如果值比較小,可能直接在內存中就完成了。
-F:指定緩存文件
示例:
iozone -l 1 -u 1 -r 16k -s 2g -F tempfile
結果:
Children see throughput for 1 initial writers = 31884.46 kB/sec Parent sees throughput for 1 initial writers = 30305.05 kB/sec Min throughput per process = 31884.46 kB/sec Max throughput per process = 31884.46 kB/sec Avg throughput per process = 31884.46 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 rewriters = 102810.49 kB/sec Parent sees throughput for 1 rewriters = 95660.98 kB/sec Min throughput per process = 102810.49 kB/sec Max throughput per process = 102810.49 kB/sec Avg throughput per process = 102810.49 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 readers = 450193.59 kB/sec Parent sees throughput for 1 readers = 450076.28 kB/sec Min throughput per process = 450193.59 kB/sec Max throughput per process = 450193.59 kB/sec Avg throughput per process = 450193.59 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 re-readers = 451833.53 kB/sec Parent sees throughput for 1 re-readers = 451756.47 kB/sec Min throughput per process = 451833.53 kB/sec Max throughput per process = 451833.53 kB/sec Avg throughput per process = 451833.53 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 reverse readers = 61854.02 kB/sec Parent sees throughput for 1 reverse readers = 61851.88 kB/sec Min throughput per process = 61854.02 kB/sec Max throughput per process = 61854.02 kB/sec Avg throughput per process = 61854.02 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 stride readers = 43441.66 kB/sec Parent sees throughput for 1 stride readers = 43439.83 kB/sec Min throughput per process = 43441.66 kB/sec Max throughput per process = 43441.66 kB/sec Avg throughput per process = 43441.66 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 random readers = 47707.72 kB/sec Parent sees throughput for 1 random readers = 47705.00 kB/sec Min throughput per process = 47707.72 kB/sec Max throughput per process = 47707.72 kB/sec Avg throughput per process = 47707.72 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 mixed workload = 50807.69 kB/sec Parent sees throughput for 1 mixed workload = 50806.24 kB/sec Min throughput per process = 50807.69 kB/sec Max throughput per process = 50807.69 kB/sec Avg throughput per process = 50807.69 kB/sec Min xfer = 2097152.00 kB Children see throughput for 1 random writers = 45131.93 kB/sec Parent sees throughput for 1 random writers = 43955.32 kB/sec Min throughput per process = 45131.93 kB/sec Max throughput per process = 45131.93 kB/sec Avg throughput per process = 45131.93 kB/sec Min xfer = 2097152.00 kB
從上面的結果中可以看到各種方式下系統磁盤的讀寫性能。
PS:其實下面還有結果,只是我的虛擬機磁盤滿了,崩潰了。