3 # harness-timer-report.pl
5 # - read in the HARNESS_TIMER=1 output of "make test"
6 # - convert the milliseconds to seconds
7 # - compute a couple of derived values
8 # - cpu: the sum of 'self' and 'kids'
9 # - ratio of the wallclock and the cpu
10 # - optionally show header, the sum, or the max of each colum
11 # - sort the rows in various ways
12 # - default ordering by 'cpu' seconds
13 # - optionally scale the column values by either the sum or the max
14 # - optionally display only rows that have rows of at least / at most a limit
16 # The --sort option has a few canned sorting rules. If those are
17 # not to your liking, there is always sort(1).
21 # perl harness-timer-report.pl log
22 # perl harness-timer-report.pl --sort=wall log
23 # perl harness-timer-report.pl --scale=sum log
24 # perl harness-timer-report.pl --scale=sum --min=0.01 log
25 # perl harness-timer-report.pl --show=header,max,sum log
26 # perl harness-timer-report.pl --min=wall=10 log
31 use File::Basename qw[basename];
33 our $ME = basename($0);
40 $ME [--scale=[sum|max]]
41 [--sort=[cpu|wall|ratio|self|kids|test|name]]
42 [--show=header,sum,max]
43 [--min=[[cpu|wall|ratio|self|kids]=value,...]]
44 [--max=[[cpu|wall|ratio|self|kids]=value,...]]
48 The --order includes the original test order as the last column.
56 'scale=s' => \$Opt{scale},
57 'sort=s' => \$Opt{sort},
58 'show=s' => \$Opt{show},
59 'min=s' => \$Opt{min},
60 'max=s' => \$Opt{max},
61 'order' => \$Opt{order},
65 if (defined $Opt{show}) {
66 for my $s (split(/,/, $Opt{show})) {
67 if ($s =~ /^(header|sum|max)$/) {
70 die "$ME: Unexpected --show='$s'\n";
75 if (defined $Opt{min}) {
76 for my $s (split(/,/, $Opt{min})) {
77 if ($s =~ /^(wall|cpu|kids|self|ratio)=(\d+(?:\.\d+)?)$/) {
80 die "$ME: Unexpected --min='$s'\n";
85 if (defined $Opt{max}) {
86 for my $s (split(/,/, $Opt{max})) {
87 if ($s =~ /^(wall|cpu|kids|self|ratio)=(\d+(?:\.\d+)?)$/) {
90 die "$ME: Unexpected --max='$s'\n";
95 use List::Util qw[max];
97 my ($sa, $sb, $sc, $sd, $se);
98 my ($ma, $mb, $mc, $md, $me);
103 # t/re/pat ....................................................... ok 2876 ms 2660 ms 210 ms
104 if (m{(.+)\s+\.+\s+ok\s+(\d+)\s+ms\s+(\d+)\s+ms\s+(\d+)\s+ms$}) {
105 my ($test, $wall, $self, $kids) = ($1, $2, $3, $4);
106 next unless $wall > 0;
107 # Milliseconds to seconds.
111 my $cpu = $self + $kids;
112 my $ratio = $cpu / $wall;
113 push @t, [ $test, $wall, $self, $kids, $cpu, $ratio, $order++ ];
118 $ma = max($wall, $ma // $wall);
119 $mb = max($self, $mb // $self);
120 $mc = max($kids, $mc // $kids);
121 $md = max($cpu, $md // $cpu);
122 $me = max($ratio, $md // $ratio);
126 die "$ME: No input found\n" unless @t;
128 # Compute the sum for the ratio only after the loop.
134 sub { $b->[4] <=> $a->[4] ||
135 $b->[1] <=> $a->[1] ||
136 $a->[0] cmp $b->[0] },
138 sub { $b->[1] <=> $a->[1] ||
139 $b->[4] <=> $a->[4] ||
140 $a->[0] cmp $b->[0] },
142 sub { $b->[5] <=> $a->[5] ||
143 $b->[4] <=> $a->[4] ||
144 $b->[1] <=> $a->[1] ||
145 $a->[0] cmp $b->[0] },
147 sub { $b->[2] <=> $a->[2] ||
148 $b->[3] <=> $a->[3] ||
149 $a->[0] cmp $b->[0] },
151 sub { $b->[3] <=> $a->[3] ||
152 $b->[2] <=> $a->[2] ||
153 $a->[0] cmp $b->[0] },
155 sub { $a->[6] <=> $b->[6] },
157 sub { $a->[0] cmp $b->[0] },
161 $Opt{sort} //= 'cpu';
163 die "$ME: Unexpected --sort='$Opt{sort}'\n"
164 unless defined $SORTER{$Opt{sort}};
166 @t = sort { $SORTER{$Opt{sort}}->() } @t;
168 if (defined $Opt{scale}) {
169 my ($ta, $tb, $tc, $td, $te) =
170 $Opt{scale} eq 'sum' ?
171 ($sa, $sb, $sc, $sd, $se) :
172 $Opt{scale} eq 'max' ?
173 ($ma, $mb, $mc, $md, $me) :
174 die "$ME: Unexpected --scale='$Opt{scale}'";
179 $t->[1] / $ta, $t->[2] / $tb,
180 $t->[3] / $tc, $t->[4] / $td,
181 $t->[5] / $te, $t->[6] ];
187 my @header = qw[TEST WALL SELF KIDS CPU RATIO];
189 push @header, 'ORDER';
191 print join(" ", @header), "\n";
194 print join(" ", "SUM",
195 map { sprintf("%.6f", $_) } $sa, $sb, $sc, $sd, $se),
199 print join(" ", "MAX",
200 map { sprintf("%.6f", $_) } $ma, $mb, $mc, $md, $me),
204 my %N2I = (wall => 1,
210 sub row_is_skippable {
212 if (scalar keys %MIN) {
213 for my $k (grep { exists $MIN{$_} } keys %N2I) {
214 if ($t->[$N2I{$k}] < $MIN{$k}) {
219 if (scalar keys %MAX) {
220 for my $k (grep { exists $MAX{$_} } keys %N2I) {
221 if ($t->[$N2I{$k}] > $MAX{$k}) {
230 next if row_is_skippable($t);
231 my $out = sprintf("%s %.6f %.6f %.6f %.6f %.6f",
232 $t->[0], $t->[1], $t->[2], $t->[3], $t->[4], $t->[5]);