如何在 Linux 中使用 Fio 測量磁盤性能

Fio 是一個免費的開源工具,可用於基準測試和硬件驗證。 它支持許多 I/O 引擎,包括 libaio、sync、nmap、syslet、network、slice 等等。 它還支持許多發行版,例如 Linux、OpenBSD、OpenSolaris、HP-UX、Android 和 Windows。它可以在文件和塊設備上工作。

在本教程中,我們將學習如何在 Linux 中安裝 Fio,我們還將通過一些示例學習如何使用 Fio。

要求

  • 在您的系統上安裝 Ubuntu 16.04 桌面版或更高版本。
  • 一個普通用戶 sudo 特權。

安裝菲奧

在這裡,我們將在所有主要 Linux 發行版中安裝 Fio,例如 CentOS 7、Ubuntu 16.04 和 Debian 9。

在 Ubuntu 16.04 上安裝 Fio

默認情況下,Fio 在 Ubuntu 默認存儲庫中可用,因此您可以通過運行以下命令輕鬆安裝它:

sudo apt-get install fio -y

在 CentOS 7 上安裝 Fio

默認情況下,Fio 在 CentOS 默認存儲庫中不可用。 因此,您需要將 EPEL 存儲庫安裝到您的系統中。 您可以通過運行以下命令來安裝它:

sudo yum install epel-release -y

安裝 EPEL 存儲庫後,您可以通過運行以下命令來安裝 Fio:

sudo yum install fio -y

在 Debian 9 上安裝 Fio

默認情況下,Fio 在 Debian 存儲庫中可用,您只需運行以下命令即可安裝它:

sudo apt-get install fio -y

與菲奧合作

現在,Fio 已安裝在您的系統上。 是時候看看如何使用 Fio 了 example.

示例 1:隨機寫入測試

讓我們從運行以下命令開始。 此命令將寫入總共 2GB 的文件 [4 jobs x 512 MB = 2GB] 一次運行4個進程:

sudo fio --name=randwrite --ioengine=libaio --iodepth=1 --rw=randwrite --bs=4k --direct=0 --size=512M --numjobs=4 --runtime=240 --group_reporting

您應該看到以下輸出:

randwrite: (g=0): rw=randwrite, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=1
fio-2.1.3
Starting 4 processes
randwrite: Laying out IO file(s) (1 file(s) / 512MB)
randwrite: Laying out IO file(s) (1 file(s) / 512MB)
randwrite: Laying out IO file(s) (1 file(s) / 512MB)
randwrite: Laying out IO file(s) (1 file(s) / 512MB)
Jobs: 1 (f=1): [__w_] [-.-% done] [0KB/27032KB/0KB /s] [0/6758/0 iops] [eta 00m:00s] 
randwrite: (groupid=0, jobs=4): err= 0: pid=28810: Tue Jul 25 20:28:20 2017
  write: io=2048.0MB, bw=297722KB/s, iops=74430, runt=  7044msec
    slat (usec): min=9, max=47469, avg=38.13, stdev=488.37
    clat (usec): min=1, max=10595, avg= 2.26, stdev=30.99
     lat (usec): min=11, max=47478, avg=40.74, stdev=489.66
    clat percentiles (usec):
     |  1.00th=[    1],  5.00th=[    2], 10.00th=[    2], 20.00th=[    2],
     | 30.00th=[    2], 40.00th=[    2], 50.00th=[    2], 60.00th=[    2],
     | 70.00th=[    2], 80.00th=[    2], 90.00th=[    2], 95.00th=[    2],
     | 99.00th=[    3], 99.50th=[    4], 99.90th=[   16], 99.95th=[   61],
     | 99.99th=[  454]
    bw (KB  /s): min=10626, max=244432, per=32.42%, avg=96530.07, stdev=85594.86
    lat (usec) : 2=4.28%, 4=95.15%, 10=0.44%, 20=0.03%, 50=0.03%
    lat (usec) : 100=0.04%, 250=0.01%, 500=0.01%, 750=0.01%, 1000=0.01%
    lat (msec) : 2=0.01%, 4=0.01%, 10=0.01%, 20=0.01%
  cpu          : usr=11.91%, sys=27.58%, ctx=4646, majf=0, minf=104
  IO depths    : 1=100.0%, 2=0.0%, 4=0.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    : total=r=0/w=524288/d=0, short=r=0/w=0/d=0

Run status group 0 (all jobs):
  WRITE: io=2048.0MB, aggrb=297721KB/s, minb=297721KB/s, maxb=297721KB/s, mint=7044msec, maxt=7044msec

Disk stats (read/write):
  sda: ios=129/2495, merge=0/1599, ticks=11520/91476, in_queue=143340, util=78.35%

示例 2:隨機讀取測試

以下命令將讀取總共 2GB 的文件,同時運行 4 個進程。

sudo fio --name=randread --ioengine=libaio --iodepth=16 --rw=randread --bs=4k --direct=0 --size=512M --numjobs=4 --runtime=240 --group_reporting

您應該會看到以下結果:

randread: (g=0): rw=randread, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=16
...
randread: (g=0): rw=randread, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=16
fio-2.1.3
Starting 4 processes
randread: Laying out IO file(s) (1 file(s) / 512MB)
randread: Laying out IO file(s) (1 file(s) / 512MB)
randread: Laying out IO file(s) (1 file(s) / 512MB)
randread: Laying out IO file(s) (1 file(s) / 512MB)
Jobs: 4 (f=4): [rrrr] [100.0% done] [467KB/0KB/0KB /s] [116/0/0 iops] [eta 00m:00s]
randread: (groupid=0, jobs=4): err= 0: pid=14521: Tue Jul 25 22:01:24 2017
  read : io=117912KB, bw=503032B/s, iops=122, runt=240028msec
    slat (usec): min=93, max=1178.5K, avg=32554.59, stdev=45398.62
    clat (usec): min=12, max=2611.2K, avg=487669.64, stdev=288615.07
     lat (msec): min=49, max=2667, avg=520.23, stdev=303.69
    clat percentiles (msec):
     |  1.00th=[  167],  5.00th=[  217], 10.00th=[  247], 20.00th=[  289],
     | 30.00th=[  330], 40.00th=[  367], 50.00th=[  404], 60.00th=[  445],
     | 70.00th=[  502], 80.00th=[  603], 90.00th=[  898], 95.00th=[ 1057],
     | 99.00th=[ 1614], 99.50th=[ 1811], 99.90th=[ 2245], 99.95th=[ 2376],
     | 99.99th=[ 2573]
    bw (KB  /s): min=    4, max=  318, per=25.59%, avg=125.65, stdev=59.18
    lat (usec) : 20=0.01%
    lat (msec) : 50=0.01%, 100=0.02%, 250=10.45%, 500=59.10%, 750=15.73%
    lat (msec) : 1000=8.21%, 2000=6.23%, >=2000=0.25%
  cpu          : usr=0.05%, sys=0.17%, ctx=29816, majf=0, minf=165
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=99.8%, 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.1%, 32=0.0%, 64=0.0%, >=64=0.0%
     issued    : total=r=29478/w=0/d=0, short=r=0/w=0/d=0

Run status group 0 (all jobs):
   READ: io=117912KB, aggrb=491KB/s, minb=491KB/s, maxb=491KB/s, mint=240028msec, maxt=240028msec

Disk stats (read/write):
  sda: ios=29552/2717, merge=0/2735, ticks=969368/97672, in_queue=1067872, util=100.00%

示例 3:讀寫性能測試

以下命令將測量 U 盤 (/dev/sdc1) 的隨機讀寫性能:

sudo fio --randrepeat=1 --ioengine=libaio --direct=1 --gtod_reduce=1 --name=test --filename=random_read_write.fio --bs=4k --iodepth=64 --size=4G --readwrite=randrw --rwmixread=75

您應該會看到以下測試結果:

test: (g=0): rw=randrw, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=64
fio-2.1.3
Starting 1 process
Jobs: 1 (f=1): [m] [98.0% done] [191KB/95KB/0KB /s] [47/23/0 iops] [eta 00m:03s]
test: (groupid=0, jobs=1): err= 0: pid=24800: Tue Jul 25 22:10:38 2017
  read : io=15512KB, bw=107237B/s, iops=26, runt=148123msec
  write: io=4968.0KB, bw=34344B/s, iops=8, runt=148123msec
  cpu          : usr=0.08%, sys=0.18%, ctx=5080, majf=0, minf=24
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.2%, 16=0.3%, 32=0.6%, >=64=98.8%
     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.1%, >=64=0.0%
     issued    : total=r=3878/w=1242/d=0, short=r=0/w=0/d=0

Run status group 0 (all jobs):
   READ: io=15512KB, aggrb=104KB/s, minb=104KB/s, maxb=104KB/s, mint=148123msec, maxt=148123msec
  WRITE: io=4968KB, aggrb=33KB/s, minb=33KB/s, maxb=33KB/s, mint=148123msec, maxt=148123msec

Disk stats (read/write):
  sdc: ios=3506/1203, merge=372/28, ticks=7386380/1400600, in_queue=8796700, util=100.00%

上述命令將在 USB 筆式驅動器中寫入 20MB 數據,並使用每個寫入比率(rwmixread=75)的三個讀取執行 4KB 讀取和寫入。

Fio 是一個非常有用的工具,可以提供有關磁盤速度和延遲狀態的信息。 您現在可以使用 Fio 輕鬆測試磁盤的性能。