如何对不同的 shell 进行基准测试

如何对不同的 shell 进行基准测试

我想知道 shell 1 是否比 shell 2 更适合工作,我可以使用 shell 3 进行测试,例如 ksh、dash、bash 或 zsh。我使用一个测试脚本来构建我的项目,启动它,通过标准输入为其提供输入,并使用 Valgrind 分析 shell 的行为,以获得有关代码正在执行的操作的信息丰富的跟踪。现在我想知道如果我想在 2 个 shell 和更复杂的管道之间进行基准测试,您是否可以帮助我使用良好的 shell 命令进行测试,因为我主要使用随机的非平凡管道来制作测试用例。

我可以想象一项大型工作通过大型文件进行 greping - 这是通过执行一项昂贵的工作来了解哪个 shell“更快”的好方法还是执行大型 shell 脚本的问题?

我想要测试的 shell 可以像此脚本一样接受输入,并且 Valgrind 可以在测试昂贵的管道时测量数据对齐问题或其他错误。

您知道用于对不同 shell 进行基准测试的好 shell 脚本吗?

#!/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 _

答案1

shell 性能的关键是尽量减少昂贵的系统调用的数量,特别是fork()exec()

不要在 shell 循环内 使用grep或。sed绝不两者都使用管道;在大多数情况下,它可以简化为sedawk

如果变得复杂,请使用可以解析正则表达式的语言,做循环,比如或者

另一方面,如果您可以使用 shell 提供的简单文本处理功能,则可以从中获得大量性能。

也就是说,at&t ksh93 始终针对速度进行优化,而 bash 和 zsh 则拥有更多功能。

答案2

for shell in $(sed '1d' /etc/shells); do # or use your own list of shells
    echo "$shell -"
    time $shell /path/to/script
done

相关内容