#!/bin/sh

myshell=$(basename $(readlink /proc/$$/exe))
bigdirs="/usr/lib/$(uname -m)-linux-gnu /usr/lib64" # a dir with a lot of files
wordfiles='words american-english' # a big file

for d in $bigdirs; do
	if [ -d "$d" ]; then
		filedir=$d
		break
	fi
done
[ -d "$filedir" ] || exec echo "bigdir missing!"

for w in $wordfiles; do
	if [ -f "/usr/share/dict/$w" ]; then
		stringsfile=/usr/share/dict/$w
		break
	fi
done
[ -f "$stringsfile" ] || exec echo "stringsfile missing!"

export myshell filedir stringsfile

echo "Bencharking $myshell"

echo 'Test 1: strings and arithmetic operations'

env time -f 'Done in %e seconds' $myshell <<'EOF'
for i in a b c d e f g h i j k l m n o p q r s t u v w x y z
do
	env echo -n $i
	for j in $(cat $stringsfile); do
		case "$j" in
			$i*)	res=$(($res+${#j}));;
		esac
	done
done
echo
EOF
echo

echo 'Test 2: wildcard operations and big memory allocations'
env time -f 'Done in %e seconds' $myshell <<'EOF'
for i in a b c d e f g h i j k l m n o p q r s t u v w x y z
do
	env echo -n $i
	for j in $filedir/*$i*; do
		res="$res:$j"
	done
done
echo
EOF
echo

echo 'Test 3: startup time'
env time -f 'Done in %e seconds' $myshell <<'EOF'
for i in $(seq -s ' ' 10)
do
	env echo -n $i
	i=1000
	while [ $i -ge 0 ]; do
		$myshell -c ''
		i=$((i-1))
	done
done
echo
EOF
