Skip to content

Commit

Permalink
fixed example/app_options.psgi: added PID and enable profile depends …
Browse files Browse the repository at this point in the history
…on PID

check enable_profile before loading Devel::NYTProf(because nytprof.out is generated same time as loading Devel::NYTProf)
  • Loading branch information
bayashi committed Feb 10, 2013
1 parent 423d684 commit fbd0043
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/app_options.psgi
Expand Up @@ -2,13 +2,13 @@ use Plack::Builder;

my $app = sub {
my $env = shift;
return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World" ] ];
return [ '200', [ 'Content-Type' => 'text/plain' ], [ "Hello World $$" ] ];
};

builder {
# you should execute 'mkdir /tmp/profile' before invoking this PSGI app;
enable_if { 1 } 'Profiler::NYTProf',
enable_profile => sub { 1 },
enable_profile => sub { $$ % 2 == 0 },
env_nytprof => 'start=no:addpid=0:file=/tmp/profile/nytprof.out',
profiling_result_dir => sub { '/tmp/profile' },
enable_reporting => 1;
Expand Down
10 changes: 7 additions & 3 deletions lib/Plack/Middleware/Profiler/NYTProf.pm
Expand Up @@ -97,7 +97,7 @@ my %PROFILER_SETUPED;
sub call {
my ( $self, $env ) = @_;

$self->_setup_profiler unless $PROFILER_SETUPED{$$};
$self->_setup_profiler($env) unless $PROFILER_SETUPED{$$};
$self->start_profiling_if_needed($env);

my $res = $self->app->($env);
Expand Down Expand Up @@ -144,15 +144,19 @@ sub stop_profiling_and_report_if_needed {
}

sub _setup_profiler {
my $self = shift;
my ( $self, $env ) = @_;

$PROFILER_SETUPED{$$} = 1;

my $is_profiler_enabled = $self->enable_profile->($env);
return unless $is_profiler_enabled;

$ENV{NYTPROF} = $self->env_nytprof || 'start=no';

# NYTPROF environment variable is set in Devel::NYTProf::Core
# so, we load Devel::NYTProf here.
require Devel::NYTProf;
DB::disable_profile();
$PROFILER_SETUPED{$$} = 1;
}

sub start_profiling {
Expand Down

0 comments on commit fbd0043

Please sign in to comment.