Using dd to test Write speeds

I'm trying to check the write speed of different devices that I have using the following:

dd bs=1M count=256 if=/dev/zero of=/path/to/device oflag=dsync

I want an accurate reading of write speed and I was wondering if I should have any considerable speed difference by using a file that isn't just zeros, or if using /dev/zero is a reasonable way to test write speed.


Solution 1:

Here's a test of /dev/zero's throughput on my system:

$ dd if=/dev/zero of=/dev/null bs=1M count=1000000
1000000+0 records in
1000000+0 records out
1048576000000 bytes (1,0 TB) copied, 65,2162 s, 16,1 GB/s

There's no other bottleneck than the CPU's cache speed here. That means that in my system /dev/zero can generate up to 16,1 GB/s of zeros, so it definetly should be fast enough for your purpose.

Solution 2:

Not always. I was recently testing a disk in exactly the same way and /dev/zero tricked me into thinking I had the performance I needed because the external disk was using NTFS disk compression. At first I tried using /dev/urandom to fix this problem, but I discovered that tricked me into thinking that things were going too slowly. If you want to do this without being tricked then you need to write a random file to a tmpfs location and copy that file to the destination disk.

dd if=/dev/urandom of=/tmp/temp-random.img bs=1G count=1 iflag=fullblock oflag=dsync
dd if=/tmp/temp-random.img of=/path/to/device/temp-random.img bs=1G count=1 iflag=fullblock oflag=dsync

Please note that this assumes that /tmp is mounted as tmpfs, if that's not the case then you should mount a temporary filesystem and use that instead:

sudo mkdir /mnt/tmp
sudo mount -t tmpfs tmpfs /mnt/tmp/
dd if=/dev/urandom of=/mnt/tmp/temp-random.img bs=1G count=1 iflag=fullblock oflag=dsync
dd if=/mnt/tmp/temp-random.img of=/path/to/device/temp-random.img bs=1G count=1 iflag=fullblock oflag=dsync
sudo umount /mnt/tmp/
sudo rmdir /mnt/tmp

Also note that you need to make sure that your file is large enough to be bigger than the cache of the disk (set count=N much bigger than the cache size in GiB).