| 1 |
<!-- spell-checker:ignore tmpfs --> |
| 2 |
|
| 3 |
# Benchmarking shuf |
| 4 |
|
| 5 |
`shuf` is a simple utility, but there are at least two important cases |
| 6 |
to benchmark: with and without repetition. |
| 7 |
|
| 8 |
When benchmarking changes, make sure to always build with the `--release` flag. |
| 9 |
You can compare with another branch by compiling on that branch and then |
| 10 |
renaming the executable from `shuf` to `shuf.old`. |
| 11 |
|
| 12 |
## Generate sample data |
| 13 |
|
| 14 |
Sample input can be generated using `/dev/random`: |
| 15 |
|
| 16 |
```shell |
| 17 |
curl -o input.txt https://www.gutenberg.org/files/100/100-0.txt |
| 18 |
``` |
| 19 |
|
| 20 |
To avoid distortions from IO, it is recommended to store input data in tmpfs. |
| 21 |
|
| 22 |
## Without repetition |
| 23 |
|
| 24 |
By default, `shuf` samples without repetition. |
| 25 |
|
| 26 |
To benchmark only the randomization and not IO, we can pass the `-i` flag with |
| 27 |
a range of numbers to randomly sample from. An example of a command that works |
| 28 |
well for testing: |
| 29 |
|
| 30 |
```shell |
| 31 |
hyperfine --warmup 10 "target/release/shuf -i 0-10000000 > /dev/null" |
| 32 |
``` |
| 33 |
|
| 34 |
To measure the time taken by shuffling an input file, the following command can |
| 35 |
be used: |
| 36 |
|
| 37 |
```shell |
| 38 |
hyperfine --warmup 10 "target/release/shuf input.txt > /dev/null" |
| 39 |
``` |
| 40 |
|
| 41 |
It is important to discard the output by redirecting it to `/dev/null`, since |
| 42 |
otherwise, a substantial amount of time is added to write the output to the |
| 43 |
filesystem. |
| 44 |
|
| 45 |
## With repetition |
| 46 |
|
| 47 |
When repetition is allowed, `shuf` works very differently under the hood, so it |
| 48 |
should be benchmarked separately. In this case, we have to pass the `-n` flag or |
| 49 |
the command will run forever. An example of a hyperfine command is |
| 50 |
|
| 51 |
```shell |
| 52 |
hyperfine --warmup 10 "target/release/shuf -r -n 10000000 -i 0-1000 > /dev/null" |
| 53 |
``` |
| 54 |
|
| 55 |
## With huge interval ranges |
| 56 |
|
| 57 |
When `shuf` runs with huge interval ranges, special care must be taken, so it |
| 58 |
should be benchmarked separately also. An example of a hyperfine command is |
| 59 |
|
| 60 |
```shell |
| 61 |
hyperfine --warmup 10 "target/release/shuf -n 100 -i 1000-2000000000 > /dev/null" |
| 62 |
``` |