作为调试过程中的重要一步,仔细查看时间和进展是必不可少的。经常使用
示例(来自如何分析缓慢启动的bash shell脚本?):
然后有两个文件:
我可以通过以下方式“重播”脚本:
使用
将回放速度减慢10倍。
将输出总执行时间或如果有太多行:
检查整体脚本输出大小。 (使用sed删除日志的第一行和最后一行,这些行包含:Script started on Wed Sep 25 14:40:20 2019 和 Script done on Wed Sep 25 14:40:23 2019。)
然后,在某个时间计算日志大小(指针)。
在定时文件中的第12行(
或者,如果我正在寻找时间已流逝的某个字符串:
我的请求:
寻找更轻量级的东西...
这可能是一些小脚本片段,使用Python来现代化我的Perl代码。
甚至可以是动态的!我可以想象一个使用NCurses的工具,带有进度条、显示和键盘控制,就像一种视频查看器: 开始,停止,下一步,...
script
和 scriptreplay
,我想知道是否存在用于操作结果文件的工具。示例(来自如何分析缓慢启动的bash shell脚本?):
script -t script.log 2>script.tim -c 'bash -x -c "
for ((i=3;i--;));do sleep .1;done
for ((i=2;i--;)) ;do
tar -cf /tmp/test.tar -C / bin
gzip /tmp/test.tar
rm /tmp/test.tar.gz
done
"'
然后有两个文件:
-rw-r--r-- 1 user user 472 Sep 25 10:44 script.log
-rw-r--r-- 1 user user 213 Sep 25 10:44 script.tim
我可以通过以下方式“重播”脚本:
scriptreplay --timing script.tim --typescript script.log 10
使用
10
作为 执行时间除数,使重放速度加快 10 倍,或者scriptreplay --timing script.tim --typescript script.log .1
将回放速度减慢10倍。
我想知道是否有这样的工具:
好的,从那里开始:
cut -d \ -f1 <script.tim | xargs | tr \ + | bc
3.616809
将输出总执行时间或如果有太多行:
cut -d \ -f1 <script.tim | xargs | tr \ + | bc | xargs | tr \ + | bc
3.616809
并且
cut -d \ -f2 <script.tim | xargs | tr \ + | bc
366
sed '1d;$d' script.log |wc -c
367
检查整体脚本输出大小。 (使用sed删除日志的第一行和最后一行,这些行包含:Script started on Wed Sep 25 14:40:20 2019 和 Script done on Wed Sep 25 14:40:23 2019。)
然后,在某个时间计算日志大小(指针)。
perl -e 'my ($l,$t,$p)=(0,0,0); # line totTime pos
open FH,"<".$ARGV[0] || die;
while (<FH>) {
my ($d,$n)=split" "; # duration numBytes
$l++;
$t+=$d;
$p+=$n;
$t>=${ARGV[1]} && do {
print $l." ".$p."\n";
exit 0;
};
};' script.tim 1.2
12 216
在定时文件中的第12行(
head -n 12
),以及在 TypeScript 文件中的第216个字节位置(head -c 216
)。或者,如果我正在寻找时间已流逝的某个字符串:
grep -ob 'tar.*test' script.log
217:tar -cf /tmp/test
320:tar -cf /tmp/test
perl -e 'my ($l,$t,$p)=(0,0,0);open FH,"<".$ARGV[0] || die;while (<FH>) {
my ($d,$n)=split" ";$l++;$t+=$d;$p+=$n;$p>=${ARGV[1]} && do {
print $l." ".$p."\n";exit 0;};};' script.tim 217
17 228
head -n 17 script.tim | cut -d \ -f1 | xargs | tr \ + | bc
1.091276
我的请求:
寻找更轻量级的东西...
这可能是一些小脚本片段,使用Python来现代化我的Perl代码。
甚至可以是动态的!我可以想象一个使用NCurses的工具,带有进度条、显示和键盘控制,就像一种视频查看器: 开始,停止,下一步,...
script
和scriptreplay
,计时文件是以微秒为单位的。使用[tag:bash]。变量$EPOCHREALTIME
显示的时间是以纳秒为单位的... 是的,我经常使用time for i in {1..100000};do someCommandToTest;done
之类的语句... - undefinedls -a
的输出中看到了时间,不知怎么把它误认为你正在测量的时间。 - undefined