1#!/usr/bin/env bash
2cd "$(dirname "${BASH_SOURCE[0]}")" || exit
3
4# Check that Hyperfine is installed.
5if ! command -v hyperfine > /dev/null 2>&1; then
6    echo "'hyperfine' does not seem to be installed."
7    echo "You can get it here: https://github.com/sharkdp/hyperfine"
8    exit 1
9fi
10
11# Determine the target directories.
12get_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
22TARGET_DIR="$(get_target_dir)"
23TARGET_DEBUG="${TARGET_DIR}/debug/bat"
24TARGET_RELEASE="${TARGET_DIR}/release/bat"
25
26# Determine which target to benchmark.
27BAT=''
28for 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
35done
36
37if [[ -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
41fi
42
43# Ensure that the target is built.
44if ! 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
52fi
53
54# Run the benchmark.
55echo "### Startup time"
56echo
57
58hyperfine --warmup 3 "$BAT"
59
60echo
61echo "### Plain text"
62echo
63
64hyperfine --warmup 3 "$(printf "%q" "$BAT") --language txt --paging=never 'test-src/jquery-3.3.1.js'"
65
66echo
67echo "### Time to syntax-highlight large files"
68echo
69
70for SRC in test-src/*; do
71    hyperfine --warmup 3 "$(printf "%q" "$BAT") --style=full --color=always --paging=never $(printf "%q" "$SRC")"
72done
73