如何监控每行stdout多少时间是Bash中用于基准测试的最后一个输出行?

时间:2018-04-12 12:58:46

标签: bash

例如,假设我有脚本:

group by

输出:

SELECT DISTINCT  ivp.VendorStockNumber, i.StockNumber, i.StockDescription, 
                 ivp.PackDescription, ib.BrandName, 
                 ich.NewPackCost as Cost, 
                 ich.ModifiedDate as DateChanged,
                 row_number() over (partition by ivp.VendorStockNumber order by DateChanged desc) as rn 
FROM InvVendors iv, InvVendorProducts ivp, InvBrands ib, InvItems i, 
InvCostPackSizeHistory ich, InvSiteTypesItems ist, 
InvSiteTypesItemsVendorProducts itp
WHERE i.InvItemID = ivp.InvItemID
AND iv.InvVendorID = ivp.InvVendorID 
AND ib.InvBrandID = ivp.InvBrandID
AND ist.InvSiteTypesItemID = ich.InvSiteTypesItemID
AND ist.InvSiteTypesItemID = itp.InvSiteTypesItemID
AND ivp.InvVendorProductID = itp.InvVendorProductID
AND i.InvItemID = ist.InvItemID
AND iv.VendorNumber = '5'
AND ivp.VendorStockNumber <> ''
AND ich.NewPackCost <> ich.OldPackCost
AND itp.Preferred = '1'
ORDER BY i.StockNumber ASC, DateChanged DESC

当运行该脚本时,可能通过管道,我希望得到类似的东西:

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

因为行a b c 是stdout的最后一行,持续1.00秒,然后被1.00 a 3.00 b 2.00 c 替换为最后一行。

我不想修改正在运行的程序,在我的实际用例中,stdout将由一个我不想修改的复杂可执行文件生成,例如: QEMU启动Linux内核。

这将允许我对正在运行的程序进行快速而脏的分析,以确定哪个分段花费的时间最长。

2 个答案:

答案 0 :(得分:3)

如果你对秒的分辨率感到满意,你可以使用这个单行:

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的作用:

enter image description here

答案 1 :(得分:1)

moreutils ts

我知道之前有人写过这个!

$ sudo apt-get install moreutils
$ (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2; echo d; sleep 1) | ts -i
00:00:00 a
00:00:01 b
00:00:03 c
00:00:02 d
$ (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2; echo d; sleep 1) | ts -i '%.s'
0.000010 a
0.983308 b
3.001129 c
2.001120 d
$ (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2; echo d; sleep 1) | ts -s
00:00:00 a
00:00:01 b
00:00:04 c
00:00:06 d   
$ (echo a; sleep 1; echo b; sleep 3; echo c; sleep 2; echo d; sleep 1) | ts
Apr 13 03:10:44 a
Apr 13 03:10:45 b
Apr 13 03:10:48 c
Apr 13 03:10:50 d| ts -s                                       

如果您使用Google代替相关的&#34;将时间戳添加到stdout&#34;有很多结果:

Bash while read

这以毫秒为单位:

stdouttime() (
  tp="$(date +%s%N)"
  while read line; do
    tc="$(date +%s%N)"
    echo $(((tc - tp) / 1000000))
    echo "$line"
    tp=$tc
  done
  tc="$(date +%s%N)";
  echo $(((tc - tp) / 1000000))
)
(echo a; sleep 1; echo b; sleep 3; echo c; sleep 2) | stdouttime

示例输出:

2
a
1000
b
3002
c
2002

基于:https://serverfault.com/questions/151109/how-do-i-get-current-unix-time-in-milliseconds-using-bash