| 1 |
<!-- spell-checker:ignore maximises --> |
| 2 |
|
| 3 |
# Benchmarking dd |
| 4 |
|
| 5 |
`dd` is a utility used for copying and converting files. It is often used for |
| 6 |
writing directly to devices, such as when writing an `.iso` file directly to a |
| 7 |
drive. |
| 8 |
|
| 9 |
## Understanding dd |
| 10 |
|
| 11 |
At the core, `dd` has a simple loop of operation. It reads in `blocksize` bytes |
| 12 |
from an input, optionally performs a conversion on the bytes, and then writes |
| 13 |
`blocksize` bytes to an output file. |
| 14 |
|
| 15 |
In typical usage, the performance of `dd` is dominated by the speed at which it |
| 16 |
can read or write to the filesystem. For those scenarios it is best to optimize |
| 17 |
the blocksize for the performance of the devices being read/written to. Devices |
| 18 |
typically have an optimal block size that they work best at, so for maximum |
| 19 |
performance `dd` should be using a block size, or multiple of the block size, |
| 20 |
that the underlying devices prefer. |
| 21 |
|
| 22 |
For benchmarking `dd` itself we will use fast special files provided by the |
| 23 |
operating system that work out of RAM, `/dev/zero` and `/dev/null`. This reduces |
| 24 |
the time taken reading/writing files to a minimum and maximises the percentage |
| 25 |
time we spend in the `dd` tool itself, but care still needs to be taken to |
| 26 |
understand where we are benchmarking the `dd` tool and where we are just |
| 27 |
benchmarking memory performance. |
| 28 |
|
| 29 |
The main parameter to vary for a `dd` benchmark is the blocksize, but benchmarks |
| 30 |
testing the conversions that are supported by `dd` could also be interesting. |
| 31 |
|
| 32 |
`dd` has a convenient `count` argument, that will copy `count` blocks of data |
| 33 |
from the input to the output, which is useful for benchmarking. |
| 34 |
|
| 35 |
## Blocksize Benchmarks |
| 36 |
|
| 37 |
When measuring the impact of blocksize on the throughput, we want to avoid |
| 38 |
testing the startup time of `dd`. `dd` itself will give a report on the |
| 39 |
throughput speed once complete, but it's probably better to use an external |
| 40 |
tool, such as `hyperfine` to measure the performance. |
| 41 |
|
| 42 |
Benchmarks should be sized so that they run for a handful of seconds at a |
| 43 |
minimum to avoid measuring the startup time unnecessarily. The total time will |
| 44 |
be roughly equivalent to the total bytes copied (`blocksize` x `count`). |
| 45 |
|
| 46 |
Some useful invocations for testing would be the following: |
| 47 |
|
| 48 |
```shell |
| 49 |
hyperfine "./target/release/dd bs=4k count=1000000 < /dev/zero > /dev/null" |
| 50 |
hyperfine "./target/release/dd bs=1M count=20000 < /dev/zero > /dev/null" |
| 51 |
hyperfine "./target/release/dd bs=1G count=10 < /dev/zero > /dev/null" |
| 52 |
``` |
| 53 |
|
| 54 |
Choosing what to benchmark depends greatly on what you want to measure. |
| 55 |
Typically you would choose a small blocksize for measuring the performance of |
| 56 |
`dd`, as that would maximize the overhead introduced by the `dd` tool. `dd` |
| 57 |
typically does some set amount of work per block which only depends on the size |
| 58 |
of the block if conversions are used. |
| 59 |
|
| 60 |
As an example, <https://github.com/uutils/coreutils/pull/3600> made a change to |
| 61 |
reuse the same buffer between block copies, avoiding the need to reallocate a |
| 62 |
new block of memory for each copy. The impact of that change mostly had an |
| 63 |
impact on large block size copies because those are the circumstances where the |
| 64 |
memory performance dominated the total performance. |