From 76eea583b43d47110d76c637a6f744cfe9eb8993 Mon Sep 17 00:00:00 2001 From: Ben Schofield Date: Mon, 21 Aug 2023 12:10:31 -0700 Subject: [PATCH] Add benchmarking for `rm` Add benchmarking script and guide for `rm`, mostly copied from `ls` benchmarking guide. Tested `rm` using `jwalk` instead of `walkdir`, and saw a slight performance regression, if any change. --- src/uu/rm/BENCHMARKING.md | 52 +++++++++++++++++++++++++++++++++++++++ src/uu/rm/benchmark.sh | 8 ++++++ 2 files changed, 60 insertions(+) create mode 100644 src/uu/rm/BENCHMARKING.md create mode 100755 src/uu/rm/benchmark.sh diff --git a/src/uu/rm/BENCHMARKING.md b/src/uu/rm/BENCHMARKING.md new file mode 100644 index 000000000..ee524cb24 --- /dev/null +++ b/src/uu/rm/BENCHMARKING.md @@ -0,0 +1,52 @@ +# Benchmarking rm + +Run `cargo build --release` before benchmarking after you make a change! + +## Simple recursive rm + +- Get a large tree, for example linux kernel source tree. +- We'll need to pass a `--prepare` argument, since `rm` deletes the dir each time. +- Benchmark simple recursive rm with hyperfine: `hyperfine --prepare "cp -r tree tree-tmp" "target/release/coreutils rm -r tree-tmp"`. + +## Comparing with GNU rm + +Hyperfine accepts multiple commands to run and will compare them. To compare performance with GNU rm +duplicate the string you passed to hyperfine but remove the `target/release/coreutils` bit from it. + +Example: `hyperfine --prepare "cp -r tree tree-tmp" "target/release/coreutils rm -rf tree-tmp"` becomes +`hyperfine --prepare "cp -r tree tree-tmp" "target/release/coreutils rm -rf tree-tmp" "rm -rf tree-tmp"` +(This assumes GNU rm is installed as `rm`) + +This can also be used to compare with version of rm built before your changes to ensure your change does not regress this. + +Here is a `bash` script for doing this comparison: + +```shell +#!/bin/bash +cargo build --no-default-features --features rm --release +test_dir="$1" +hyperfine --prepare "cp -r $test_dir tmp_d" "rm -rf tmp_d" "target/release/coreutils rm -rf tmp_d" +``` + +## Checking system call count + +- Another thing to look at would be system calls count using strace (on linux) or equivalent on other operating systems. +- Example: `strace -c target/release/coreutils rm -rf tree` + +## Cargo Flamegraph + +With Cargo Flamegraph you can easily make a flamegraph of `rm`: + +```shell +cargo flamegraph --cmd coreutils -- rm [additional parameters] +``` + +However, if the `-r` option is given, the output becomes pretty much useless due to recursion. We can fix this by merging all the direct recursive calls with `uniq`, below is a `bash` script that does this. + +```shell +#!/bin/bash +cargo build --release --no-default-features --features rm +perf record target/release/coreutils rm "$@" +perf script | uniq | inferno-collapse-perf | inferno-flamegraph > flamegraph.svg +``` + diff --git a/src/uu/rm/benchmark.sh b/src/uu/rm/benchmark.sh new file mode 100755 index 000000000..5feaea4e8 --- /dev/null +++ b/src/uu/rm/benchmark.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +# Exit on any failures +set +x + +cargo build --no-default-features --features rm --release +test_dir="$1" +hyperfine --prepare "cp -r $test_dir tmp_d" "rm -rf tmp_d" "target/release/coreutils rm -rf tmp_d"