profile using Devel::NYTProf
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 21:15:53 +0000 (23:15 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 26 Jun 2010 21:15:53 +0000 (23:15 +0200)
lib/MojoFacets/Plugin/NYTProf.pm [new file with mode: 0644]

diff --git a/lib/MojoFacets/Plugin/NYTProf.pm b/lib/MojoFacets/Plugin/NYTProf.pm
new file mode 100644 (file)
index 0000000..37a1bf2
--- /dev/null
@@ -0,0 +1,33 @@
+package MojoFacets::Plugin::NYTProf;
+
+use Devel::NYTProf;
+use Time::HiRes ();
+
+sub register {
+       my ($self, $app) = @_;
+
+       # Start timer
+       $app->plugins->add_hook(
+               before_dispatch => sub {
+                       my ($self, $c) = @_;
+                       my $id = Time::HiRes::gettimeofday();
+                       $c->stash('nytprof.id' => $id);
+                       my $path = "/tmp/nytprof.$id";
+                       DB::enable_profile($path);
+                       warn "profile $path started\n";
+               }
+       );
+
+       # End timer
+       $app->plugins->add_hook(
+               after_dispatch => sub {
+                       my ($self, $c) = @_;
+                       DB::disable_profile();
+                       return unless my $id = $c->stash('nytprof.id');
+                       my $path = "/tmp/nytprof.$id";
+                       warn "profile $path ", -s $profile, " bytes\n";
+               }
+       );
+}
+
+1;