Author: gisle.aas
Date: Thu Oct 23 08:23:58 2008
New Revision: 546

Added:
    trunk/t/30.util.t
Modified:
    trunk/Changes
    trunk/MANIFEST
    trunk/bin/nytprofhtml
    trunk/lib/Devel/NYTProf/Util.pm

Log:
Produce more readable timestamps in the code prologs.

Modified: trunk/Changes
==============================================================================
--- trunk/Changes       (original)
+++ trunk/Changes       Thu Oct 23 08:23:58 2008
@@ -41,6 +41,8 @@
    The html global subroutine index pages no longer list subs that
      were never called.

+  More readable formatting of timestamps inlined in the code.
+
    Exclusive and Inclusive time column positions have been switched
      to be consistent with how the times are presented elsewhere.


Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST      (original)
+++ trunk/MANIFEST      Thu Oct 23 08:23:58 2008
@@ -26,6 +26,7 @@
  README
  t/00.load.t
  t/20.runtests.t
+t/30.util.t
  t/50.errno.t
  t/90.pod.t
  t/80.version.t

Modified: trunk/bin/nytprofhtml
==============================================================================
--- trunk/bin/nytprofhtml       (original)
+++ trunk/bin/nytprofhtml       Thu Oct 23 08:23:58 2008
@@ -20,7 +20,7 @@
  use Devel::NYTProf::Reader;
  use Devel::NYTProf::Core;
  use Devel::NYTProf::Util qw(
-    fmt_float fmt_incl_excl_time
+    fmt_float fmt_time fmt_incl_excl_time
      calculate_median_absolute_deviation
      get_abs_paths_alternation_regex
      html_safe_filename
@@ -277,13 +277,13 @@
                  }
                  my $total_calls = sum(my @caller_calls = map { $_->[2] }  
@callers);
                  my $max_calls = max(@caller_calls);
-                my $avg_per_call = fmt_float($sub_info->incl_time /  
$total_calls);
+                my $avg_per_call = fmt_time($sub_info->incl_time /  
$total_calls);

                  push @prologue, sprintf "# spent %s within %s which was  
called%s",
                      fmt_incl_excl_time($sub_info->incl_time,  
$sub_info->excl_time),
                      $sub_info->subname, ($total_calls <= 1)
                      ? ""
-                    : " $total_calls times, avg ${avg_per_call}s/call:";
+                    : " $total_calls times, avg ${avg_per_call}/call:";

                  # order by most frequent caller first
                  @callers = sort { $b->[2] <=> $a->[2] } @callers;
@@ -295,9 +295,9 @@
                      my $avg_time =
                          ($count <= 1)
                          ? ""
-                        : sprintf ", avg %ss/call", fmt_float($incl_time /  
$count);
-                    my $times = sprintf " (%s+%ss)", fmt_float($excl_time),
-                        fmt_float($incl_time - $excl_time);
+                        : sprintf ", avg %s/call", fmt_time($incl_time /  
$count);
+                    my $times = sprintf " (%s+%s)", fmt_time($excl_time),
+                        fmt_time($incl_time - $excl_time);

                      my $filename = $profile->fid_filename($fid);
                      my $href =  
$reporter->get_file_stats()->{$filename}{html_safe} || "unknown";
@@ -327,11 +327,11 @@

                  $epilogue = join "\n", map {
                      my ($count, $incl_time, $reci_time, $rec_depth) =  
(@{$calls->{$_}})[0,1,5,6];
-                    my $html = sprintf qq{%s# spent %ss making %*d call%s  
to }, $ws,
-                        fmt_float($incl_time+$reci_time),  
length($max_calls_to),
+                    my $html = sprintf qq{%s# spent %s making %*d call%s  
to }, $ws,
+                        fmt_time($incl_time+$reci_time),  
length($max_calls_to),
                        $count, $count == 1 ? "" : "s";
                      $html .= sprintf qq{<a %s>%s</a>},  
$reporter->href_for_sub($_), $_;
-                    $html .= sprintf qq{, avg %ss/call},  
fmt_float($incl_time / $count)
+                    $html .= sprintf qq{, avg %s/call},  
fmt_time($incl_time / $count)
                          if $count > 1;
                      $html .= sprintf qq{, max recursion depth %d},  
$rec_depth
                          if $rec_depth;

Modified: trunk/lib/Devel/NYTProf/Util.pm
==============================================================================
--- trunk/lib/Devel/NYTProf/Util.pm     (original)
+++ trunk/lib/Devel/NYTProf/Util.pm     Thu Oct 23 08:23:58 2008
@@ -44,7 +44,7 @@

  our @EXPORT_OK = qw(
      fmt_float
-    fmt_incl_excl_time
+    fmt_time fmt_incl_excl_time
      strip_prefix_from_paths
      calculate_median_absolute_deviation
      get_alternation_regex
@@ -147,13 +147,21 @@
      return $val;
  }

+sub fmt_time {
+    my $sec = shift;
+    return 0 unless $sec;
+    return sprintf "%.0fns", $sec * 1e9                              if  
$sec < 1e-6;
+    return sprintf "%.0f&micro;s", $sec * 1e6                        if  
$sec < 1e-3;
+    return sprintf "%.*fms", 3 - length(int($sec * 1e3)), $sec * 1e3 if  
$sec < 1;
+    return sprintf "%.*fs",  3 - length(int($sec)),       $sec       if  
$sec < 100;
+    return sprintf "%.0fs", $sec;
+}

  sub fmt_incl_excl_time {
      my ($incl, $excl) = @_;
      my $diff = $incl - $excl;
-    return fmt_float($incl) . "s" unless $diff;
-    return sprintf "%ss (%s+%s)", fmt_float($incl), fmt_float($excl),
-        fmt_float($diff);
+    return fmt_time($incl) unless $diff;
+    return sprintf "%s (%s+%s)", fmt_time($incl), fmt_time($excl),  
fmt_time($diff);
  }



Added: trunk/t/30.util.t
==============================================================================
--- (empty file)
+++ trunk/t/30.util.t   Thu Oct 23 08:23:58 2008
@@ -0,0 +1,22 @@
+use Test::More tests => 15;
+
+use Devel::NYTProf::Util qw(fmt_time);
+
+my $us = "&micro;s";
+
+is(fmt_time(0), "0");
+
+is(fmt_time(1.1253e-10), "0ns");
+is(fmt_time(1.1253e-9), "1ns");
+is(fmt_time(1.1253e-8), "11ns");
+is(fmt_time(1.1253e-7), "113ns");
+is(fmt_time(1.1253e-6), "1$us");
+is(fmt_time(1.1253e-5), "11$us");
+is(fmt_time(1.1253e-4), "113$us");
+is(fmt_time(1.1253e-3), "1.13ms");
+is(fmt_time(1.1253e-2), "11.3ms");
+is(fmt_time(1.1253e-1), "113ms");
+is(fmt_time(1.1253e-0), "1.13s");
+is(fmt_time(1.1253e+1), "11.3s");
+is(fmt_time(1.1253e+2), "113s");
+is(fmt_time(1.1253e+3), "1125s");

--~--~---------~--~----~------------~-------~--~----~
You've received this message because you are subscribed to
the Devel::NYTProf Development User group.

Group hosted at:  http://groups.google.com/group/develnytprof-dev
Project hosted at:  http://perl-devel-nytprof.googlecode.com
CPAN distribution:  http://search.cpan.org/dist/Devel-NYTProf

To post, email:  [email protected]
To unsubscribe, email:  [EMAIL PROTECTED]
-~----------~----~----~----~------~----~------~--~---

Reply via email to