@@ -1,4 +1,5 @@
11#!/usr/bin/env bash
2+
23cd "$(dirname "${BASH_SOURCE[0]}")" || exit
34
45# Check that Hyperfine is installed.
@@ -19,16 +20,28 @@ get_cargo_target_dir() {
1920 cargo metadata --no-deps --format-version 1 | jq -r .target_directory
2021}
2122
23+heading() {
24+ bold=$(tput bold)$(tput setaf 220)
25+ normal=$(tput sgr0)
26+ echo
27+ printf "\n%s%s%s\n\n" "$bold" "$1" "$normal"
28+
29+ echo -e "\n### $1\n" >> "$REPORT"
30+}
31+
32+RESULT_DIR="benchmark-results"
33+REPORT="$RESULT_DIR/report.md"
34+
2235TARGET_DIR="$(get_cargo_target_dir)"
23−TARGET_DEBUG="${TARGET_DIR}/debug/bat"
2436TARGET_RELEASE="${TARGET_DIR}/release/bat"
2537
38+WARMUP_COUNT=3
39+
2640# Determine which target to benchmark.
2741BAT=''
2842for arg in "$@"; do
2943 case "$arg" in
3044 --system) BAT="bat" ;;
31− --debug) BAT="$TARGET_DEBUG" ;;
3245 --release) BAT="$TARGET_RELEASE" ;;
3346 --bat=*) BAT="${arg:6}" ;;
3447 esac
@@ -36,37 +49,48 @@ done
3649
3750if [[ -z "$BAT" ]]; then
3851 echo "A build of 'bat' must be specified for benchmarking."
39− echo "You can use '--system', '--debug', or '--release'."
52+ echo "You can use '--system', '--release' or '--bat=path/to/bat'."
4053 exit 1
4154fi
4255
43−# Ensure that the target is built.
4456if ! command -v "$BAT" &>/dev/null; then
45− echo "Could not find the build of bat to benchmark."
57+ echo "Could not find the build of bat to benchmark ($BAT)."
4658 case "$BAT" in
4759 "bat") echo "Make you sure to symlink 'batcat' as 'bat'." ;;
48− "$TARGET_DEBUG") echo "Make you sure to 'cargo build' first." ;;
4960 "$TARGET_RELEASE") echo "Make you sure to 'cargo build --release' first." ;;
5061 esac
5162 exit 1
5263fi
5364
54−# Run the benchmark.
55−echo "### Startup time"
56−echo
65+# Run the benchmarks
66+mkdir -p "$RESULT_DIR"
67+rm -f "$RESULT_DIR"/*.md
5768
58−hyperfine --warmup 3 "$BAT"
69+echo "## \`bat\` benchmark results" >> "$REPORT"
5970
60−echo
61−echo "### Plain text"
62−echo
71+heading "Startup time"
72+hyperfine \
73+ "$BAT" \
74+ --warmup "$WARMUP_COUNT" \
75+ --export-markdown "$RESULT_DIR/startup-time.md" \
76+ --export-json "$RESULT_DIR/startup-time.json"
77+cat "$RESULT_DIR/startup-time.md" >> "$REPORT"
6378
64−hyperfine --warmup 3 "$(printf "%q" "$BAT") --language txt --paging=never 'test-src/jquery-3.3.1.js'"
65−
66−echo
67−echo "### Time to syntax-highlight large files"
68−echo
79+heading "Plain text speed"
80+hyperfine \
81+ "$(printf "%q" "$BAT") --language txt --paging=never 'test-src/jquery-3.3.1.js'" \
82+ --warmup "$WARMUP_COUNT" \
83+ --export-markdown "$RESULT_DIR/plain-text-speed.md" \
84+ --export-json "$RESULT_DIR/plain-text-speed.json"
85+cat "$RESULT_DIR/plain-text-speed.md" >> "$REPORT"
6986
7087for SRC in test-src/*; do
71− hyperfine --warmup 3 "$(printf "%q" "$BAT") --style=full --color=always --paging=never $(printf "%q" "$SRC")"
88+ filename="$(basename "$SRC")"
89+ heading "Syntax highlighting speed: \`$filename\`"
90+
91+ hyperfine --warmup "$WARMUP_COUNT" \
92+ "$(printf "%q" "$BAT") --style=full --color=always --paging=never $(printf "%q" "$SRC")" \
93+ --export-markdown "$RESULT_DIR/syntax-highlighting-speed-${filename}.md" \
94+ --export-json "$RESULT_DIR/syntax-highlighting-speed-${filename}.json"
95+ cat "$RESULT_DIR/syntax-highlighting-speed-${filename}.md" >> "$REPORT"
7296done