profile using Devel::NYTProf
[MojoFacets.git] / lib / MojoFacets / Plugin / NYTProf.pm
1 package MojoFacets::Plugin::NYTProf;
2
3 use Devel::NYTProf;
4 use Time::HiRes ();
5
6 sub register {
7         my ($self, $app) = @_;
8
9         # Start timer
10         $app->plugins->add_hook(
11                 before_dispatch => sub {
12                         my ($self, $c) = @_;
13                         my $id = Time::HiRes::gettimeofday();
14                         $c->stash('nytprof.id' => $id);
15                         my $path = "/tmp/nytprof.$id";
16                         DB::enable_profile($path);
17                         warn "profile $path started\n";
18                 }
19         );
20
21         # End timer
22         $app->plugins->add_hook(
23                 after_dispatch => sub {
24                         my ($self, $c) = @_;
25                         DB::disable_profile();
26                         return unless my $id = $c->stash('nytprof.id');
27                         my $path = "/tmp/nytprof.$id";
28                         warn "profile $path ", -s $profile, " bytes\n";
29                 }
30         );
31 }
32
33 1;