Linux: flush buffers and cache for testing

Linux caches disk reads and buffers writes to improve filesystem performance, but that can play havoc with results when doing disk I/O benchmarking.

For example, search all of /var for text that likely won’t be found:

[brian@penguin ~]$ time sudo grep -r qazwsxedc /var
real    0m16.577s
user    0m0.425s
sys     0m0.463s

Now run the command a second time:

[brian@penguin ~]$ time sudo grep -r qazwsxedc /var
real    0m0.493s
user    0m0.404s
sys     0m0.086s

Half a second vs 16.5 seconds. The reason for the wild difference between the two time is on the first run the computer had to read the data from the disk. But as it did so, Linux put the data into its cache, which is in memory. So the second time the command ran, practically all of /var was in RAM, so it took a lot less time to scan the data.

Doing benchmarks when disk I/O is involved requires flushing the caches between tests. You can do that with the following command:

free && sync && sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches' && echo && free