在 Linux VM 上基準化永久磁碟效能


本文說明如何在 Linux 虛擬機器 (VM) 上基準測試 Persistent Disk 效能。如為 Windows VM,請參閱「在 Windows VM 上基準測試永久磁碟效能」。

如要基準化 Linux 上的永久磁碟效能,請使用彈性 I/O 測試工具 (FIO),而非其他磁碟基準測試工具,例如 dd。根據預設,dd 會使用非常低的 I/O 佇列深度,因此可能無法準確測試磁碟效能。一般來說,請勿在永久磁碟效能基準測試中使用 /dev/urandom/dev/random/dev/zero 等特殊裝置。

如要評估在執行中執行個體上使用的磁碟的 IOPS 和處理量,請使用檔案系統的預期設定進行基準測試。使用這個選項可測試實際的工作負載,且不會遺失現有磁碟的內容。請注意,當您在現有磁碟上對檔案系統進行基準測試時,開發環境中可能有許多因素會影響基準測試結果,因此您可能無法達到磁碟效能限制

如要測量永久磁碟的原始效能,請直接基準化區塊裝置。使用這個選項,將原始磁碟效能與磁碟效能限制進行比較。

下列指令適用於搭配 apt 套件管理工具的 Debian 或 Ubuntu 作業系統。

在執行中的執行個體上基準測試磁碟的 IOPS 和總處理量

如果您想在執行中的執行個體上,針對實際工作負載的活動磁碟測量 IOPS 和總處理量,且不想因此遺失磁碟內容,請根據現有檔案系統的新目錄進行基準測試。每項 fio 測試都會執行五分鐘。

  1. 連線至執行個體

  2. 安裝依附元件:

    sudo apt update
    sudo apt install -y fio
    
  3. 在終端機中,列出連接至 VM 的磁碟,並找出要測試的磁碟。如果永久磁碟尚未格式化,請格式化並掛接磁碟

    sudo lsblk
    
    NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda      8:0    0   10G  0 disk
    └─sda1   8:1    0   10G  0 part /
    sdb      8:32   0  2.5T  0 disk /mnt/disks/mnt_dir
    

    在本範例中,我們測試裝置 ID 為 sdb 的 2,500 GB SSD 永久磁碟。

  4. 在磁碟上建立新目錄 fiotest。在這個範例中,磁碟會掛接在 /mnt/disks/mnt_dir 中:

    TEST_DIR=/mnt/disks/mnt_dir/fiotest
    sudo mkdir -p $TEST_DIR
    
  5. 測試寫入總處理量。搭配多個平行串流 (16 個以上) 使用循序寫入,且 I/O 區塊大小為 1 MB,I/O 深度則為至少 64:

    sudo fio --name=write_throughput --directory=$TEST_DIR --numjobs=16 \
    --size=10G --time_based --runtime=5m --ramp_time=2s --ioengine=libaio \
    --direct=1 --verify=0 --bs=1M --iodepth=64 --rw=write \
    --group_reporting=1 --iodepth_batch_submit=64 \
    --iodepth_batch_complete_max=64
    
  6. 使用 4 KB 的 I/O 區塊大小和至少 256 的 I/O 深度,執行隨機寫入作業,測試寫入 IOPS:

     sudo fio --name=write_iops --directory=$TEST_DIR --size=10G \
    --time_based --runtime=5m --ramp_time=2s --ioengine=libaio --direct=1 \
    --verify=0 --bs=4K --iodepth=256 --rw=randwrite --group_reporting=1  \
    --iodepth_batch_submit=256  --iodepth_batch_complete_max=256
    
  7. 測試讀取總處理量。搭配多個平行串流 (16 個以上) 使用循序讀取,且 I/O 區塊大小為 1 MB,I/O 深度則為至少 64:

    sudo fio --name=read_throughput --directory=$TEST_DIR --numjobs=16 \
    --size=10G --time_based --runtime=5m --ramp_time=2s --ioengine=libaio \
    --direct=1 --verify=0 --bs=1M --iodepth=64 --rw=read \
    --group_reporting=1 \
    --iodepth_batch_submit=64 --iodepth_batch_complete_max=64
    
  8. 使用 4 KB 的 I/O 區塊大小和至少 256 的 I/O 深度,執行隨機讀取作業,以測試讀取 IOPS:

    sudo fio --name=read_iops --directory=$TEST_DIR --size=10G \
    --time_based --runtime=5m --ramp_time=2s --ioengine=libaio --direct=1 \
    --verify=0 --bs=4K --iodepth=256 --rw=randread --group_reporting=1 \
    --iodepth_batch_submit=256  --iodepth_batch_complete_max=256
    
  9. 清理:

    sudo rm $TEST_DIR/write* $TEST_DIR/read*
    

基準化原始永久磁碟效能

如果您想在開發環境以外測量永久磁碟的效能,請在一次性永久磁碟和 VM 上測試區塊裝置的讀取和寫入效能。每項 fio 測試都會執行五分鐘。下列指令假設您使用裝置大小為 2,500 GB 的 SSD 永久磁碟。如果您的裝置大小不同,請修改 --filesize 引數。必須使用此磁碟大小才能達到 32 vCPU VM 總處理量上限。詳情請參閱「區塊儲存空間效能」。

  1. 建立並啟動 VM 執行個體

  2. 將永久磁碟新增至您要進行基準測試的 VM 執行個體

  3. 連線至執行個體

  4. 安裝依附元件:

    sudo apt-get update
    sudo apt-get install -y fio
    
  5. 為磁碟填充非零資料。空白區塊與含資料區塊的永久磁碟讀取作業有不同的延遲狀況。 建議您執行任何讀取延遲基準前先填充磁碟。

    # Running this command causes data loss on the second device.
    # We strongly recommend using a throwaway VM and disk.
    sudo fio --name=fill_disk \
      --filename=/dev/sdb --filesize=2500G \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=128K --iodepth=64 --rw=randwrite \
      --iodepth_batch_submit=64 --iodepth_batch_complete_max=64
    
  6. 測試寫入頻寬。搭配多個平行串流 (16 個以上) 使用循序寫入,且 I/O 大小為 1MB,I/O 深度則為 64 以上。

    # Running this command causes data loss on the second device.
    # We strongly recommend using a throwaway VM and disk.
    sudo fio --name=write_bandwidth_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=1M --iodepth=64 --iodepth_batch_submit=64 --iodepth_batch_complete_max=64 \
      --rw=write --numjobs=16 --offset_increment=100G
    
  7. 測試寫入 IOPS。如要達到 PD 磁碟 IOPS 上限,請務必維持夠深的 I/O 佇列。舉例來說,如果寫入延遲為 1 毫秒,則針對每項傳輸中的 I/O,VM 最高可達到 1,000 IOPS。 如要達到 15,000 IOPS,VM 必須維持至少 15 個傳輸中的 I/O。如果您的磁碟和 VM 能夠達到 30,000 IOPS,則至少要有 30 個傳輸中的 I/O。如果 I/O 大小超過 4 KB,VM 可能會在達到 IOPS 上限前先達到頻寬上限。

    # Running this command causes data loss on the second device.
    # We strongly recommend using a throwaway VM and disk.
    sudo fio --name=write_iops_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=4K --iodepth=256 --rw=randwrite \
      --iodepth_batch_submit=256 --iodepth_batch_complete_max=256
    
  8. 測試寫入延遲。測試 I/O 延遲時,請確保 VM 未達到頻寬或 IOPS 上限,否則觀察到的延遲無法反映出實際的永久磁碟 I/O 延遲狀況。舉例來說,如果 I/O 深度為 30 時會達到 IOPS 上限,且 fio 指令會產生兩倍的值,則總 IOPS 會維持不變,回報的 I/O 延遲時間則會加倍。

    # Running this command causes data loss on the second device.
    # We strongly recommend using a throwaway VM and disk.
    sudo fio --name=write_latency_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=4K --iodepth=4 --rw=randwrite --iodepth_batch_submit=4 \
      --iodepth_batch_complete_max=4
    
  9. 測試讀取頻寬。搭配多個平行串流 (16 個以上) 使用循序讀取,且 I/O 大小為 1MB,I/O 深度則為 64 以上。

    sudo fio --name=read_bandwidth_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=1M --iodepth=64 --rw=read --numjobs=16 --offset_increment=100G \
      --iodepth_batch_submit=64 --iodepth_batch_complete_max=64
    
  10. 測試讀取 IOPS。如要達到 PD 磁碟 IOPS 上限,請務必維持夠深的 I/O 佇列。如果 I/O 大小超過 4 KB,VM 可能會在達到 IOPS 上限前先達到頻寬上限。如要達到最大 10 萬次讀取 IOPS,請為這項測試指定 --iodepth=256

    sudo fio --name=read_iops_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=4K --iodepth=256 --rw=randread \
      --iodepth_batch_submit=256 --iodepth_batch_complete_max=256
    
  11. 測試讀取延遲。請務必為磁碟填充資料,這樣才能測出真實的延遲時間。此外,請確保 VM 在測試期間未達到 IOPS 或總處理量上限,因為永久磁碟達到飽和上限後,就會拒絕連入 I/O,導致 I/O 延遲出現人為增加的情況。

    sudo fio --name=read_latency_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --bs=4K --iodepth=4 --rw=randread \
      --iodepth_batch_submit=4 --iodepth_batch_complete_max=4
    
  12. 測試循序讀取頻寬。

    sudo fio --name=read_bandwidth_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --numjobs=4 --thread --offset_increment=500G \
      --bs=1M --iodepth=64 --rw=read \
      --iodepth_batch_submit=64  --iodepth_batch_complete_max=64
    
  13. 測試循序寫入頻寬。

    sudo fio --name=write_bandwidth_test \
      --filename=/dev/sdb --filesize=2500G \
      --time_based --ramp_time=2s --runtime=5m \
      --ioengine=libaio --direct=1 --verify=0 --randrepeat=0 \
      --numjobs=4 --thread --offset_increment=500G \
      --bs=1M --iodepth=64 --rw=write \
      --iodepth_batch_submit=64  --iodepth_batch_complete_max=64
    
  14. 清除一次性永久磁碟和 VM:

    1. 刪除用於效能基準測試的磁碟
    2. 刪除用於效能基準測試而建立的 VM。

後續步驟