Last active
October 3, 2023 22:02
-
-
Save ar1ja/510437a0bd866a377e129f48e5cb3e2a to your computer and use it in GitHub Desktop.
benchmark for shells -- performance and features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# -*- | |
# name(ben.sh) | |
# description(benchmark for shells -- performance and features) | |
# running(to run it id suggest to keep it fair, i personally did : `env -i bash --norc -i <ben.sh` ( 26 s ) and `env -i busybox ash -i <ben.sh` ( 19 s )) | |
# author(Ari Archer <[email protected]> [ari-web.xyz]) | |
# license(wtfpl) | |
# -*- | |
# | |
[ "$WITH_DEBUG" ] && set -x | |
set -eu | |
# texts exports | |
export TEST_VAR=10 | |
# tests readonly | |
readonly TEST_VAR_RO=10 | |
# tests aliases | |
alias pipes_alias=pipes | |
# tests local variables, if-elif-else construction, [ ] builtin, echo builtin, | |
# subshells, mathematical expressions, recursion, variables, subsells | |
fib_if() { | |
local n="$1" | |
if [ "$n" -eq 0 ]; then | |
echo 0 | |
elif [ "$n" -eq 1 ]; then | |
echo 1 | |
else | |
n0="$(fib_if $((n - 1)))" | |
n1="$(fib_if $((n - 2)))" | |
echo "$((n0 + n1))" | |
fi | |
} | |
# tests local variables, argument references, [ ] builtin, echo builtin, | |
# recursion, pipes, `read` builtin, mathematical expressions, conditions, | |
# { } syntax, subsells | |
fib_cond() { | |
local n="$1" | |
[ "$1" -le 1 ] && echo "$1" || { | |
fib_cond $((n - 1)) | read -r n0; | |
fib_cond $((n - 2)) | read -r n1; | |
echo "$((n0 + n1))" | |
} | |
} | |
# tests variables, for loops, subshells, mathematical expressions, variable reassignment, | |
# string formatting, printf builtin | |
for_compute_seq() { | |
numbers=1 | |
for number in $(seq 99999); do | |
numbers="$((numbers * number + 1))" | |
done | |
numbers="$numbers.2$((numbers / 2))" | |
printf ':D computation of of first 99999 numbers is %f, %s ( %g )\n' "$numbers" "[${numbers}]" "$numbers" | |
} | |
# tests local variables, variables, while loops, variable reassignment, mathematical expressions, | |
# [ ] builtin, loop flow, printf builtin | |
while_compute_seq() { | |
local idx=0 | |
numbers=1 | |
while true; do | |
idx="$((idx + 1))" | |
[ "$((idx % 1300))" = 0 ] && continue | |
numbers="$((numbers * idx + 1))" | |
[ "$idx" = 99999 ] && break | |
done | |
numbers="$numbers.2$((numbers / 2))" | |
printf 'computation of first 99999 numbers is %g ( %f, %s )\n' "$numbers" "$numbers" "[${numbers}]" | |
} | |
# tests pipes, accessing of global variables, ( ) syntax, read builtin, eval builtin | |
pipes() { | |
fib_cond "$TEST_VAR_RO" | (read -r x; eval "echo $TEST_VAR $x") | |
} | |
# tests for loops, subshells, { } syntax, || syntax, redirects, true builtin, job syntax, | |
# wait builtin | |
bg_jobs() { | |
sleep 5 & | |
for _ in $(seq 100); do | |
sleep 0.06 | |
{ sleep 3 && echo "done $? $!" 2>/dev/null || true; } & | |
done | |
wait | |
sleep 1 & | |
sleep 6 & | |
sleep 2 & | |
wait | |
} | |
# tests for loops, commands, command builtin | |
commands() { | |
for _ in $(seq 5); do | |
id | |
command id | |
done | |
} | |
# tests a bunch of builtins | |
builtins() { | |
echo 'sh -c id' | . /dev/stdin | |
eval 'ls -a' | |
set | head -n 10 | |
shift || true | |
test 0 = 0 | |
times | |
[ ! -z "$TEST_VAR" ] | |
unset TEST_VAR | |
return 0 | |
} | |
# tests mathematical expressions, delimeter piping, for loops, pre-determened for loop args | |
test_maths() { | |
for _ in 1 2 3 4 5; do | |
cat <<EOF | |
$((~100 ** 6)) | |
$((3 * (4 + 5) - 2)) | |
$((~10 ** 5 + 1)) | |
$((3 << 2)) | |
$((10 ** 5 - 1000)) | |
$((!0 && 1)) | |
$((2 ** 3 & 1 - 5 * 10000 - 1 + 10)) | |
$((59 >= 5 && 100 <= 200)) | |
$((4 ** 5)) | |
$((1 << 4)) | |
$((0 - 1)) | |
$((2 ^ 3 & 1 - 5 * 10000 - 1 + 500)) | |
$((2 ** 3 & 1 - 5 * 10000 - 1)) | |
$((2 ^ 3 & 1 - 5 * 10000 - 1)) | |
$((~7)) | |
$((4 << 3)) | |
$((15 > 10)) | |
$((25 > 20)) | |
$((0 < 5)) | |
$((1 << 3 + 2)) | |
$((5 == 5)) | |
$((7 ** 3)) | |
$((15 / 6)) | |
$((3 << 2 - 1)) | |
$((10 * (2 + 3) + 4)) | |
$((5 > 3 || 7 > 10)) | |
$((10 ** 6)) | |
$((~10)) | |
$((59 >= 5)) | |
$((10 ** 5)) | |
$((1 << 3)) | |
$((1812 > 5 && 10 < 1000)) | |
$((4912 % 19 - 1)) | |
$((2 ^ 4)) | |
$((1 <= 5)) | |
$((5 / 2 + 3)) | |
$((197371837849873298)) | |
$((10 >> 2)) | |
$((2 ** 4 - 10)) | |
$((~10 - 1)) | |
$((10 / (2 + 3))) | |
$((60 & 13)) | |
$((15 | 6 - 1)) | |
$((5 > 3 && 7 < 10)) | |
$((100 / 5)) | |
$((2 / 6 * 100)) | |
$((-837167198743)) | |
$((15 | 6)) | |
$((8 > 6 || 9 > 12)) | |
$((25 | 10)) | |
$((5 > 3 || 7 > 10 && 1 > 5)) | |
$((183 + 17)) | |
$((5 >> 1 + 3)) | |
$((2 ** 6 - 1)) | |
$((~8)) | |
$((1000 != 5 && 1000 != 2000)) | |
$((2 / 6 * 100 + 25)) | |
$((183 + 17 - 10)) | |
$((4 ** 5 + 10)) | |
$((5 >> 1)) | |
$((~10 ** 5)) | |
$((5 / 2)) | |
$((6 % 1992 - 1 ^ 2)) | |
$((4 * (8 + 2) - 5)) | |
$((20 % 9)) | |
$((10 / (2 + 3) + 3)) | |
$((5 > 3 && 7 < 10 || 1 < 5)) | |
$((!1)) | |
$((~20)) | |
$((0 - 1 + 2)) | |
$((1000 != 5)) | |
$((45 & 12 + 5)) | |
$((3 * (4 + 5))) | |
$((5 == 5 || 10 == 20)) | |
$((6 % 1992 - 1 ^ 2 + 4)) | |
$((2 ** 4)) | |
$((1812 > 5)) | |
$((-1812 > 5)) | |
$((1 <= 5 || 200 >= 100)) | |
$((~7 + 5)) | |
$((45 & 12)) | |
$((!0)) | |
$((4912 % 19 - 1 + 100)) | |
$((0 < 5 || 10 > 20)) | |
$((8 > 6 && 9 < 12)) | |
$((15 > 10 && 20 < 30)) | |
$((4 * (8 + 2))) | |
EOF | |
done | |
} | |
# tests everything, $0, $@, redirections, exec builtin | |
main() { | |
echo >/dev/shm/args | |
for arg in "$0" "$@"; do | |
echo "$arg" >>/dev/shm/args | |
echo "$(seq 10) $arg" >/dev/shm/seq | |
done | |
fib_if 18 | |
fib_cond 18 | |
for_compute_seq | |
while_compute_seq | |
pipes | |
bg_jobs | |
commands | |
builtins "$@" | |
test_maths | |
exec ls | |
} | |
echo trying | |
main hello world this is "$@" | |
echo gbye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment