对 linux Bash 脚本进行基准测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8030768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Benchmark a linux Bash script
提问by mondo
Is there a way to benchmark a bash script's performance? the script downloads a remote file, and then makes calls to multiple commandline programs to manipulate. I would like to know (or as much as possible):
有没有办法对 bash 脚本的性能进行基准测试?该脚本下载一个远程文件,然后调用多个命令行程序进行操作。我想知道(或尽可能多地):
- Total time
- Time spent downloading
- Time spent on each command called
- -=[ I think these could be wrapped in "time" calls right? ]=-
- Average download speed
- uses wget
- Total Memory used
- Total CPU usage
- CPU usage per command called
- 总时间
- 下载时间
- 每个调用的命令花费的时间
- -=[ 我认为这些可以包含在“时间”调用中,对吗?]=-
- 平均下载速度
- 使用 wget
- 使用的总内存
- 总 CPU 使用率
- 每个调用的命令的 CPU 使用率
I'm able to make edits to the bash script to insert any benchmark commands needed at specific points (ie, between app calls). Not sure if some "top" ninja-ry could solve this or not. Not able to find anything useful (at least to limited understanding) in man file.
我能够对 bash 脚本进行编辑,以插入在特定点(即,在应用程序调用之间)所需的任何基准命令。不确定一些“顶级”忍者是否可以解决这个问题。无法在 man 文件中找到任何有用的东西(至少是有限的理解)。
Will be running the benchmarks on OSX Terminal as well as Ubuntu (if either matter).
将在 OSX 终端和 Ubuntu(如果两者都重要)上运行基准测试。
回答by Farhan
strace -o trace -c -Ttt ./scrip
-cis to trace the time spent by cpu on specific call.-Tttwill tell you time in microseconds at time of each system call running.-owill save output in file "trace".
-c是跟踪cpu在特定调用上花费的时间。-Ttt将在每个系统调用运行时告诉您时间(以微秒为单位)。-o将输出保存在文件“trace”中。
回答by Ahmed Masud
You should be able to achieve this a number of ways. One way is to use timebuilt-in function for each command of interest and capture the results. You may have to be careful about any pipes and redirects;
您应该能够通过多种方式实现这一目标。一种方法是time对每个感兴趣的命令使用内置函数并捕获结果。您可能必须小心任何管道和重定向;
You may also consider trapping SIGCHLD, DEBUG, RETURN, ERR and EXIT signals and putting timing information in there, but you may not get some results.
您还可以考虑捕获 SIGCHLD、DEBUG、RETURN、ERR 和 EXIT 信号并将计时信息放入其中,但您可能不会得到一些结果。
This concept of CPU usage of each command won't give you any thing useful, all commands use 100% of cpu. Memory usage is something you can pull out but you should look at
每个命令的 CPU 使用率概念不会给您任何有用的东西,所有命令都使用 100% 的 CPU。内存使用情况是您可以提取的,但您应该查看
If you want to get deep process statistics then you would want to use strace... See strace(1) man page for details. I doubt that -Ttt as it is suggest elsewhere is useful all that tells you are system call times and you want other process trace info.
如果您想获得深入的进程统计信息,那么您将需要使用 strace... 有关详细信息,请参阅 strace(1) 手册页。我怀疑 -Ttt 正如其他地方所建议的那样是否有用,因为它告诉您是系统调用时间并且您需要其他进程跟踪信息。
You may also want to see ltrace and dstat tools.
您可能还想查看 ltrace 和 dstat 工具。
A similar question is answered here Linux benchmarking tools
这里回答了一个类似的问题Linux 基准测试工具

