@@ -1,28 +1,72 @@
11#!/usr/bin/env bash
2−
32cd "$(dirname "${BASH_SOURCE[0]}")" || exit
43
4+# Check that Hyperfine is installed.
55if ! command -v hyperfine > /dev/null 2>&1; then
66 echo "'hyperfine' does not seem to be installed."
77 echo "You can get it here: https://github.com/sharkdp/hyperfine"
88 exit 1
99fi
1010
11+# Determine the target directories.
12+get_target_dir() {
13+ if [[ -f "$HOME/.cargo/config" ]]; then
14+ grep 'target-dir[[:space:]]*=' "$HOME/.cargo/config" \
15+ | sed 's/^[[:space:]]*target-dir[[:space:]]*=//; s/^[[:space:]]*"//; s/"[[:space:]]*$//' \
16+ && return 0
17+ fi
18+
19+ echo "../../target"
20+}
21+
22+TARGET_DIR="$(get_target_dir)"
23+TARGET_DEBUG="${TARGET_DIR}/debug/bat"
24+TARGET_RELEASE="${TARGET_DIR}/release/bat"
25+
26+# Determine which target to benchmark.
27+BAT=''
28+for arg in "$@"; do
29+ case "$arg" in
30+ --system) BAT="bat" ;;
31+ --debug) BAT="$TARGET_DEBUG" ;;
32+ --release) BAT="$TARGET_RELEASE" ;;
33+ --bat=*) BAT="${arg:6}" ;;
34+ esac
35+done
36+
37+if [[ -z "$BAT" ]]; then
38+ echo "A build of 'bat' must be specified for benchmarking."
39+ echo "You can use '--system', '--debug', or '--release'."
40+ exit 1
41+fi
42+
43+# Ensure that the target is built.
44+if ! command -v "$BAT" &>/dev/null; then
45+ echo "Could not find the build of bat to benchmark."
46+ case "$BAT" in
47+ "bat") echo "Make you sure to symlink 'batcat' as 'bat'." ;;
48+ "$TARGET_DEBUG") echo "Make you sure to 'cargo build' first." ;;
49+ "$TARGET_RELEASE") echo "Make you sure to 'cargo build --release' first." ;;
50+ esac
51+ exit 1
52+fi
53+
54+# Run the benchmark.
1155echo "### Startup time"
1256echo
1357
14−hyperfine --warmup 3 bat
58+hyperfine --warmup 3 "$BAT"
1559
1660echo
1761echo "### Plain text"
1862echo
1963
20−hyperfine --warmup 3 "bat --language txt --paging=never 'test-src/jquery-3.3.1.js'"
64+hyperfine --warmup 3 "$(printf "%q" "$BAT") --language txt --paging=never 'test-src/jquery-3.3.1.js'"
2165
2266echo
2367echo "### Time to syntax-highlight large files"
2468echo
2569
2670for SRC in test-src/*; do
27− hyperfine --warmup 3 "bat --style=full --color=always --paging=never '$SRC'"
71+ hyperfine --warmup 3 "$(printf "%q" "$BAT") --style=full --color=always --paging=never $(printf "%q" "$SRC")"
2872done