1

I want to know if shell 1 is better for a job than shell 2 with a test that I handle maybe with shell 3 for example ksh, dash, bash or zsh. I use a test script that builds my projects, starts it,feed it input via standard in and analyses the shell's behavior with Valgrind to get very informative traces of what the code is doing. Now I wonder if you can help me with good shell commands for testing purposes if I want to benchmark between 2 shells and more complicated pipelines, since I mostly made test cases with randon non-trivial pipelines.

I can imagine a large job greping through large files - is that a good way to know which shell is "faster" by making an expensive job or is it a matter of exeucting a large shell script?

The shell I want to test can take input like in this script and Valgrind can measure for data alignment problems or other bugs while testing expensive pipelines.

Do you know a good shell script for benchmarking different shells?

#!/bin/sh
echo "-- Testing our implementation of POSIX shell --"
echo ""
echo "- If you have any problem in passing a test read the corresponding"
echo "- source file to understand what the test is checking"
echo ""
printf "********************* PRESS ENTER TO RUN TESTS  ... "
#read _
make
valgrind --leak-check=yes ./shell .<< EOF
ls -al|grep open|awk '{print \$9}'
EOF
printf "********************* TEST WILDCARDS \n***** Press any key to listing all files in current directory...\nYou should see filesnames *.* below "
read _
./shell << EOF
ls
EOF
printf "********************* TEST ALGORITHMS ...  \n***** Press any key to run the algorithms... .\nYou should see the output from top -b -n1|head -8|tail -1 "
read _
valgrind --leak-check=yes ./shell .<< EOF
top|head -8|tail -1|sort -n|wc -l
EOF

printf "********************* TEST ALGORITHMS Part II.  ... .\nYou should see the output from who|awk '{print \$4 ; print \$3}'|sort -n|wc -l. "
read _
valgrind --leak-check=yes ./shell .<< EOF
who|awk '{print \$4 ; print \$3}'|sort -n|wc -l
EOF

printf "********************* TEST CHECKENV.  ..... .\nYou should see the output checkenv below "
read _
valgrind --leak-check=yes ./shell .<< EOF
checkenv
EOF
printf "********************* TEST DONE. YOU SHOULD SEE OUTPUT FROM TEST ABOVE ... "
read _
2
  • 5
    each shell has its own internal quirks. Hence the performance of the shell is very dependent on what you are going to perform with it. If the process is going to be a very repetitive one, say you will run a script to generate a report from a database, every hour for the rest of your life, then you can spare a day or two to run your report on each alternative shell and see which one works the best for it. timeing the processes is a good measure in my opinion.
    – MelBurslan
    May 5, 2016 at 21:45
  • 1
    The configure scripts for open source packages are probably good tests. May 9, 2016 at 7:49

2 Answers 2

2

The key to shell performance is to minimise the number of expensive system calls, in particular fork() and exec().

Don't use grep or sed inside a shell loop. Never use a pipeline with both; in most cases, it can be reduced to just sed or awk.

If it gets complicated, use a language that can parse regular expressions, and do loops, like or .

On the other hand, if you can get by with the simple text processing offered by the shell, you can squeeze a lot of performance out of it.

That said, the at&t ksh93 has always been optimised for speed, while bash and zsh have way more features.

1
  • So if I want to load test my shell, should I make very expensive calls and long pipelines? Thanks for a good answer! May 8, 2016 at 7:22
1
for shell in $(sed '1d' /etc/shells); do # or use your own list of shells
    echo "$shell -"
    time $shell /path/to/script
done
2
  • How do you know all shells are going to be under /bin? Mightn't it be better to use the contents of /etc/shells?
    – forquare
    May 7, 2016 at 22:19
  • I don't. But that was just an example for a list of shells through which to iterate. Naturally, you can use any set of shells, with or without using brace expansion. for shell in $(sed '1d' /etc/shells); do [...] would indeed be comprehensive for any given system.
    – DopeGhoti
    May 8, 2016 at 1:01

Not the answer you're looking for? Browse other questions tagged .