-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrcenv.benchmarks
More file actions
executable file
·106 lines (81 loc) · 2.9 KB
/
srcenv.benchmarks
File metadata and controls
executable file
·106 lines (81 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
#
# srcenv.benchmarks - Benchmark suite for srcenv
# region Setup
unset SRCENV_RESTORE
unset SRCENV_TEST_A
unset SRCENV_TEST_B
unset SRCENV_TEST_C
unset SRCENV_TEST_ESCAPE
unset SRCENV_TEST_MULTILINE
export SRCENV_TEST_EXISTING=1
unset SRCENV_TEST_NOT_EXISTING
env_input=test.env
posix_input=test.env.sh
tee "$env_input" > /dev/null << 'EOF'
# Interpolation tests
SRCENV_TEST_A=${SRCENV_TEST_EXISTING:+6}
SRCENV_TEST_B=${SRCENV_TEST_NOT_EXISTING:-7}
SRCENV_TEST_C=$((SRCENV_TEST_A * SRCENV_TEST_B))
# Escape sequence tests
SRCENV_TEST_ESCAPE="\$NOT_EXPANDED\t\r\n'\""
# Multi-line tests
SRCENV_TEST_MULTILINE="
$SRCENV_TEST_A
$SRCENV_TEST_B
$SRCENV_TEST_C"
EOF
tee "$posix_input" > /dev/null << 'EOF'
# Unexported variable tests
SRCENV_TEST_NOT_EXPORTED=1
# Interpolation tests
export SRCENV_TEST_A=${SRCENV_TEST_EXISTING:+6}
export SRCENV_TEST_B=${SRCENV_TEST_NOT_EXISTING:-7}
export SRCENV_TEST_C=$((SRCENV_TEST_A * SRCENV_TEST_B * SRCENV_TEST_NOT_EXPORTED))
# Escape sequence tests
export SRCENV_TEST_ESCAPE="\$NOT_EXPANDED\t\r\n'\""
# Multi-line tests
export SRCENV_TEST_MULTILINE="
$SRCENV_TEST_A
$SRCENV_TEST_B
$SRCENV_TEST_C"
EOF
# shellcheck disable=SC2317
cleanup() {
unset SRCENV_TEST_EXISTING
rm -f "$env_input" "$posix_input"
trap - EXIT
exit
}
trap cleanup EXIT INT HUP TERM
# endregion Setup
# region Benchmarks
if ! command -v hyperfine > /dev/null; then
NORMAL=$(tput sgr0 2> /dev/null || printf '\033[0m')
RED=$(tput setaf 1 2> /dev/null || printf '\033[31m')
YELLOW=$(tput setaf 3 2> /dev/null || printf '\033[33m')
echo >&2 "${RED}✖${NORMAL} ${YELLOW}hyperfine${NORMAL} not found; see https://github.com/sharkdp/hyperfine for installation options"
exit 1
fi
case $1 in
-*) against=$(command -v srcenv) ;;
'') against=$(command -v srcenv); shift ;;
*) against=$1; shift ;;
esac
if [ "$against" = shells ]; then
sh=$(command -v sh)
ash=$(command -v ash)
bash=$(command -v bash)
dash=$(command -v dash)
ksh=$(command -v ksh)
mksh=$(command -v mksh)
zsh=$(command -v zsh)
hyperfine --shell=none "$@" -- ${sh:+"sh srcenv sh $env_input"} ${ash:+"ash srcenv ash $env_input"} ${bash:+"bash srcenv bash $env_input"} ${dash:+"dash srcenv dash $env_input"} ${ksh:+"ksh srcenv ksh $env_input"} ${mksh:+"mksh srcenv mksh $env_input"} ${zsh:+"zsh srcenv zsh $env_input"}
echo
hyperfine --shell=none "$@" -- ${sh:+"sh srcenv sh $posix_input"} ${ash:+"ash srcenv ash $posix_input"} ${bash:+"bash srcenv bash $posix_input"} ${dash:+"dash srcenv dash $posix_input"} ${ksh:+"ksh srcenv ksh $posix_input"} ${mksh:+"mksh srcenv mksh $posix_input"} ${zsh:+"zsh srcenv zsh $posix_input"}
exit 0
fi
hyperfine --shell=none "$@" -- "./srcenv sh $env_input" ${against:+"$against sh $env_input"}
echo
hyperfine --shell=none "$@" -- "./srcenv sh $posix_input" ${against:+"$against sh $posix_input"}
# endregion Benchmarks