Skip to content

Commit

Permalink
Try implemente benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Mar 19, 2024
1 parent 9dbc0ac commit e585156
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
target
tarpaulin-report.html
cobertura.xml
svejk.txt
rur.txt
6 changes: 5 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dev: format build clippy test docs
dev-docs:
#!/usr/bin/env -S sh -eux
mdbook serve --open &
cargo watch ---ignore docs -- just docs &
cargo watch --ignore '*.{md,css,sh,txt}' -- just docs &
trap 'kill $(jobs -pr)' EXIT
wait

Expand Down Expand Up @@ -51,6 +51,10 @@ test:
docs:
cargo run --package xtask -- docs

# Run benchmarks
bench:
./bench.sh >./docs/guide/benchmarks.md

# Run fuzzer
fuzz:
cargo +nightly fuzz run --jobs {{num_cpus()}} pattern
Expand Down
161 changes: 161 additions & 0 deletions bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env sh

set -eu

echo "# Benchmarks"
echo
echo 'This page contains `rew` benchmarks against various other tools:'
echo
echo "- [GNU coreutils](https://www.gnu.org/software/coreutils/)"
echo "- [uutils coreutils](https://github.com/uutils/coreutils?tab=readme-ov-file)"

specs() {
echo "<details>"
echo "<summary><code>$1</code></summary>"
echo
echo '```'
sh -c "$1"
echo '```'
echo
echo "</details>"
}

echo
echo "## Environment"
echo
echo "All benchmarks were run within the following environment."
echo
echo "System:"
echo
specs 'uname -a'
specs 'lscpu'
specs 'lsmem'
echo
echo "Software:"
echo
specs 'hyperfine --version'
specs 'rew --version'
specs 'coreutils --help | head -n1'
specs 'seq --version'
specs 'head --version'

BASE_DATA_URL=https://jpikl.github.io/data

SVEJK_FILE=svejk.txt
SVEJK_REPEAT=100
SVEJK_INIT="curl $BASE_DATA_URL/$SVEJK_FILE | rew loop $SVEJK_REPEAT >$SVEJK_FILE"

RUR_FILE=rur.txt
RUR_REPEAT=1000
RUR_INIT="curl $BASE_DATA_URL/$RUR_FILE | rew loop $RUR_REPEAT >$RUR_FILE"

[ -f "$SVEJK_FILE" ] || sh -c "$SVEJK_INIT"
[ -f "$RUR_FILE" ] || sh -c "$RUR_INIT"

echo
echo "## Data"
echo
echo "The following text files were used as input data for benchmarking."
echo
echo '```shell'
echo "$SVEJK_INIT"
echo '```'
echo
echo "- UTF-8, non-ascii (czech diacritics)"
echo "- 5913 lines (repeated $SVEJK_REPEAT times)"
echo "- very long lines (up to 4952 chars)"
echo "- trimmed whitespaces"
echo
echo '```shell'
echo "$RUR_INIT"
echo '```'
echo
echo "- UTF-8, non-ascii (czech diacritics)"
echo "- 4477 lines (repeated $RUR_REPEAT times)"
echo "- short lines (up to 81 chars)"
echo "- untrimmed whitespaces"

BENCH_OUT=.bench.md

bench_group() {
echo
echo "### $1"
}

bench() {
set -- hyperfine --warmup 5 "$@"
"$@" --sort mean-time --export-markdown "$BENCH_OUT" 1>&2

echo
echo "Command:"
echo
echo '```shell'

printf "$1"
shift

while [ $# -gt 0 ]; do
printf ' \\\n '

case "$1" in
--warmup)
printf "%s %s" "$1" "$2"
shift 2
;;
--setup | --cleanup)
printf "%s '%s'" "$1" "$2"
shift 2
;;
*)
printf "'%s'" "$1"
shift
;;
esac
done

echo
echo '```'
echo
echo "Results:"
echo

cat "$BENCH_OUT"
rm "$BENCH_OUT"
}

echo
echo "## Runs"

bench_group "rew seq"
for MULT in '' 0 00; do
bench \
"seq 1 1$MULT 1000000$MULT" \
"rew seq 1..1000000$MULT 1$MULT"
done
exit
bench_group "rew first"
for FILE in "$SVEJK_FILE" "$RUR_FILE"; do
bench \
"head -n 100000 <$FILE" \
"coreutils head -n 100000 <$FILE" \
"rew first 100000 <$FILE"
done

bench_group "rew last"
for FILE in "$SVEJK_FILE" "$RUR_FILE"; do
bench \
"tail -n 100000 <$FILE" \
"coreutils tail -n 100000 <$FILE" \
"rew last 100000 <$FILE"
done

bench_group "rew cat"
for FILE in "$SVEJK_FILE" "$RUR_FILE"; do
bench \
"cat <$FILE" \
"coreutils cat <$FILE" \
"rew cat <$FILE" \
"rew cat --bytes <$FILE" \
"rew cat --chars <$FILE" \
"rew cat --lines <$FILE"
done
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [Installation](./guide/installation.md)
- [FAQ](./guide/faq.md)
- [Benchmarks](./guide/benchmarks.md)

# Reference

Expand Down
Loading

0 comments on commit e585156

Please sign in to comment.