Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created April 24, 2009 11:28
Show Gist options
  • Save miyagawa/101073 to your computer and use it in GitHub Desktop.
Save miyagawa/101073 to your computer and use it in GitHub Desktop.
package HTTP::Engine::Middleware::Profile::NYTProf;
use Any::Moose;
use Devel::NYTProf;
use Time::HiRes;
with 'HTTP::Engine::Middleware::Profile::Role';
has header_name => (
is => 'rw',
default => 'X-NYTProf-Id',
);
# TODO: better should be place in ::Profile base
has profile_on => (
is => 'rw',
default => sub { sub { 1 } },
);
sub start {
my($self, $c, $profile, $req) = @_;
if ($self->profile_on->($c, $req)) {
my $id = Time::HiRes::gettimeofday;
$req->header($self->header_name => $id);
DB::enable_profile("/tmp/nytprof.$id.out");
}
}
sub end {
DB::disable_profile();
}
sub report {
my($self, $c, $profile, $req, $res) = @_;
if (my $id = $req->header($self->header_name)) {
DB::enable_profile("/tmp/nyprof.null.out"); # dummy
DB::disable_profile();
$req->headers->remove_header($self->header_name);
$res->header($self->header_name => $id);
$profile->log("Profiled with ID $id");
system "nytprofhtml", "-f", "/tmp/nytprof.$id.out", "--open";
}
}
sub DESTROY {
DB::finish_profile()
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment