Subject: | [patch] Feature added: $t->report(collapse => 1); |
Per this thread:
http://www.mail-archive.com/module-authors@perl.org/msg04318.html
The Devel::Timer author seems to have disappeared. As suggested on the
module-authors mailing list, I'm submitting my patch as a "bug report"
here. If no one can find JMOORE I'm volunteering to take over
maintenance of Devel::Timer.
Cheers,
j
Omaha Perl Mongers
$ diff -ruN Timer_orig.pm Timer.pm
--- Timer_orig.pm 2006-05-10 15:43:03.000000000 -0500
+++ Timer.pm 2006-05-11 11:10:36.000000000 -0500
@@ -76,7 +76,7 @@
sub report
{
- my $self = shift;
+ my ($self, %args) = @_;
## calculate total time (start time vs last time)
@@ -84,9 +84,31 @@
$self->print("\n");
$self->print(ref($self) . " Report -- Total time: " .
sprintf("%.4f", $total_time) . " secs");
+
+ if ($args{collapse})
+ {
+ $self->calculate_collapsed;
+
+ $self->print("Count Time Percent");
+ $self->print("----------------------------------------------");
+
+ my $c = $self->{collapsed};
+ my $sort_by = $args{sort_by} || "time";
+ my @labels = sort { $c->{$b}->{$sort_by} <=>
$c->{$a}->{$sort_by} } keys %$c;
+ foreach (@labels)
+ {
+ my $count = $c->{$_}->{count};
+ my $time = $c->{$_}->{time};
+ my $msg = sprintf("%8s %.4f %5.2f%% %s",
+ ($count, $time, (($time/$total_time)*100), $_));
+ $self->print($msg);
+ }
+ return 1;
+ }
+
$self->print("Interval Time Percent");
$self->print("----------------------------------------------");
-
+
## sort interval structure based on value
@{$self->{intervals}} = sort { $b->{value} <=> $a->{value} }
@{$self->{intervals}};
@@ -111,6 +133,23 @@
}
}
+
+sub calculate_collapsed
+{
+ my ($self) = @_;
+
+ my %collapsed;
+ foreach my $i (0 .. $self->{count} - 2)
+ {
+ my $label = $self->{label}->{$i} . ' -> ' . $self->{label}->{$i
+ 1};
+ my $time = Time::HiRes::tv_interval($self->{times}->[$i],
$self->{times}->[$i + 1]);
+ $collapsed{$label}{time} += $time;
+ $collapsed{$label}{count}++;
+ }
+ $self->{collapsed} = \%collapsed;
+}
+
+
## output methods
## note: if you want to send output to somewhere other than stderr,
## you can override the print() method below. The initialize()