Skip to content

Instantly share code, notes, and snippets.

@yappo
Forked from miyagawa/NYTProf.pm
Created May 1, 2009 07:22
Show Gist options
  • Save yappo/104919 to your computer and use it in GitHub Desktop.
Save yappo/104919 to your computer and use it in GitHub Desktop.
package HTTP::Engine::Middleware::Profile::NYTProf;
use Any::Moose;
use Time::HiRes;
with 'HTTP::Engine::Middleware::Profile::Role';
BEGIN {
# referred to L<Devel::NYTProf::Apache>
if (!$ENV{NYTPROF}) {
$ENV{NYTPROF} = "file=/tmp/nytprof.$$.out";
}
require Devel::NYTProf;
}
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();
}
}
sub end {
DB::_finish();
}
sub report {
my($self, $c, $profile, $req, $res) = @_;
if (my $id = $req->header($self->header_name)) {
DB::enable_profile(); # 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.$$.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