FIO磁盤性能測試工具
簡介
一般我們測試硬盤或者存儲的性能的時候,會用Linux系統自帶的dd命令,因爲是自帶命令,簡單易使用,因此一些客戶喜歡使用dd命令來測試磁盤的讀寫性能。
但是用dd命令來測試性能,有如下問題:
1. dd命令的IO模型單一,只能測試順序IO,不能測試隨機IO。
2. dd命令可設置的參數較少,並且測試結果不一定能反映出磁盤的真實性能。
3. dd命令的設計初衷就不是用例測試性能的,在dd的手冊中可以看到。
4. 無法設置隊列深度,因此不推薦用dd命令來測試最大讀IOPS。
因此,我們要使用更專業的磁盤性能測試工具來測試,目前主流的第三方IO測試工具有fio、iometer和Orion。
iometer 是Windows下使用方便,Orion是Oracle的IO測試軟件,而FIO是我們比較常用的在Linux系統下(當然在Windows下安裝cygwin也可以運行FIO)的IO系統Benchmark和壓力測試工具,可以模擬不同IO場景下的IO負載。
FIO支持 13種不同的 I/O 引擎, 包括:sync,mmap, libaio, posixaio, SG v3, splice, null, network,syslet,guasi,solarisaio 等等。
根據實際業務的場景,一般將 I/O 的表現分爲四種場景,隨機讀、隨機寫、順序讀、順序寫。FIO 工具允許指定具體的應用模式,配合多線程對磁盤進行不同深度的測試。
安裝
FIO默認已經存在於yum倉庫裏,所以我們yum安裝即可
yum install -y libaio-devel yum install -y fio-3.7-2.el7.x86_64
運行格式
fio [options] [job options] <job file(s)>
工作原理
使用 FIO 工具測試硬盤性能,首先確定待模擬IO負載的IO參數,如I/O type,Block size,I/O engine,I/O depth,Target file/device等
然後需要編寫一個作業(命令行或者腳本job file)來預定義 FIO 將要以什麼樣的模式來執行任務。
作業中可以包含任意數量的線程或文件,一個作業的典型模式是定義一部分的全局共享參數global 段和一個(或多個)作業的job 段實體對象(即硬盤)。
運行 FIO 作業時,FIO 會自上而下解析作業中的配置,並做相應的調整去執行任務。
測試完成後,輸出測試結果,對FIO測試結果進行分析,評估IO系統的設計或優化方案或確定debug思路。
FIO 中的常用選項如下
filename: 支持文件系統或者裸設備(硬盤或分區),-filename=/dev/sda2 或 -filename=/tmp/fio_read_test.txt 或 -filename=/dev/sda
direct: 1表示測試過程繞過機器自帶的buffer,相當於o_direct,0表示使用bufferio
runtime:測試時長,單位秒,如果不寫則直接寫5GB文件
time_based: 如果在runtime指定的時間還沒到時文件就被讀寫完成,將繼續重複直到runtime時間結束,加上這個參數防止job提前結束。
size: 向硬盤中寫入/讀取測試文件的大小,可以是絕對值MB,KB,GB,也可以是百分比,佔所測磁盤的百分比%,
group_reporting:彙總所有的信息,而不是每個job都顯示具體的結果
thread:fio默認會使用fork()創建job進程,如果這個選項設置的話,fio將使用pthread_create來創建線程
numjobs:創建的線程或進程數量,建議等於 CPU 的 core 值,,如果要測試吞吐量,那麼numjobs要設置小一點
rw: 讀寫方式,順序讀read,順序寫write,隨機讀randread ,隨機寫randwrite,混合讀寫randrw,如果要測試吞吐量,需要設爲順序讀read,順序寫write
rwmixread:70,混合讀寫7:3 ,配合rw = randrw
rwmixwrite:30 在混合讀寫的模式下7:3
bs: 單次io的塊大小,一般測試4k, 8k, 64k, 128k, 1M
bsrange: =512-2048 同上,提定數據塊的大小範圍
allow_mounted_write:允許虛擬機寫入
ioengine:FIO 工作時使用的引擎。引擎決定了使用什麼樣的 I/O,同步模式、異步模式,如果要使用libaio引擎,需要yum install -y libaio-devel包,要使用異步模式引擎才能設置iodepth
iodepth:I/O 引擎如果使用異步模式,要保持多少的隊列深度,沒有設置則使用操作系統默認隊列深度,如果要測試吞吐量,那麼iodepth要設置小一點
nrfiles:每個進程/線程生成文件的數量,表示負載將分發到幾個文件中。
使用測試
編寫 FIO 任務文件
要執行一個 FIO 任務前,需要先定義一個 FIO 任務作業。
按參數輸入方式分爲如下兩種作業定義方式:
(1) 命令行方式(命令行方式一次測試一個磁盤):如fio –filename=/dev/sdb –direct=1 –rw=randread –bs=4k –size=60G –numjobs=64 –runtime=10 –group_reporting –name=test
(2) fio job_file 方式(job_file方式一次測試多個磁盤,每個磁盤寫在不同的job段,官方推薦這種方式):如job_file 的名字爲 test,則運行方式爲 fio test。
按FIO工作模式分爲如下兩種:
(1)client/server方式:FIO工作模式可以分爲前端和後端,即client端和server端。server端執行fio --server,client端執行fio --client=hostname jobfile,這樣client端就可以把jobfile發到server端來執行,hostname爲server端的IP。
(2)本地執行:命令行 job file。
FIO 任務腳本模板,如果filename寫在global段其他job段不寫filename,則跟命令行一樣,一次只測試一個磁盤
cat fio.conf [global] ioengine=libaio iodepth=4 direct=1 runtime=60 time_based=1 size=1G group_reporting=1 thread=1 numjobs=2 bs=4k name='test' allow_mounted_write=1 nrfiles=1 #filename=/data/total.txt [seqread] rw=read filename=/data/fio_read_test.txt [seqwrite] rw=write filename=/data/fio_write_test.txt
上面的job_file文件轉換爲命令行形式就是下面的樣子
#read 順序讀 fio -ioengine=libaio -direct=1 -bs=4k -nrfiles=1 -thread -rw=read -size=1G -filename=/data/fio_read_test.txt -name='test' -iodepth=4 -runtime=30 -numjobs=2 -time_based=1 -allow_mounted_write=1 -group_reporting #write 順序寫 fio -ioengine=libaio -direct=1 -bs=4k -nrfiles=1 -thread -rw=write -size=1G -filename=/data/fio_write_test.txt -name='test' -iodepth=4 -runtime=30 -numjobs=2 -time_based=1 -allow_mounted_write=1 -group_reporting
測試結果解讀
-------------------------------------------------------------------- 展示測試的基本信息 fio fio.conf seqread: (g=0): rw=read, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=libaio, iodepth=4 ... seqwrite: (g=0): rw=write, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=libaio, iodepth=4 ... fio-3.7 Starting 4 threads -------------------------------------------------------------------- 總共4個job, seqread是2線程,所以是2,seqwrite是2線程,所以是2,加起來4 Jobs: 4 (f=4): [R(2),W(2)][100.0%][r=89.3MiB/s,w=188KiB/s][r=22.9k,w=47 IOPS][eta 00m:00s] -------------------------------------------------------------------- 重點看這裏 IOPS 最大IOPS BW 最大帶寬 延時時間 slat表示磁盤需要多久將io提交到kernel做處理 clat表示命令提交到了內核,提交到內核到io完成的時間。 Lat表示從io結果提創建到clat完成,一般說時延,就是看這個值。 clat各個延時所佔的百分比。 seqread: (groupid=0, jobs=4): err= 0: pid=11385: Thu Sep 22 17:09:31 2022 read: IOPS=20.5k, BW=80.0MiB/s (83.9MB/s)(4803MiB/60015msec) slat (nsec): min=1, max=163324k, avg=53835.94, stdev=297380.37 clat (usec): min=50, max=1179.2k, avg=330.98, stdev=4855.59 lat (usec): min=99, max=1179.2k, avg=386.15, stdev=4864.96 clat percentiles (usec): | 1.00th=[ 113], 5.00th=[ 116], 10.00th=[ 119], 20.00th=[ 123], | 30.00th=[ 127], 40.00th=[ 131], 50.00th=[ 137], 60.00th=[ 141], | 70.00th=[ 153], 80.00th=[ 314], 90.00th=[ 367], 95.00th=[ 388], | 99.00th=[ 490], 99.50th=[ 898], 99.90th=[ 58983], 99.95th=[ 90702], | 99.99th=[160433] bw ( KiB/s): min= 7, max=85555, per=50.24%, avg=41180.02, stdev=16441.93, samples=238 iops : min= 1, max=21388, avg=10294.83, stdev=4110.45, samples=238 -------------------------------------------------------------------- 同上 write: IOPS=41, BW=165KiB/s (169kB/s)(9952KiB/60190msec) slat (usec): min=13, max=42741, avg=118.82, stdev=1177.06 clat (msec): min=2, max=1708, avg=193.14, stdev=118.53 lat (msec): min=2, max=1708, avg=193.26, stdev=118.48 clat percentiles (msec): | 1.00th=[ 33], 5.00th=[ 77], 10.00th=[ 97], 20.00th=[ 120], | 30.00th=[ 142], 40.00th=[ 161], 50.00th=[ 182], 60.00th=[ 205], | 70.00th=[ 228], 80.00th=[ 255], 90.00th=[ 288], 95.00th=[ 321], | 99.00th=[ 472], 99.50th=[ 726], 99.90th=[ 1703], 99.95th=[ 1703], | 99.99th=[ 1703] bw ( KiB/s): min= 7, max= 230, per=50.76%, avg=83.76, stdev=35.04, samples=235 iops : min= 1, max= 57, avg=20.55, stdev= 8.71, samples=235 -------------------------------------------------------------------- lat指的是時延,50=0.06%,表示有0.06%的io時延小於50ms。 lat (usec) : 100=0.08%, 250=77.12%, 500=21.64%, 750=0.40%, 1000=0.09% lat (msec) : 2=0.09%, 4=0.09%, 10=0.11%, 20=0.03%, 50=0.05% lat (msec) : 100=0.09%, 250=0.17%, 500=0.04%, 750=0.01%, 1000=0.01% -------------------------------------------------------------------- usr表示用戶cpu佔用率,sys表示系統cpu佔用率,ctx爲上下文切換次數,majf是主要頁面錯誤數量, minf是次要頁面錯誤數量 cpu : usr=3.85%, sys=27.45%, ctx=20221, majf=0, minf=18 -------------------------------------------------------------------- 隊列深度,Submit和complete表示同一時段fio發送和完成的io數據量。Issued爲發送的io數量,latency用於調節吞吐量直到達到預設的延遲目標。 IO depths : 1=0.1%, 2=0.1%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, >=64=0.0% submit : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0% complete : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0% issued rwts: total=1229695,2488,0,0 short=0,0,0,0 dropped=0,0,0,0 latency : target=0, window=0, percentile=100.00%, depth=4 -------------------------------------------------------------------- 最後是結果的彙總 util:磁盤利用率 Run status group 0 (all jobs): READ: bw=80.0MiB/s (83.9MB/s), 80.0MiB/s-80.0MiB/s (83.9MB/s-83.9MB/s), io=4803MiB (5037MB), run=60015-60015msec WRITE: bw=165KiB/s (169kB/s), 165KiB/s-165KiB/s (169kB/s-169kB/s), io=9952KiB (10.2MB), run=60190-60190msec Disk stats (read/write): sdb: ios=1229695/2526, merge=0/8, ticks=287074/481664, in_queue=767911, util=100.00% --------------------------------------------------------------------
關於IO隊列深度
在某個時刻,有N個inflight的IO請求,也就是緩衝的IO請求,包括在隊列中的IO請求,而N就是隊列深度。
加大硬盤隊列深度就是讓硬盤不斷工作,減少硬盤的空閒時間。
加大隊列深度 -> 提高IO利用率 -> 獲得更高的IOPS和MBPS峯值 ,但是隊列深度增加了,IO在隊列的等待時間也會增加,導致IO響應時間變大,這個是值得考慮的問題。
就好比一部電梯一次只能搭乘一人,那麼每個人一但乘上電梯,就能快速達到目的樓層(單個IO響應時間),但其他人需要耗費較長的等待時間(總IO響應時間)
如果增加電梯容量(隊列深度)一次搭乘五個人,那麼低樓層的人能快速到達目的樓層(單個IO響應時間),高樓層的人需要耗費多一點點時間(總IO響應時間)
爲何要對磁盤I/O進行隊列化處理呢?主要目的是提升應用程序的性能。這一點對於多物理磁盤組成的虛擬磁盤(或LUN)顯得尤爲重要。
如果一次提交一個I/O,雖然響應時間較短,但系統的吞吐量很小。相比較而言,一次提交多個I/O既縮短了磁頭移動距離(通過電梯算法),同時也能夠提升IOPS。
因此一次向磁盤系統提交多個I/O能夠平衡吞吐量和整體響應時間。
在Centos7下下查看系統默認的隊列深度
lsscsi -l [1:0:0:0] cd/dvd NECVMWar VMware IDE CDR10 1.00 /dev/sr0 state=running queue_depth=1 scsi_level=6 type=5 device_blocked=0 timeout=30 [2:0:0:0] disk VMware Virtual disk 1.0 /dev/sda state=running queue_depth=32 scsi_level=3 type=0 device_blocked=0 timeout=180 [2:0:1:0] disk VMware Virtual disk 1.0 /dev/sdb state=running queue_depth=32 scsi_level=3 type=0 device_blocked=0 timeout=180
FIO 任務命令行模板,filename需要指定被測磁盤所在分區路徑
#read 順序讀 吞吐量 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=read -size=10G -nrfiles=1 -filename=fio_readputth_test.txt -name='fio read test' -iodepth=2 -runtime=120 -numjobs=4 -time_based=1 -allow_mounted_write=1 -group_reporting #write 順序寫 吞吐量 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=write -size=10G -nrfiles=1 -filename=fio_writeputth_test.txt -name='fio write test' -iodepth=2 -runtime=120 -numjobs=4 -time_based=1 -allow_mounted_write=1 -group_reporting #read 順序讀 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=read -size=2G -nrfiles=1 -filename=fio_read_test.txt -name='fio read test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting #write 順序寫 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=write -size=2G -nrfiles=1 -filename=fio_write_test.txt -name='fio write test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting #readwrite 順序混合讀寫 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=readwrite -size=2G -nrfiles=1 -filename=fio_readwrite_test.txt -name='fio readwrite test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting #randread 隨機讀 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=randread -size=2G -nrfiles=1 -filename=fio_randread_test.txt -name='fio randread test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting #randwrite 隨機寫 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=randwrite -size=2G -nrfiles=1 -filename=fio_randwrite_test.txt -name='fio randwrite test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting #randrw 隨機混合讀寫 fio -ioengine=libaio -direct=1 -bs=4k -thread -rw=randrw -size=2G -nrfiles=1 -filename=fio_randrw_test.txt -name='fio randrw test' -iodepth=4 -runtime=60 -numjobs=8 -time_based=1 -allow_mounted_write=1 -group_reporting
最後,我們可以把結果彙總到表格,方便把報告遞給領導
本文版權歸作者所有,未經作者同意不得轉載。