-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime-by-line
executable file
·105 lines (93 loc) · 2.75 KB
/
time-by-line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl -w
#
# Given input, sorts all lines by the time it took them to appear
# Useful for finding slowest lines/progress in a long process
# which produces many lines.
#
# Note: some outputs refer to what's about to run (before it starts)
# while others print summary at end (after they finish), so we need
# two different modes to attribute each inter-line duration:
# 1) attribute the time to the -- starting-line --
# 2) attribute the time to the -- ending-line --
#
# By default: time between two lines will be attributed to the _ending_ line.
# To switch from ending-line timing to starting-line timing:
# pass '-' or '--' as the 1st arg to this program.
#
use strict;
use Time::HiRes qw/gettimeofday/;
#
# -- global vars
#
my %Duration = (); # line -> time since previous line
my $T0;
#
# Whether to assign the time to ending-line or starting-line
# By default, we assume a time-measurement belongs to its ending-line
#
my $StartingLine = 0;
sub mymax($$) { $_[$_[0] < $_[1]] }
sub time_since_last() {
my $t1 = gettimeofday;
my $t_diff = $t1 - $T0;
$T0 = $t1;
$t_diff;
}
sub summarize() {
for my $line (sort {$Duration{$a} <=> $Duration{$b}} keys %Duration) {
my $duration = $Duration{$line};
printf "%12.6f\t%s\n", $duration, $line;
}
}
sub input_line_loop() {
# main loop over input lines
my $prev_line = 'TIME-TILL-FIRST-LINE';
my $t_diff;
while (<STDIN>) {
print;
chomp;
my $line_to_time = $StartingLine ? $prev_line : $_;
$t_diff = time_since_last();
if (exists $Duration{$line_to_time}) {
printf STDERR
"%s: warning: duplicate line (slowest time wins): %s\n",
$0, $line_to_time;
$t_diff = mymax($t_diff, $Duration{$line_to_time});
}
$Duration{$line_to_time} = $t_diff;
$prev_line = $_;
}
if ($StartingLine) {
# when $StartingLine is in effect
# we need to assign time to last line (till exit) as well
$t_diff = time_since_last();
$Duration{$prev_line} = $t_diff;
}
}
sub init() {
# Force $0 to basename($0) for messages/errors
$0 =~ s{.*/}{};
#
# whether to switch to starting-line timing
#
if ($ARGV[0] && $ARGV[0] =~ /^-+$/) {
shift @ARGV;
$StartingLine = 1;
}
if (@ARGV > 0) {
# Dual API:
# If this script is used as a command prefix (like 'time' or
# 'nice', oblige, and read input from combined (STDOUT+STDERR)
# of command postfix.
# print STDERR "Got args: (@ARGV): reading from it...\n";
open(STDIN, "bash -c '2>&1 @ARGV' |");
@ARGV = ();
}
$T0 = gettimeofday;
}
#
# -- main
#
init();
input_line_loop();
summarize();