FIO_磁盘性能测试_命令详解
前提条件
安装FIO
bash
# CentOS/RHEL
yum install fio -y
# Ubuntu/Debian
apt-get install fio -y基础测试命令示例
1. 4K 随机读取测试(高并发)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=randread -ioengine=libaio -bs=4k -size=200G -numjobs=32 -runtime=60 -group_reporting -name=4k_randread_j32_d322. 4K 随机写入测试(高线程)
bash
fio -filename=/dev/sdc -direct=1 -iodepth 1 -thread -rw=randwrite -ioengine=libaio -bs=4k -size=100G -numjobs=64 -runtime=180 -group_reporting -name=4k_randwrite_j64_d13. 4K 随机写入测试(单线程高队列)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=randwrite -ioengine=libaio -bs=4k -size=200G -numjobs=1 -runtime=60 -group_reporting -name=4k_randwrite_j1_d324. 1M 顺序写入测试(吞吐量)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=write -ioengine=libaio -bs=1M -size=200G -numjobs=1 -runtime=60 -group_reporting -name=1M_write_j1_d32参数详解
| 参数 | 含义 | 说明 |
|---|---|---|
-filename=/dev/sdb | 测试目标 | 指定测试的块设备或文件路径 |
-direct=1 | Direct I/O | 绕过操作系统页面缓存,直接测试磁盘真实性能 |
-iodepth N | I/O 深度 | 每个job同时向设备队列发送的I/O请求数 |
-thread | 线程模式 | 使用线程模式运行(numjobs>1时为每个job创建线程) |
-rw=MODE | 读写模式 | randread/randwrite/read/write/randrw(随机读/写,顺序读/写,混合读写) |
-ioengine=libaio | I/O引擎 | 使用Linux异步I/O引擎 |
-bs=SIZE | 块大小 | I/O操作的块大小,如4k、1M |
-size=SIZE | 测试大小 | 测试的总文件或设备区域大小 |
-numjobs=N | 并发任务数 | 启动的并发job(线程)数量 |
-runtime=SEC | 运行时间 | 测试持续时间(秒) |
-group_reporting | 汇总报告 | 将所有job结果汇总输出 |
-name=NAME | 任务名称 | 为测试任务指定名称,建议格式:{bs}_{mode}_j{jobs}_d |
-rwmixread=N | 读取比例 | 混合读写模式下的读取操作百分比(0-100) |
测试场景分类
1. 4K 随机性能测试(IOPS核心指标)
这类测试模拟数据库、虚拟机、大量小文件读写等场景,核心性能指标是IOPS(每秒读写次数)。
高并发随机读取
- 配置: 4k块大小,32线程,32队列深度,随机读取
- 并发数: 32 × 32 = 1024个并发请求
- 目的: 压榨硬盘在最高并发压力下的最大随机读取IOPS
- 适用场景: 高并发数据库、虚拟化环境
高线程随机写入
- 配置: 4k块大小,64线程,1队列深度,随机写入
- 并发数: 64 × 1 = 64个并发请求
- 目的: 测试硬盘处理大量独立并发写入流的能力
- 适用场景: 多用户文件服务器、日志系统
单线程高队列随机写入
- 配置: 4k块大小,1线程,32队列深度,随机写入
- 并发数: 1 × 32 = 32个并发请求
- 目的: 测试单应用高负载下的写入响应能力
- 适用场景: 单实例数据库、应用服务器
2. 1M 顺序性能测试(吞吐量核心指标)
这类测试模拟大文件拷贝、视频流读写、备份等场景,核心性能指标是吞吐量/带宽(MB/s)。
顺序写入测试
- 配置: 1M块大小,1线程,32队列深度,顺序写入
- 目的: 测试硬盘最大顺序写入速度和带宽上限
- 适用场景: 大文件传输、视频编辑、数据备份
测试建议
测试前准备
⚠️ 数据备份:确保测试设备没有重要数据或已进行备份
- 服务停止:停止可能影响测试的相关服务,避免干扰
- 多次测试:进行多次测试取平均值,提高结果准确性
- SSD预热(重要):SSD测试前进行一次完整的顺序写入预热,消除新盘的"开箱性能加成",测出真实稳定态性能
bash
# SSD预热命令
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=write -ioengine=libaio -bs=1M -size=100% -numjobs=1 -group_reporting -name=ssd_warmup结果分析重点
- 4K随机测试:关注IOPS、延迟(latency)
- 1M顺序测试:关注带宽(BW)、吞吐量
- 混合测试:关注读写比例下的综合性能
常见测试组合
数据库场景(70%读30%写)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=randrw -rwmixread=70 -ioengine=libaio -bs=4k -size=200G -numjobs=16 -runtime=300 -group_reporting -name=4k_randrw70_j16_d32文件服务器场景(50%读50%写)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 16 -thread -rw=randrw -rwmixread=50 -ioengine=libaio -bs=4k -size=200G -numjobs=32 -runtime=300 -group_reporting -name=4k_randrw50_j32_d16备份场景(100%顺序写入)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 32 -thread -rw=write -ioengine=libaio -bs=1M -size=500G -numjobs=4 -runtime=600 -group_reporting -name=1M_write_j4_d32视频编辑场景(80%读20%写)
bash
fio -filename=/dev/sdb -direct=1 -iodepth 8 -thread -rw=randrw -rwmixread=80 -ioengine=libaio -bs=1M -size=200G -numjobs=2 -runtime=300 -group_reporting -name=1M_randrw80_j2_d8混合读写参数说明
-rw=randrw:随机读写混合模式-rwmixread=70:读取操作占比70%,写入30%
测试结果解读
FIO测试结果包含以下关键指标:
| 指标 | 说明 | 单位 |
|---|---|---|
| IOPS | 每秒输入输出操作次数 | 次/秒 |
| BW | 带宽,数据传输速率 | KB/s, MB/s |
| Latency | 延迟,单个操作所需时间 | μs, ms |
| CPU | CPU使用率 | % |
| I/O Depth | 实际I/O队列深度 | 数值 |
示例结果解读
4k_randread_j32_d32: (groupid=0, jobs=32): err= 0: pid=12345: Wed Jun 15 10:30:45 2023
read : IOPS=98.5k, BW=383MiB/s (402MB/s)(22.1GiB/60001msec)
slat (nsec): min=560, max=85640, avg=1250.32, stdev=852.37
clat (usec): min=45, max=8500, avg=320.51, stdev=215.84
lat (usec): min=46, max=8501, avg=321.76, stdev=215.85
clat percentiles (usec):
| 1.00th=[ 70], 5.00th=[ 92], 10.00th=[ 110], 20.00th=[ 135],
| 30.00th=[ 160], 40.00th=[ 185], 50.00th=[ 215], 60.00th=[ 250],
| 70.00th=[ 300], 80.00th=[ 365], 90.00th=[ 480], 95.00th=[ 620],
| 99.00th=[ 1000], 99.50th=[ 1300], 99.90th=[ 2000], 99.95th=[ 2500],
| 99.99th=[ 4000]
cpu : usr=1.23%, sys=4.56%, ctx=3158528, majf=0, minf=75
IO depths : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=0.1%, 32=100.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=6370880,0,0,0 short=0,0,0,0 dropped=0,0,0,0
latency : target=0, window=0, percentile=100.00%, depth=32结果解读:
IOPS=98.5k:每秒完成98,500次读取操作BW=383MiB/s:带宽为383MiB每秒lat (usec): avg=321.76:平均延迟321.76微秒IO depths: 32=100.0%:所有I/O操作都在深度32完成
注意事项
⚠️ 安全提醒:
- 测试会导致数据覆盖,请确保无重要数据或已备份
- 长时间高负载测试可能影响磁盘寿命,特别是SSD
- 生产环境测试应避开业务高峰期
⚠️ 性能调优:
- 不同存储介质(HDD/SSD/NVMe)的最佳测试参数不同
- SSD建议使用较大iodepth(如32-128)
- HDD建议使用较小iodepth(如1-8)
- 测试结果受系统内核版本、文件系统等因素影响
扩展应用
RAID测试
bash
# RAID5性能测试
fio -filename=/dev/md0 -direct=1 -iodepth 16 -thread -rw=randrw -ioengine=libaio -bs=64k -size=500G -numjobs=8 -runtime=600 -group_reporting -name=raid5_test网络存储测试
bash
# NFS性能测试
fio -filename=/mnt/nfs/testfile -direct=1 -iodepth 8 -thread -rw=randread -ioengine=libaio -bs=4k -size=100G -numjobs=4 -runtime=300 -group_reporting -name=nfs_randread