blib/lib/Plack/Middleware/Profiler/NYTProf.pm

filter: Statementstmt 86.3% 114 / 132 Branchbran 63.0% 29 / 46 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 88.1% 37 / 42 Podpod 18.1% 2 / 11 totaltotal 75.2% 188 / 250 CC 28 SCAR 10.6
CC
28
Cov
78%
report
12.0
_setup_profiler
6.1
__ANON__
6.0
CRAP 2.9
↑ Summary
1 package Plack::Middleware::Profiler::NYTProf;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 our $VERSION = '0.17';
6  
7 5 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 enable_profile
9 enable_reporting
10 env_nytprof
11 generate_profile_id
12 profiling_result_dir
13 report_dir
14 nytprofhtml_path
15 profiling_result_file_name
16 nullfile_name
17 before_profile
18 after_profile
19 5 );
20  
21 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use constant PROFILE_ID => 'psgix.profiler.nytprof.reqid';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use constant PROFILER_ENABLED => 'psgix.profiler.nytprof.enabled';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 my %PROFILER_SETUPED;
29  
30 5 my $NYTPROF_LOADED = 0;
31  
32 # Devel::NYTProf requires to be loaded in a compile phase.
33 # So you should call this method in the BEGIN of your app.psgi with the NYTPROF environment variable.
34 1 ▶sub preload {
Subroutine preload: called CC=2 SCAR=7.4
Pod undocumented
35 1 ▶ $ENV{NYTPROF} ||= _default_env();
Condition33%1/3
$ENV{"NYTPROF"} ||= _default_env()
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{"NYTPROF"} ||= _default_env()
$ENV{"NYTPROF"} _default_env()
Truth table33%1/3
A ||= B
A= $ENV{"NYTPROF"}B= _default_env()
ABresult
1X1
011
000
36 1 require Devel::NYTProf;
37 1 DB::disable_profile();
38 1 $NYTPROF_LOADED = 1;
39 }
40  
41 1 ▶sub _default_env { 'start=no:sigexit=int' }
Subroutine _default_env: called CC=1 SCAR=0.0
42  
43 4 ▶sub prepare_app {
Subroutine prepare_app: called CC=1 SCAR=0.0
Pod documented
44 4 my $self = shift;
45  
46 4 $self->_setup_profile_id;
47 4 $self->_setup_profiling_file_paths;
48 4 $self->_setup_profiling_hooks;
49 4 $self->_setup_enable_profile;
50 4 $self->_setup_enable_reporting;
51 4 $self->_setup_report_dir;
52 4 $self->_setup_nytprofhtml_path;
53 }
54  
55 4 ▶sub _setup_profiling_file_paths {
Subroutine _setup_profiling_file_paths: called CC=1 SCAR=0.0
56 4 my $self = shift;
57 4 $self->_setup_profiling_result_dir;
58 4 $self->_setup_profiling_result_file_name;
59 4 $self->_setup_nullfile_name;
60 }
61  
62 4 ▶sub _setup_enable_reporting {
Subroutine _setup_enable_reporting: called CC=2 SCAR=7.2
63 4 my $self = shift;
64 4 ▶ $self->enable_reporting(1) unless defined $self->enable_reporting;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->enable_reporting 0 4 4
65 }
66  
67 4 ▶sub _setup_enable_profile {
Subroutine _setup_enable_profile: called CC=2 SCAR=6.9
68 4 my $self = shift;
69 4 ▶ $self->enable_profile( sub {1} ) unless $self->enable_profile;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enable_profile 3 1 4
70 }
71  
72 4 ▶sub _setup_profiling_result_dir {
Subroutine _setup_profiling_result_dir: called CC=2 SCAR=6.9
73 4 my $self = shift;
74 2 ▶ $self->profiling_result_dir( sub {'.'} )
Subroutine __ANON__: called CC=1 SCAR=0.0
75 4 ▶ unless is_code_ref( $self->profiling_result_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->profiling_result_dir) 3 1 4
76 }
77  
78 4 ▶sub _setup_report_dir {
Subroutine _setup_report_dir: called CC=2 SCAR=8.1
79 4 my $self = shift;
80 0 ▶ $self->report_dir( sub {'report'} )
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 4 ▶ unless is_code_ref( $self->report_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->report_dir) 4 0 4
82 }
83  
84 4 ▶sub _setup_nytprofhtml_path {
Subroutine _setup_nytprofhtml_path: called CC=3 SCAR=11.4
85 4 my $self = shift;
86 4 ▶ return if $self->nytprofhtml_path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->nytprofhtml_path 0 4 4
87 4 ▶ my $nytprofhtml_path = File::Which::which('nytprofhtml')
Branch: 1 branch
BranchTrueFalseTotal
my $nytprofhtml_path = File::Which::which("nytprofhtml") or die "Could not find nytprofhtml script. Ensure it's in your path" 4 0 4
88 or die "Could not find nytprofhtml script. Ensure it's in your path";
89 4 $self->nytprofhtml_path($nytprofhtml_path);
90 }
91  
92 4 ▶sub _setup_profile_id {
Subroutine _setup_profile_id: called CC=2 SCAR=7.1
93 4 my $self = shift;
94 3 ▶ $self->generate_profile_id( sub { return $$ . "-" . gettimeofday; } )
Subroutine __ANON__: called CC=1 SCAR=0.0
95 4 ▶ unless is_code_ref( $self->generate_profile_id );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->generate_profile_id) 4 0 4
96 }
97  
98 4 ▶sub _setup_profiling_result_file_name {
Subroutine _setup_profiling_result_file_name: called CC=2 SCAR=6.9
99 4 my $self = shift;
100 $self->profiling_result_file_name(
101 2 ▶ sub { my $id = $_[1]->{PROFILE_ID}; return "nytprof.$id.out"; } )
Subroutine __ANON__: called CC=1 SCAR=0.0
102 4 ▶ unless is_code_ref( $self->profiling_result_file_name );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->profiling_result_file_name) 3 1 4
103 }
104  
105 4 ▶sub _setup_nullfile_name {
Subroutine _setup_nullfile_name: called CC=2 SCAR=7.2
106 4 my $self = shift;
107  
108 4 ▶ $self->nullfile_name('nytprof.null.out') unless $self->nullfile_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->nullfile_name 4 0 4
109 }
110  
111 4 ▶sub _setup_profiling_hooks {
Subroutine _setup_profiling_hooks: called CC=3 SCAR=11.7
112 4 my $self = shift;
113 3 ▶ $self->before_profile( sub { } )
Subroutine __ANON__: called CC=1 SCAR=0.0
114 4 ▶ unless is_code_ref( $self->before_profile );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->before_profile) 4 0 4
115 3 ▶ $self->after_profile( sub { } )
Subroutine __ANON__: called CC=1 SCAR=0.0
116 4 ▶ unless is_code_ref( $self->after_profile );
Branch: 1 branch
BranchTrueFalseTotal
unless is_code_ref($self->after_profile) 4 0 4
117  
118 }
119  
120 4 ▶sub call {
Subroutine call: called CC=3 SCAR=15.8
Pod documented
121 4 my ( $self, $env ) = @_;
122  
123 4 $self->_setup_profiler($env);
124 4 $self->start_profiling_if_needed($env);
125  
126 4 my $res = $self->app->($env);
127  
128 4 ▶ if ( ref($res) && ref($res) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res and ref $res eq "ARRAY") 4 0 4
Condition33%1/3
ref $res and ref $res eq "ARRAY"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $res and ref $res eq "ARRAY"
ref $res ref $res eq "ARRAY"
Truth table33%1/3
A and B
A= ref $resB= ref $res eq "ARRAY"
ABresult
0X0
100
111
129 4 $self->stop_profiling_and_report_if_needed($env);
130 4 return $res;
131 }
132  
133 Plack::Util::response_cb(
134 $res,
135 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 my $res = shift;
137 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
138 0 my $chunk = shift;
139 0 ▶ if ( !defined $chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $chunk) 0 0 0
140 0 $self->stop_profiling_and_report_if_needed($env);
141 0 return;
142 }
143 0 return $chunk;
144 }
145 0 }
146 0 );
147 }
148  
149 4 ▶sub start_profiling_if_needed {
Subroutine start_profiling_if_needed: called CC=2 SCAR=6.9
Pod undocumented
150 4 my ( $self, $env ) = @_;
151 4 my $is_profiler_enabled = $self->enable_profile->($env);
152 4 ▶ return unless $is_profiler_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_profiler_enabled 1 3 4
153  
154 3 $env->{PROFILER_ENABLED} = 1;
155 3 $self->before_profile->( $self, $env );
156 3 $self->start_profiling($env);
157 }
158  
159 4 ▶sub stop_profiling_and_report_if_needed {
Subroutine stop_profiling_and_report_if_needed: called CC=3 SCAR=11.0
Pod undocumented
160 4 my ( $self, $env ) = @_;
161 4 my $is_profiler_enabled = $env->{PROFILER_ENABLED};
162 4 ▶ return unless $is_profiler_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_profiler_enabled 1 3 4
163  
164 3 $self->stop_profiling($env);
165 3 ▶ $self->report($env) if $self->enable_reporting;
Branch: 1 branch
BranchTrueFalseTotal
if $self->enable_reporting 0 3 3
166 3 $self->after_profile->( $self, $env );
167 }
168  
169 4 ▶sub _setup_profiler {
Subroutine _setup_profiler: called CC=6 SCAR=18.0
170 4 my ( $self, $env ) = @_;
171  
172 4 my $pid = $$;
173 4 ▶ return if $PROFILER_SETUPED{$pid};
Branch: 1 branch
BranchTrueFalseTotal
if $PROFILER_SETUPED{$pid} 0 4 4
174 4 $PROFILER_SETUPED{$pid} = 1;
175  
176 4 my $is_profiler_enabled = $self->enable_profile->($env);
177 4 ▶ return unless $is_profiler_enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_profiler_enabled 1 3 4
178  
179 3 ▶ return if $NYTPROF_LOADED;
Branch: 1 branch
BranchTrueFalseTotal
if $NYTPROF_LOADED 1 2 3
180  
181 2 ▶ $ENV{NYTPROF} = $ENV{NYTPROF} || $self->env_nytprof || _default_env();
Condition66%4/6
$ENV{'NYTPROF'} || $self->env_nytprof || _default_env()
l!l&&r!l&&!r
110
$ENV{"NYTPROF"} or $self->env_nytprof
l!l&&r!l&&!r
011
MC/DC0%0/3
$ENV{'NYTPROF'} || $self->env_nytprof || _default_env()
$ENV{"NYTPROF"} $self->env_nytprof _default_env()
Truth table50%2/4
A or B || C
A= $ENV{"NYTPROF"}B= $self->env_nytprofC= _default_env()
ABCresult
1XX1
01X1
0011
0000
182 2 require Devel::NYTProf;
183 2 DB::disable_profile();
184 2 $NYTPROF_LOADED = 1;
185 }
186  
187 3 ▶sub start_profiling {
Subroutine start_profiling: called CC=1 SCAR=0.0
Pod undocumented
188 3 my ( $self, $env ) = @_;
189  
190 3 $env->{PROFILE_ID} = $self->generate_profile_id->( $self, $env );
191 3 DB::enable_profile( $self->profiling_result_file_path($env) );
192 }
193  
194 3 ▶sub stop_profiling {
Subroutine stop_profiling: called CC=1 SCAR=0.0
Pod undocumented
195 3 DB::disable_profile();
196 }
197  
198 0 ▶sub report {
Subroutine report: not called CC=3 SCAR=24.8
Pod undocumented
199 0 my ( $self, $env ) = @_;
200  
201 0 ▶ return unless $env->{PROFILE_ID};
Branch: 1 branch
BranchTrueFalseTotal
unless $env->{"PROFILE_ID"} 0 0 0
202  
203 0 DB::enable_profile( $self->nullfile_path );
204 0 DB::disable_profile();
205 0 my $profiling_result_file = $self->profiling_result_file_path($env);
206 0 ▶ return unless ( -f $profiling_result_file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $profiling_result_file 0 0 0
207 0 system $self->nytprofhtml_path, "-f", $profiling_result_file,
208 '-o', $self->report_dir->();
209 }
210  
211 3 ▶sub profiling_result_file_path {
Subroutine profiling_result_file_path: called CC=1 SCAR=0.0
Pod undocumented
212 3 my ( $self, $env ) = @_;
213  
214 3 return File::Spec->catfile(
215 $self->profiling_result_dir->( $self, $env ),
216 $self->profiling_result_file_name->( $self, $env )
217 );
218 }
219  
220 0 ▶sub nullfile_path {
Subroutine nullfile_path: not called CC=1 SCAR=6.9
Pod undocumented
221 0 my ( $self, $env ) = @_;
222  
223 0 return File::Spec->catfile( $self->profiling_result_dir->( $self, $env ),
224 $self->nullfile_name );
225 }
226  
227 24 ▶sub is_code_ref {
Subroutine is_code_ref: called CC=2 SCAR=6.9
Pod undocumented
228 24 my $ref = shift;
229 24 ▶ return ( ref($ref) eq 'CODE' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'CODE' ? : 2 22 24
230 }
231  
232 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
233 4 ▶ DB::finish_profile() if defined &{"DB::finish_profile"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"DB::finish_profile";} 3 1 4
234 }
235  
236 5 1;
237  
238 __END__
↑ Summary