在开发过程中,我们有时候需要模拟磁盘慢的情况, 以验证代码在在低性能机器上还能完成工作。通常我们的做法是使用cgroup
之类的工具或者docker 模拟, 但是这在Mac book
上使用比较麻烦。其实mac有个内置软件,可以帮我们实现这个功能, 它就是 dmc
macos内置了dmc 软件 ,我们可以通过 dmc -h
查看它的一些介绍
➜ /tmp dmc -h
usage: dmc <commands...>
# commands:
start <mount> (profile_name|profile_index [-boot])
stop <mount>
status <mount> [-json]
show profile_name|profile_index
list
select <mount> (profile_name|profile_index)
configure <mount> <type> <access_time> <read_throughput> <write_throughput> [<ioqueue_depth> <maxreadcnt> <maxwritecnt> <segreadcnt> <segwritecnt>]
help | -h
它内置了几种不同的硬盘模式给我们选择。
➜ /tmp dmc list
0: Faulty 5400 HDD
1: 5400 HDD
2: 7200 HDD
3: Slow SSD
4: SATA II SSD
5: SATA III SSD
6: PCIe 2 SSD
7: PCIe 3 SSD
从0 -7 对应不同的速率模式,比如
➜ /tmp dmc show 0
Profile: Faulty 5400 HDD
Type: HDD
Access time: 52222 us
Read throughput: 50 MB/s
Write throughput: 50 MB/s
I/O Queue Depth: 16
Max Read Bytes: 16777216
Max Write Bytes: 16777216
Max Read Segments: 128
Max Write Segments: 128
➜ /tmp dmc show 7
Profile: PCIe 3 SSD
Type: SSD
Access time: 3 us
Read throughput: 3072 MB/s
Write throughput: 2560 MB/s
I/O Queue Depth: 256
Max Read Bytes: 67108864
Max Write Bytes: 67108864
Max Read Segments: 256
Max Write Segments: 256
使用也很简单
假设我们的磁盘挂载/tmp/data
目录。我们将它设置成 0 级别
这时只需要
sudo dmc start /tmp/data 0
然后查看一下状态,设置成功了。
➜ /tmp dmc status /tmp/data
Disk Mount Conditioner: ON
Profile: Custom
Type: HDD
Access time: 52222 us
Read throughput: 50 MB/s
Write throughput: 50 MB/s
I/O Queue Depth: 16
Max Read Bytes: 1048576
Max Write Bytes: 1048576
Max Read Segments: 128
Max Write Segments: 128
我们使用fio 工具验证一下
➜ /tmp fio --filename=./data/test1 -direct=1 --rw=write --ioengine=posixaio --bs=1m --iodepth=32 --size=1G --numjobs=1 --runtime=60 --time_base=1 --group_reporting --name=test-seq-read --log_avg_msec=1000
顺序写入一个1G的文件,它的速度只有95.4MB/s,IOPS 只有91
Run status group 0 (all jobs):
WRITE: bw=91.0MiB/s (95.4MB/s), 91.0MiB/s-91.0MiB/s (95.4MB/s-95.4MB/s), io=5468MiB (5734MB), run=60073-60073msec
现在dmc
关了跑同样的命令
/tmp sudo dmc stop /tmp/data
/tmp fio --filename=./data/test1 -direct=1 --rw=write --ioengine=posixaio --bs=1m --iodepth=32 --size=1G --numjobs=1 --runtime=60 --time_base=1 --group_reporting --name=test-seq-read --log_avg_msec=1000
测试结果到了 3211MB/s IOPS 3061,这才是这块磁盘的真实速度。
WRITE: bw=3062MiB/s (3211MB/s), 3062MiB/s-3062MiB/s (3211MB/s-3211MB/s), io=179GiB (193GB), run=60006-60006msec
总的来说,dmc 在测试场景下很有用,以前都不知道有这个软件, 如果您觉得能用起来,就去试试把。