> 技术文档 > Linux测试磁盘的连续读写性能以及4K随机读写性能的工具使用教程:dd命令、fio工具、hdparm工具、ioping工具_linux硬盘读写测试工具

Linux测试磁盘的连续读写性能以及4K随机读写性能的工具使用教程:dd命令、fio工具、hdparm工具、ioping工具_linux硬盘读写测试工具

在Linux系统中,有多种工具可以用来测试磁盘的连续读写性能以及4K随机读写性能。以下是一些常用的工具和方法:

1. dd 命令

dd 是一个非常基础且强大的命令行工具,可以用来复制文件并同时进行性能测试。

连续读写测试
  • 写入测试

    dd if=/dev/zero of=testfile bs=1M count=1024 oflag=direct

    这个命令会从 /dev/zero 读取数据,写入到 testfile 中,块大小为 1MB,总共写入 1024 个块(即 1GB)。

  • 读取测试

    dd if=testfile of=/dev/null bs=1M count=1024 iflag=direct

    这个命令会从 testfile 读取数据,并丢弃到 /dev/null,块大小为 1MB,总共读取 1024 个块(即 1GB)。

4K 随机读写测试
  • 写入测试

    dd if=/dev/zero of=testfile bs=4k count=262144 oflag=direct

    这个命令会从 /dev/zero 读取数据,写入到 testfile 中,块大小为 4KB,总共写入 262144 个块(即 1GB)。

  • 读取测试

    dd if=testfile of=/dev/null bs=4k count=262144 iflag=direct

    这个命令会从 testfile 读取数据,并丢弃到 /dev/null,块大小为 4KB,总共读取 262144 个块(即 1GB)。

2. fio 工具

fio 是一个更高级的 I/O 测试工具,支持多种测试场景,包括连续读写和随机读写。

安装 fio

如果你还没有安装 fio,可以通过包管理器来安装它:

sudo apt-get install fio # Debian/Ubuntusudo yum install fio # CentOS/RHEL
连续读写测试
  • 写入测试

    fio --name=seqwrite --ioengine=sync --rw=write --bs=1m --size=1G --numjobs=1 --direct=1 --group_reporting
  • 读取测试

    fio --name=seqread --ioengine=sync --rw=read --bs=1m --size=1G --numjobs=1 --direct=1 --group_reporting
4K 随机读写测试
  • 写入测试

    fio --name=randwrite --ioengine=sync --rw=randwrite --bs=4k --size=1G --numjobs=1 --direct=1 --group_reporting
  • 读取测试

    fio --name=randread --ioengine=sync --rw=randread --bs=4k --size=1G --numjobs=1 --direct=1 --group_reporting

3. hdparm 工具

hdparm 主要用于获取硬盘参数和测试硬盘的读取速度。

安装 hdparm
sudo apt-get install hdparm # Debian/Ubuntusudo yum install hdparm # CentOS/RHEL
读取测试
  • 连续读取测试

    sudo hdparm -Tt /dev/sda

    其中 -T 选项测试缓存读取速度,-t 选项测试实际读取速度。

4. ioping 工具

ioping 是一个简单的工具,可以用来测试磁盘的延迟和吞吐量。

安装 ioping
sudo apt-get install ioping # Debian/Ubuntusudo yum install ioping # CentOS/RHEL
测试
  • 连续读写测试

    sudo ioping -c 100 -b 1M /path/to/testfile
  • 4K 随机读写测试

    sudo ioping -c 100 -b 4K /path/to/testfile

总结

  • dd:简单易用,适合快速测试。
  • fio:功能强大,支持多种测试场景,推荐使用。
  • hdparm:主要用于硬盘参数和读取速度测试。
  • ioping:简单工具,适合快速测试延迟和吞吐量。