Testing Raspberry PI Storage Speed (SD, USB) with dd
Last Updated on 6th February 2022 by peppe8o
Raspberry PI performaces are deeply impacted from storage speed.
To figure it out, you can imagine for example your SD card as a PC hard disk. Changing mechanical HDD with an SDD storage will drastically increase operating system performances as OS can read and write faster programs code and files.
For this reason I refer in my tricks to improve Raspberry PI performances also using a fast SD card.
The best way to test effective write/read speed is writing and reading on it. But you should consider results from this test indicative (even really near to real speed) because affected from some factors:
- live OS resources usage – if you test a storage media (micro SD for Raspberry PI) where OS is running, then some write/read running processes can reduce measured speed. For this reason I suggest to close all running programs before performing test. It is also better testing speed from a very light OS (like Raspberry PI OS Lite)
- hardware bottelnecks – speed testing can be affected by hardware limits involved in writing. This means, to be synthetic, that if you test a USB 3.0 stick from a RPI having only USB 2.0 ports will result in speed performances restricted by the slower part (USB 2.0 port).
For this test I’ll use “dd” terminal command. a common utility for Unix and Unix-like operating systems (already available in all Raspberry PI OS distributions) whose primary purpose is to convert and copy files.
In this tutorial I’m going to show you how to test storage speed (SD card or any USB storage) from terminal. This guide will use a Raspberry PI Zero W, but works with all Raspberry PI boards.
What we Need
Please find below hardware I used:
- Raspberry PI Zero W (including proper power supply or using a smartphone micro usb charger with at least 3A) or newer Raspberry PI Board
- high speed micro SD card (I suggest at least 16 GB, at least class 10)
Check hardware prices with following links:
Step-by-Step Procedure
If not already done, please install Raspberry PI OS Lite (for a headless, fast OS) or install Raspberry PI OS Desktop (in this case, using its internal terminal).
Eve not strictly needed, I always suggest to make your OS up-to-date. From terminal:
sudo apt update -y && sudo apt upgrade -y
In following steps, we’ll first test writing speed by creating a dummy file (100MB). You can change this size according to you preferences. Greater files will get more precise results, but will require more time.
After this, we’ll get reading speed by reading the same file.
We are going to use following dd command:
dd if=/dev/input.file of=/path/to/output.file bs=BYTES count=N oflag=FLAGS
Options are explained in following list:
- if = this option enables reading from a file (having “/dev/input.file” absolute or relative path) instead of using stdin
- of = this option enables writing to a file (having “/path/to/output.file” absolute or relative path) instead of using stdout
- bs = bytes to read/write at a time (default: 512). BYTES may be followed by the following multiplicative suffixes: c=1, w=2, b=512, kB=1000, K=1024, MB=1000×1000, M=1024×1024, xM=M, GB=1000x1000x1000, G=1024x1024x1024, and so on for T, P, E, Z, Y.
- count = number of input blocks to copy. N uses same multiplicative suffixes as bs
- oflag = defines how to write data. Common values are: direct (use direct I/O for data), dsync (use synchronized I/O for data) and sync (like dsync, but also for metadata)
Important note: when using a “pure” device path in of option (such as /dev/sda) the data stored there will be lost. For that reason, you should only use empty storage media or specify file name to write, unless you know what you are doing.
Test Write Speed Performance
Write speed test will use a dummy source to generate bytes to write on phisical file on storage. The /dev/zero is used as source. It is a special device file which generates a continuous stream of null characters (zero-value bytes). If used as destination path, it accepts and discards all data written to it.
For write speed test purposes, at least 100MB of free storage space required (creating 5 blocks of 20MB each one copied inside the same output file).
You can run this test by using following terminal command:
dd if=/dev/zero of=./speedTestFile bs=20M count=5 oflag=direct
This will generate a similar output to following one:
pi@raspberrypi:~ $ dd if=/dev/zero of=./speedTestFile bs=20M count=5 oflag=direct
5+0 records in
5+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 8.12716 s, 12.9 MB/s
Last row reports measured time spent and average speed during copy operations. Assuming /dev/zero read time irrelevant compared to storage performances, these values are the write performance results.
You can also change bs and count values to perform tests in changing conditions, taking care to have enough disk space for each test.
Test Read Speed Performance
Read speed test will use previously generated file as source, sending data to /dev/zero virtual device file. This will require dsync value for oflag option instead of direct.
Terminal command to perform read test will be the following:
dd if=./speedTestFile of=/dev/zero bs=20M count=5 oflag=dsync
Resulting in a final report similar to following one:
pi@raspberrypi:~ $ dd if=./speedTestFile of=/dev/zero bs=20M count=5 oflag=dsync
5+0 records in
5+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 4.50321 s, 23.3 MB/s
Last row reports opertion time spent and average speed. Again, assuming write speed to /dev/zero irrelevant compared to storage performances, measured speed will be likely near your storage read performances.
Little curiosity. Running again read test will produce different speed results:
pi@raspberrypi:~ $ dd if=./speedTestFile of=/dev/zero bs=20M count=5 oflag=dsync
5+0 records in
5+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.447308 s, 234 MB/s
Second test should give different performances because this time your OS knows that the same file have been read and is available and cached in RAM (without changes from storage copy), so uses RAM copy instead of storage one. If you regenerate speedTestFile again, you will have a “new” file (even if with same name) and read test will go back to first reading speed.
Enjoy!
Hello, thanks for the procedure – its worked well with my new external SamsungT5 portable USB3 drive.
Note: At the beginning of the article you wrote that you want to create and test with a 1GB file – but later on you write and read 20MB x 5 = 100 MiB.
Yes, you are right. Sorry, this was my fault. Post updated accordingly.
Many thanks
Hey! I ran the speed test command and it returned the “records in” and “records out” values, but the last line that shows the actual speed did not appear. What could be wrong with my setup? Thanks!
This is really strange. You can try appending at the end of command the option “status=progress”. What is your OS? Is it updated and upgraded?