Simple speed tests of SD Card and RAM on (embedded) Linux
One of dd's purposes is to copy a file, but copying a file is not really what we want. If our source would be a file it would somewhat complicated to vary the size of the data transferred. Additionally reading the source will have some impact we want to minimize. In Linux there is a special device zero which appear in the file system just like a normal file and provides as much as zero values as we like. We will use this as source for our speed tests.
Warning: Wrong usage of dd may damage your system. Always have a backup. Never test on production systems.
Testing the RAM
O.k. if we want to the the RAM speed we have to write to the RAM, right? Fine, so lets have a look if we already have a RAM-Disk we could write to (type mount). If you see a line like this you probably have a RAM-Disk:
tmpfs on /media/ram type tmpfs (rw,relatime)
If not, lets create one:
mount -t tmpfs -o size=32M tmpfs /media/ram
Now lets write some data to the disk. The following command will write 30MB of zeros in 1MB chunks to a file on the RAM-Disk:
dd if=/dev/zero of=/media/ram/test.bin bs=1M count=30 oflag=direct
The oflag=direct parameter tells dd to use direct I/O for the data and therefore omit the cache. Note that the kernel may impose restrictions on read or write buffer sizes, so if you get an EINVAL its best to have a look into the manual.
Testing SD-Cards
You can do the same with an SD-Card. Either by writing in a file like in the example above, or by writing to the device directly (e.g. /dev/sdb1):
dd if=/dev/zero of=/dev/sdb1 bs=1M count=30 oflag=direct
Writing to a device directly will destroy your data on the card, so make sure you test on an empty card. On the other hand you will test the card without writing file-system meta data.
If you want to test the read performance you may use the following command to read from a file:
sudo dd if=/media/0AF6-3F81/test.bin of=/dev/null bs=1M count=30
We use the device /dev/null to trash the data (instead of writing the data to a real file). By reading the file the read cache is filled with the data from the file. A subsequent read would test your read cache instead of your sd card so we have to clear the cache before doing a read again:
sudo echo 3 | sudo tee /proc/sys/vm/drop_caches
Note: This is a non-destructive operation, and dirty objects are not freed, but you have to run sync first to make sure all your cached objects are freed.
Links: