1、安装必要的工具

确保系统上已经安装了必要的工具,hdparmfio。使用以下命令来安装它们:

sudo yum install hdparm fio -y

2、使用hdparm测试磁盘读性能

hdparm是用于测试磁盘性能的简单工具。使用以下命令测试磁盘的读性能:

sudo hdparm -Tt /dev/vda1

/dev/vda1是我要测试的实际磁盘设备名称。该命令将显示磁盘的缓存和读性能。

3、使用fio测试磁盘写性能

fio是一个更强大的工具,可用于测试磁盘的写性能。创建一个fio配置文件,例如在/opt下创建文件disk_test.fio,并在其中定义写性能测试任务。以下是一个示例配置文件:

[write-test]
rw=write
filename=/opt/testfile
size=5G
  • rw=write 表示执行写入测试。
  • filename 指定要写入的文件的路径。
  • size 指定要写入的数据大小。

然后运行命令进行测试:

sudo fio disk_test.fio

4、使用dd命令测试磁盘写性能

运行以下dd命令,将数据写入到/opt/testfile文件中:

sudo dd if=/dev/zero of=/opt/testfile bs=1M count=5000 conv=fdatasync

解释一下这个命令:

  • if=/dev/zero 指定输入文件为/dev/zero,这是一个特殊的设备文件,它会提供无限的零字节数据,用于测试写入性能。
  • of=/opt/testfile 指定输出文件为您之前创建的/opt/testfile
  • bs=1M 设置块大小为1MB,这是每次写入的数据块大小。
  • count=5000 设置要写入的块数量,这里是5000个1MB块,总共写入5GB的数据。
  • conv=fdatasync 强制dd在每次写入后调用fdatasync以确保数据真正写入磁盘。

命令执行完后,dd将显示写入操作的性能统计信息,包括写入速度和用时。

可以使用ls -lh /opt/testfile命令来验证文件的大小是否为5GB,以确保测试成功。

请注意,这只是一个基本的磁盘写入性能测试示例。如果您需要更详细的性能数据或更复杂的测试场景,fio可能是更强大的工具,但dd可以用于快速的基本测试。在执行这些测试时,请谨慎操作以避免不必要的数据损失或对磁盘的不良影响。

5、执行fiodd测试结果如下:

root@ecs-8e73-0001.novalocal:/opt#sudo fio disk_test.fio
write-test: (g=0): rw=write, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=psync, iodepth=1
fio-3.7
Starting 1 process
write-test: Laying out IO file (1 file / 5120MiB)
Jobs: 1 (f=1): [W(1)][100.0%][r=0KiB/s,w=1386MiB/s][r=0,w=355k IOPS][eta 00m:00s]
write-test: (groupid=0, jobs=1): err= 0: pid=51004: Tue Sep  5 18:26:47 2023
  write: IOPS=358k, BW=1397MiB/s (1465MB/s)(5120MiB/3665msec)
    clat (nsec): min=1280, max=145687, avg=2038.07, stdev=923.48
     lat (nsec): min=1445, max=145856, avg=2207.73, stdev=944.24
    clat percentiles (nsec):
     |  1.00th=[ 1592],  5.00th=[ 1624], 10.00th=[ 1640], 20.00th=[ 1656],
     | 30.00th=[ 1688], 40.00th=[ 1720], 50.00th=[ 1768], 60.00th=[ 1880],
     | 70.00th=[ 2064], 80.00th=[ 2256], 90.00th=[ 2640], 95.00th=[ 3152],
     | 99.00th=[ 4576], 99.50th=[ 5920], 99.90th=[10304], 99.95th=[11072],
     | 99.99th=[14144]
   bw (  MiB/s): min= 1320, max= 1479, per=100.00%, avg=1400.88, stdev=59.90, samples=7
   iops        : min=337972, max=378664, avg=358625.86, stdev=15333.74, samples=7
  lat (usec)   : 2=66.61%, 4=31.42%, 10=1.83%, 20=0.13%, 50=0.01%
  lat (usec)   : 100=0.01%, 250=0.01%
  cpu          : usr=15.61%, sys=84.25%, ctx=59, majf=0, minf=32
  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 rwts: total=0,1310720,0,0 short=0,0,0,0 dropped=0,0,0,0
     latency   : target=0, window=0, percentile=100.00%, depth=1

Run status group 0 (all jobs):
  WRITE: bw=1397MiB/s (1465MB/s), 1397MiB/s-1397MiB/s (1465MB/s-1465MB/s), io=5120MiB (5369MB), run=3665-3665msec

Disk stats (read/write):
    dm-0: ios=0/7583, merge=0/0, ticks=0/330535, in_queue=338409, util=58.68%, aggrios=0/7741, aggrmerge=0/0, aggrticks=0/346394, aggrin_queue=346394, aggrutil=56.98%
  vda: ios=0/7741, merge=0/0, ticks=0/346394, in_queue=346394, util=56.98%
root@ecs-8e73-0001.novalocal:/opt#sudo dd if=/dev/zero of=/opt/testfile bs=1M count=5000 conv=fdatasync
5000+0 records in
5000+0 records out
5242880000 bytes (5.2 GB) copied, 2.97053 s, 1.8 GB/s
root@ecs-8e73-0001.novalocal:/opt#ls -lh /opt/testfile
-rw-r--r-- 1 root root 4.9G Sep  5 18:26 /opt/testfile
root@ecs-8e73-0001.novalocal:/opt#

fio测试解读

  • IOPS(每秒写入操作数):358,000
  • 带宽(吞吐量):1,397 MiB/s(约1.4 GB/s)
  • 用时(总时间)run=3665msec(约3.67秒)
  • 平均延迟(latency):约2微秒(平均),最大延迟约145,687纳秒

dd测试解读

  • 写入速度:1.8 GB/s
  • ls命令显示/opt/testfile的大小为4.9GB,这是用dd命令创建的那个5GB的文件。

**从结果来看,**这个磁盘的写性能非常不错,可以达到高达1.8 GB/s的写入速度,而且IOPS也很高,平均延迟非常低。这表明您的磁盘在处理大量写入操作时表现良好。(因为有SSD做缓存加速,因此这个时间是写入到SSD的时间,不是真正存储到磁盘的时间,数据先记录到SSD中,再由SSD慢慢写入到硬盘中)

性能测试的结果可能会受到多种因素的影响,包括硬件、磁盘类型、文件系统等。因此,性能测试的目的是了解磁盘的潜在性能,并帮助您确定是否需要进一步的优化或更改硬件配置。

6、4K随机读写性能测试:

上面是关于磁盘的随机写入性能测试。要执行随机读写测试(默认4K),你需要编辑disk_test.fio配置文件,将测试模式更改为随机读写(randrw)并重新运行测试。修改配置文件以执行随机读写测试的步骤如下:

  1. 打开
disk_test.fio

配置文件:

sudo vim /opt/disk_test.fio
  1. 修改配置文件的内容,将测试模式更改为随机读写:
[randrw-test]
rw=randrw
filename=/opt/testfile
size=5G
  • rw=randrw 表示执行随机读写测试。
  • 其余配置项保持不变,包括filename(测试文件路径)和size(测试文件大小)。
  1. 保存并退出编辑器。
  2. 现在,使用fio运行修改后的配置文件进行随机读写测试:
sudo fio /opt/disk_test.fio

这将执行随机读写测试并提供与随机读写性能相关的结果,包括随机读取和随机写入的IOPS、带宽和延迟等信息。

  1. 过程如下:
root@ecs-8e73-0001.novalocal:/opt#sudo fio /opt/disk_test.fio
write-test: (g=0): rw=randrw, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=psync, iodepth=1
fio-3.7
Starting 1 process
write-test: Laying out IO file (1 file / 5120MiB)
Jobs: 1 (f=1): [m(1)][13.6%][r=4388KiB/s,w=4308KiB/s][r=1097,w=1077 IOPS][eta 08m:46s]

过程解读,正在执行混合的随机读写测试,测试文件大小为5GB,已经完成了13.6%的任务。读取速度约为4388KiB/s,写入速度约为4308KiB/s。读取IOPS约为1097,写入IOPS约为1077。测试预计将在剩余的8分钟和46秒内完成。这些指标将帮助您了解磁盘的随机读写性能。

  1. 最后的结果如下:
root@ecs-8e73-0001.novalocal:/opt#sudo fio /opt/disk_test.fio
write-test: (g=0): rw=randrw, bs=(R) 4096B-4096B, (W) 4096B-4096B, (T) 4096B-4096B, ioengine=psync, iodepth=1
fio-3.7
Starting 1 process
write-test: Laying out IO file (1 file / 5120MiB)
Jobs: 1 (f=1): [m(1)][100.0%][r=4540KiB/s,w=4564KiB/s][r=1135,w=1141 IOPS][eta 00m:00s]
write-test: (groupid=0, jobs=1): err= 0: pid=58231: Tue Sep  5 18:51:37 2023
   read: IOPS=1037, BW=4149KiB/s (4249kB/s)(2561MiB/632135msec)
    clat (usec): min=254, max=338111, avg=957.69, stdev=3045.60
     lat (usec): min=254, max=338111, avg=957.88, stdev=3045.60
    clat percentiles (usec):
     |  1.00th=[   668],  5.00th=[   709], 10.00th=[   734], 20.00th=[   775],
     | 30.00th=[   799], 40.00th=[   824], 50.00th=[   857], 60.00th=[   881],
     | 70.00th=[   922], 80.00th=[   971], 90.00th=[  1074], 95.00th=[  1221],
     | 99.00th=[  2089], 99.50th=[  3097], 99.90th=[  7767], 99.95th=[ 11076],
     | 99.99th=[206570]
   bw (  KiB/s): min=  920, max= 4952, per=100.00%, avg=4148.16, stdev=599.48, samples=1264
   iops        : min=  230, max= 1238, avg=1037.00, stdev=149.87, samples=1264
  write: IOPS=1036, BW=4145KiB/s (4244kB/s)(2559MiB/632135msec)
    clat (nsec): min=1367, max=116379, avg=3415.12, stdev=1474.56
     lat (nsec): min=1535, max=116591, avg=3623.44, stdev=1519.50
    clat percentiles (nsec):
     |  1.00th=[ 1896],  5.00th=[ 2064], 10.00th=[ 2160], 20.00th=[ 2352],
     | 30.00th=[ 2576], 40.00th=[ 2832], 50.00th=[ 3120], 60.00th=[ 3440],
     | 70.00th=[ 3760], 80.00th=[ 4128], 90.00th=[ 4832], 95.00th=[ 5664],
     | 99.00th=[ 9152], 99.50th=[11072], 99.90th=[14144], 99.95th=[15808],
     | 99.99th=[21376]
   bw (  KiB/s): min=  992, max= 5488, per=100.00%, avg=4144.40, stdev=660.02, samples=1264
   iops        : min=  248, max= 1372, avg=1036.06, stdev=165.00, samples=1264
  lat (usec)   : 2=1.55%, 4=36.68%, 10=11.36%, 20=0.38%, 50=0.01%
  lat (usec)   : 100=0.01%, 250=0.01%, 500=0.01%, 750=6.70%, 1000=35.26%
  lat (msec)   : 2=7.51%, 4=0.39%, 10=0.13%, 20=0.02%, 50=0.01%
  lat (msec)   : 100=0.01%, 250=0.01%, 500=0.01%
  cpu          : usr=0.24%, sys=1.74%, ctx=656045, majf=0, minf=36
  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 rwts: total=655676,655044,0,0 short=0,0,0,0 dropped=0,0,0,0
     latency   : target=0, window=0, percentile=100.00%, depth=1

Run status group 0 (all jobs):
   READ: bw=4149KiB/s (4249kB/s), 4149KiB/s-4149KiB/s (4249kB/s-4249kB/s), io=2561MiB (2686MB), run=632135-632135msec
  WRITE: bw=4145KiB/s (4244kB/s), 4145KiB/s-4145KiB/s (4244kB/s-4244kB/s), io=2559MiB (2683MB), run=632135-632135msec

Disk stats (read/write):
    dm-0: ios=655401/592963, merge=0/0, ticks=622124/2297686, in_queue=2920059, util=98.50%, aggrios=655983/593644, aggrmerge=11/160, aggrticks=622303/2290813, aggrin_queue=2913116, aggrutil=99.86%
  vda: ios=655983/593644, merge=11/160, ticks=622303/2290813, in_queue=2913116, util=99.86%
root@ecs-8e73-0001.novalocal:/opt#

这份fio测试结果总结如下:

  1. 测试配置
  • 测试类型:随机读写(randrw)
  • 块大小:4096字节
  • I/O引擎:psync
  • I/O深度:1
  1. 任务进度
  • 单个测试任务在一个混合读写(randrw)场景下执行。
  • 测试任务已完成,进度为100%。
  • 在测试结果中,读取任务的运行时间(run)为632135毫秒。这相当于632.135秒或约0.17小时(大约10分钟)。
  1. 读取任务性能
  • 平均读取速度:4149KiB/s(约4.05MB/s)
  • 平均读取IOPS:1037
  • 延迟范围:最小254微秒,最大338111微秒(约0.3毫秒至338毫秒)
  • 读取任务的延迟百分位数:99%的读取操作在7767微秒(约7.8毫秒)以下完成。
  1. 写入任务性能
  • 平均写入速度:4145KiB/s(约4.05MB/s)
  • 平均写入IOPS:1036
  • 延迟范围:最小1367纳秒,最大116379纳秒(约1.4微秒至116毫秒)
  • 写入任务的延迟百分位数:99%的写入操作在14144纳秒(约0.01毫秒)以下完成。
  1. CPU利用率
  • 用户态CPU利用率:0.24%
  • 内核态CPU利用率:1.74%
  1. I/O深度
  • 100%的I/O操作深度为1。
  1. 磁盘统计
  • 磁盘dm-0vda的统计信息,包括I/O操作数量、合并操作、CPU时间和磁盘利用率等。

上面是这台主机有关磁盘4K随机读写性能的详细信息,包括读取和写入速度、延迟、分布情况以及CPU利用率。(这次是因为读取无法被SSD精准加速,因此读取的性能可以看做是直接读硬盘的性能)这样做可以评估系统的磁盘性能和潜在瓶颈,并根据需要进行性能优化或调整配置。

如果你想测试10线程可以这样配置

[random-rw-test]
rw=randrw
bs=4k
iodepth=10
numjobs=10
size=5G
filename=/opt/testfile

使用了10个进程(jobs=10)并且每个进程的IO深度(iodepth)设置为10。这意味着您在同时运行10个IO操作,并且每个进程将在磁盘上维护10个IO请求的深度。这是一个典型的并发测试场景,用于模拟多个IO操作同时进行的情况。

请注意,具体的性能指标可能会因硬件、文件系统和测试负载而异