3 # harness-timer-report.pl
5 # - read in the time-annotated outputs of
6 # "env HARNESS_TIMER=1 make test" or
8 # - convert the milliseconds to seconds
9 # - compute a couple of derived values
10 # - cpu: the sum of 'self' and 'kids'
11 # - ratio of the wallclock and the cpu
12 # - optionally show header, the sum, or the max of each colum
13 # - sort the rows in various ways
14 # - default ordering by 'cpu' seconds
15 # - optionally scale the column values by either the sum or the max
16 # - optionally display only rows that have rows of at least / at most a limit
18 # The --sort option has a few canned sorting rules. If those are
19 # not to your liking, there is always sort(1).
23 # perl harness-timer-report.pl log
24 # perl harness-timer-report.pl --sort=wall log
25 # perl harness-timer-report.pl --scale=sum log
26 # perl harness-timer-report.pl --scale=sum --min=0.01 log
27 # perl harness-timer-report.pl --show=header,max,sum log
28 # perl harness-timer-report.pl --min=wall=10 log
33 use File::Basename qw[basename];
35 our $ME = basename($0);
42 $ME [--scale=[sum|max]]
43 [--sort=[cpu|wall|ratio|self|kids|test|name]]
44 [--show=header,sum,max]
45 [--min=[[cpu|wall|ratio|self|kids]=value,...]]
46 [--max=[[cpu|wall|ratio|self|kids]=value,...]]
51 The --order includes the original test order as the last column.
52 The logfile default is STDIN.
60 'scale=s' => \$Opt{scale},
61 'sort=s' => \$Opt{sort},
62 'show=s' => \$Opt{show},
63 'min=s' => \$Opt{min},
64 'max=s' => \$Opt{max},
65 'order' => \$Opt{order},
66 'help|usage' => \$Opt{help},
68 usage() if $Opt{help};
71 if (defined $Opt{show}) {
72 for my $s (split(/,/, $Opt{show})) {
73 if ($s =~ /^(header|sum|max)$/) {
76 die "$ME: Unexpected --show='$s'\n";
81 if (defined $Opt{min}) {
82 for my $s (split(/,/, $Opt{min})) {
83 if ($s =~ /^(wall|cpu|kids|self|ratio)=(\d+(?:\.\d+)?)$/) {
86 die "$ME: Unexpected --min='$s'\n";
91 if (defined $Opt{max}) {
92 for my $s (split(/,/, $Opt{max})) {
93 if ($s =~ /^(wall|cpu|kids|self|ratio)=(\d+(?:\.\d+)?)$/) {
96 die "$ME: Unexpected --max='$s'\n";
101 use List::Util qw[max];
103 my ($sa, $sb, $sc, $sd, $se);
104 my ($ma, $mb, $mc, $md, $me);
110 open($logfh, "<", $logfn) or die "$ME: Failed to open logfn: $logfn\n";
111 } elsif (@ARGV == 0) {
115 die "$ME: Unexpected logfile arguments: @ARGV\n";
122 my ($test, $wall, $self, $kids);
123 # Output of "env HARNESS_TIMER=1 make test":
124 # t/re/pat ....................................................... ok 2876 ms 2660 ms 210 ms
125 if (m{^#\s+(\S+)\s+\.+\s+ok\s+(\d+)\s+ms\s+(\d+)\s+ms\s+(\d+)\s+ms$}) {
126 ($test, $wall, $self, $kids) = ($1, $2, $3, $4);
128 # Output of "env HARNESS_TIMER=1 make test_harness":
129 # [08:26:11] base/cond.t ........................................................ ok 2 ms ( 0.00 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.00 CPU)
130 if (m{^\[.+?\]+\s+(\S+)\s+\.+\s+ok\s+(\d+)\s+ms\s+\(\s*(\d+\.\d+)\s+usr\s+\s+(\d+\.\d+)\s+sys\s+\+\s+(\d+\.\d+)\s+cusr\s+(\d+\.\d+)\s+csys\s+=\s+(\d+\.\d+)\s+CPU\)}) {
135 $test =~ s{^\.\./}{}; # "../lib/foo" -> "../lib/foo"
137 next unless defined $test && defined $wall && $wall > 0;
138 # Milliseconds to seconds.
142 my $cpu = $self + $kids;
143 my $ratio = $cpu / $wall;
144 push @t, [ $test, $wall, $self, $kids, $cpu, $ratio, $order++ ];
149 $ma = max($wall, $ma // $wall);
150 $mb = max($self, $mb // $self);
151 $mc = max($kids, $mc // $kids);
152 $md = max($cpu, $md // $cpu);
153 $me = max($ratio, $md // $ratio);
156 die "$ME: No input detected in '$logfn'\n" unless @t;
158 # Compute the sum for the ratio only after the loop.
164 sub { $b->[4] <=> $a->[4] ||
165 $b->[1] <=> $a->[1] ||
166 $a->[0] cmp $b->[0] },
168 sub { $b->[1] <=> $a->[1] ||
169 $b->[4] <=> $a->[4] ||
170 $a->[0] cmp $b->[0] },
172 sub { $b->[5] <=> $a->[5] ||
173 $b->[4] <=> $a->[4] ||
174 $b->[1] <=> $a->[1] ||
175 $a->[0] cmp $b->[0] },
177 sub { $b->[2] <=> $a->[2] ||
178 $b->[3] <=> $a->[3] ||
179 $a->[0] cmp $b->[0] },
181 sub { $b->[3] <=> $a->[3] ||
182 $b->[2] <=> $a->[2] ||
183 $a->[0] cmp $b->[0] },
185 sub { $a->[6] <=> $b->[6] },
187 sub { $a->[0] cmp $b->[0] },
191 $Opt{sort} //= 'cpu';
193 die "$ME: Unexpected --sort='$Opt{sort}'\n"
194 unless defined $SORTER{$Opt{sort}};
196 @t = sort { $SORTER{$Opt{sort}}->() } @t;
198 if (defined $Opt{scale}) {
199 my ($ta, $tb, $tc, $td, $te) =
200 $Opt{scale} eq 'sum' ?
201 ($sa, $sb, $sc, $sd, $se) :
202 $Opt{scale} eq 'max' ?
203 ($ma, $mb, $mc, $md, $me) :
204 die "$ME: Unexpected --scale='$Opt{scale}'";
209 $t->[1] / $ta, $t->[2] / $tb,
210 $t->[3] / $tc, $t->[4] / $td,
211 $t->[5] / $te, $t->[6] ];
217 my @header = qw[TEST WALL SELF KIDS CPU RATIO];
219 push @header, 'ORDER';
221 print join(" ", @header), "\n";
224 print join(" ", "SUM",
225 map { sprintf("%.6f", $_) } $sa, $sb, $sc, $sd, $se),
229 print join(" ", "MAX",
230 map { sprintf("%.6f", $_) } $ma, $mb, $mc, $md, $me),
234 my %N2I = (wall => 1,
240 sub row_is_skippable {
242 if (scalar keys %MIN) {
243 for my $k (grep { exists $MIN{$_} } keys %N2I) {
244 if ($t->[$N2I{$k}] < $MIN{$k}) {
249 if (scalar keys %MAX) {
250 for my $k (grep { exists $MAX{$_} } keys %N2I) {
251 if ($t->[$N2I{$k}] > $MAX{$k}) {
260 next if row_is_skippable($t);
261 my $out = sprintf("%s %.6f %.6f %.6f %.6f %.6f",
262 $t->[0], $t->[1], $t->[2], $t->[3], $t->[4], $t->[5]);