如何监视stdout的每一行是Bash中最后一条输出行用于基准测试的时间? [英] How to monitor for how much time each line of stdout was the last output line in Bash for benchmarking?

查看:67
本文介绍了如何监视stdout的每一行是Bash中最后一条输出行用于基准测试的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设我有脚本:

(echo a; sleep 1; echo b; sleep 3; echo c; sleep 2)

输出:

a
b
c

在运行该脚本时(可能通过管道),我想获得类似的东西:

1.00 a
3.00 b
2.00 c

因为a行是stdout的最后一行,持续了1.00秒,然后才被b替换为最后一行.

我不想修改正在运行的程序,在我的实际用例中,stdout将由我不想修改的复杂可执行文件生成,例如QEMU正在引导Linux内核.

这将使我能够对正在运行的程序进行快速而肮脏的剖析,以确定哪个段花费了最长的时间.

解决方案

如果您对秒的分辨率感到满意,则可以使用以下一种格式:

t=$SECONDS; (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2) | while read line; do echo $((SECONDS - t)) $line; t=$SECONDS; done

如果您可以安装node.js,那么这正是 gnomon 的作用:

For example, suppose I have the script:

(echo a; sleep 1; echo b; sleep 3; echo c; sleep 2)

which outputs:

a
b
c

When running that script, possibly through a pipe, I would like to obtain something like:

1.00 a
3.00 b
2.00 c

because the line a was the last line of stdout for 1.00 seconds, before it was replaced by b as the last line.

I don't want to modify the program being run, and in my actual use case the stdout will be generated by a complex executable which I don't want to modify, e.g. QEMU booting the Linux kernel.

This would allow me to do a quick and dirty profiling of the program being run, to determine what segment is taking the longest.

解决方案

If you're happy with a resolution of seconds, you can use this one-liner:

t=$SECONDS; (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2) | while read line; do echo $((SECONDS - t)) $line; t=$SECONDS; done

If you are OK with installing node.js, this is exactly what gnomon does:

这篇关于如何监视stdout的每一行是Bash中最后一条输出行用于基准测试的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆