use PROFILE to specify minimum duration
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 21:38:38 +0000 (23:38 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 21:38:38 +0000 (23:38 +0200)
This allows us to ignore short timed requests, and focus just on
long running ones

lib/MojoFacets/Plugin/NYTProf.pm

index 37a1bf2..7604a19 100644 (file)
@@ -10,11 +10,11 @@ sub register {
        $app->plugins->add_hook(
                before_dispatch => sub {
                        my ($self, $c) = @_;
+                       return unless $ENV{PROFILE};
                        my $id = Time::HiRes::gettimeofday();
                        $c->stash('nytprof.id' => $id);
                        my $path = "/tmp/nytprof.$id";
                        DB::enable_profile($path);
-                       warn "profile $path started\n";
                }
        );
 
@@ -22,10 +22,17 @@ sub register {
        $app->plugins->add_hook(
                after_dispatch => sub {
                        my ($self, $c) = @_;
+                       my $p = $ENV{PROFILE} || return;
                        DB::disable_profile();
                        return unless my $id = $c->stash('nytprof.id');
-                       my $path = "/tmp/nytprof.$id";
-                       warn "profile $path ", -s $profile, " bytes\n";
+                       my $duration = Time::HiRes::gettimeofday() - $id;
+                       if ( $duration > $p ) {
+                               my $path = "/tmp/nytprof.$id";
+                               warn "profile $path $duration ", -s $path, " bytes\n";
+                       } else {
+                               warn "profile $path $duration < $p unlink\n";
+                               unlink $path;
+                       }
                }
        );
 }