blib/lib/Devel/ContinuousProfiler.pm

filter: Statementstmt 43.8% 61 / 139 Branchbran 25.8% 16 / 62 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 50.0% 10 / 20 Podpod 50.0% 6 / 12 totaltotal 36.1% 95 / 263 CC 37 SCAR 25.2
CC
37
Cov
36%
report
23.4
report
23.4
take_snapshot
13.4
CRAP 12.4
↑ Summary ContinuousProfiler.xs »
1 package Devel::ContinuousProfiler;
2 # ABSTRACT: Ultra cheap profiling for use in production environments
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 ## no critic (UseWarnings, DotMatchAnything, NoCritic, NewLines, InterpolationOfMetachars))
6  
7 1 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant _COUNT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant _DEEPER => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use constant _FILE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant _FUNC => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our %DATA;
17 1 our $LAST_TIME_REPORT = 0;
18 1 our $FRAME_FORMAT;
19 1 our $FRAME_FORMAT2;
20 1 our $OUTPUT_HANDLE;
21 1 our $OUTPUT_SEEKABLE;
22 1 our $VERSION = '0.12';
23  
24 1 XSLoader::load(__PACKAGE__, $VERSION);
25  
26 1 ▶if ($ENV{PROFILER}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"PROFILER"}) 0 1 1
27 0 my %args = map { split /=/, $_, 2 } split /,/, $ENV{PROFILER};
28 0 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file"}) 0 0 0
29 0 output_filename( $args{file} );
30 }
31 0 ▶ if ($args{frame_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"frame_format"}) 0 0 0
32 0 $FRAME_FORMAT = $args{frame_format};
33 }
34 0 ▶ if ($args{frame_format2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"frame_format2"}) 0 0 0
35 0 $FRAME_FORMAT2 = $args{frame_format2};
36 }
37 }
38 1 ▶$OUTPUT_HANDLE = \ *STDERR unless defined $OUTPUT_HANDLE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $OUTPUT_HANDLE 1 0 1
39 1 ▶$FRAME_FORMAT = '%' . (1+_FUNC) . '$s' unless defined $FRAME_FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $FRAME_FORMAT 1 0 1
40 1 ▶$FRAME_FORMAT2 = '%' . (1+_FILE) . '$s' unless defined $FRAME_FORMAT2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $FRAME_FORMAT2 1 0 1
41  
42 0 ▶END { report() }
Subroutine END: not called CC=1 SCAR=6.9
43  
44 0 ▶sub frame_format {
Subroutine frame_format: not called CC=2 SCAR=17.9
Pod undocumented
45 0 ▶ if (@_) {
Subroutine frame_format: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
if (@_) { } 0 0 0
46 0 return ( $FRAME_FORMAT, $FRAME_FORMAT2 ) = @_;
47 }
48 else {
49 0 return ( $FRAME_FORMAT, $FRAME_FORMAT2 );
50 }
51 }
52  
53 0 ▶sub output_handle {
Subroutine output_handle: not called CC=2 SCAR=17.9
Pod undocumented
54 0 ▶ if (@_) {
Subroutine output_handle: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
if (@_) { } 0 0 0
55 0 $OUTPUT_SEEKABLE = 1;
56 0 return $OUTPUT_HANDLE = shift;
57 }
58 else {
59 0 return ($OUTPUT_HANDLE, $OUTPUT_SEEKABLE);
60 }
61 }
62  
63 0 ▶sub output_filename {
Subroutine output_filename: not called CC=3 SCAR=24.8
Pod undocumented
64 0 ▶ my ( $file ) = @_;
Subroutine output_filename: not called CC=3 SCAR=24.8
Pod undocumented
65  
66 0 ▶ if ($file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file) 0 0 0
if ($file) 0 0 0
67 ## no critic (InputOutput::RequireBriefOpen)
68 0 $OUTPUT_SEEKABLE = 0;
69 open $OUTPUT_HANDLE, '>', $file
70 0 ▶ or do {
Branch: 2 branches
BranchTrueFalseTotal
open $Devel::ContinuousProfiler::OUTPUT_HANDLE, ">", $file or do { warn "Can't open ${file}: $Devel::ContinuousProfiler::ERRNO"; return } 0 0 0
open $Devel::ContinuousProfiler::OUTPUT_HANDLE, ">", $file or do { warn "Can't open ${file}: $Devel::ContinuousProfiler::ERRNO"; return } 0 0 0
71 0 warn "Can't open $file: $ERRNO";
72 0 return;
73 };
74 0 $OUTPUT_SEEKABLE = 1;
75 }
76  
77 0 return $file;
78 }
79  
80 1 ▶sub take_snapshot {
Subroutine take_snapshot: called CC=6 SCAR=25.9
Pod documented
81 ## no critic (Variables::RequireInitializationForLocalVars)
82 ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
83 ## no critic (ControlStructures::ProhibitCStyleForLoops)
84  
85 0 ▶ local $@;
Subroutine take_snapshot: not called CC=6 SCAR=25.9
Pod documented
86 0 eval {
87  
88 # Witty comment
89 0 my $seen_take_snapshot;
90 my @stack;
91 0 ▶ for ( my $cx = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (my(@frame) = caller $cx) 0 0 0
if (my(@frame) = caller $cx) 4 1 5
92 my @frame = caller $cx;
93 ++ $cx ) {
94 0 ▶ if ( $frame[_FUNC] eq 'Devel::ContinuousProfiler::take_snapshot' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($frame[3] eq 'Devel::ContinuousProfiler::take_snapshot') { } 0 0 0
elsif ($seen_take_snapshot) { } 0 0 0
if ($frame[3] eq 'Devel::ContinuousProfiler::take_snapshot') { } 1 3 4
elsif ($seen_take_snapshot) { } 2 1 3
95 0 $seen_take_snapshot = 1;
96 }
97 elsif ( $seen_take_snapshot ) {
98 0 my $p = sprintf($FRAME_FORMAT, @frame);
99 0 ▶ $p ||= sprintf($FRAME_FORMAT2, @frame);
Condition16%1/6
$p ||= sprintf($Devel::ContinuousProfiler::FRAME_FORMAT2, @frame)
l!l&&r!l&&!r
000
$p ||= sprintf($FRAME_FORMAT2, @frame)
l!l&&r!l&&!r
200
MC/DC0%0/2
$p ||= sprintf($Devel::ContinuousProfiler::FRAME_FORMAT2, @frame)
$p sprintf($Devel::ContinuousProfiler::FRAME_FORMAT2, @frame)
MC/DC0%0/2
$p ||= sprintf($FRAME_FORMAT2, @frame)
$p sprintf($FRAME_FORMAT2, @frame)
Truth table0%0/3
A ||= B
A= $pB= sprintf($Devel::ContinuousProfiler::FRAME_FORMAT2, @frame)
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $pB= sprintf($FRAME_FORMAT2, @frame)
ABresult
1X1
011
000
100 0 unshift @stack, $p;
101 }
102 }
103  
104 0 my $t = time;
105 0 my $s = join ',', @stack;
106 0 ▶ if (my $h = $DATA{$s}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $h = $Devel::ContinuousProfiler::DATA{$s}) { } 0 0 0
if (my $h = $DATA{$s}) { } 0 1 1
107 0 ++ $h->[0];
108 0 $h->[1] = $t;
109 }
110 else {
111 0 $DATA{$s} = [
112 1,
113 $t,
114 $t,
115 ];
116 }
117  
118 0 report();
119 };
120  
121 0 return;
122 }
123  
124 #sub _take_tree_snapshot {
125 # # Erudite comment.
126 # my $frame = \ %TREE_DATA;
127 # for ( my $i = 0;
128 # $i < $#{$_[0]} / 2;
129 # $i += 2 ) {
130 #
131 # # "filename:function"
132 # my $label =
133 # $_[0][$i]
134 # . ':'
135 # . $_[0][$i+1];
136 #
137 # # Utter BS.
138 # if ( my $f = $frame->{$label} ) {
139 # ++ $f->[_COUNT];
140 # $frame = $f->[_DEEPER];
141 # }
142 # else {
143 # $frame->{$label} =
144 # [
145 # 1, # _COUNT
146 # {}, # _DEEPER
147 # ];
148 # }
149 # }
150 #
151 # return;
152 #}
153  
154 #sub _take_basic_snapshot {
155 # my @frames;
156 # # Irony.
157 # for ( my $i = 0;
158 # $i < $#{$_[0]} / 2;
159 # $i += 2 ) {
160 # push @frames, "$_[0][$i]:$_[0][$i+1]";
161 # }
162 # ++$BASIC_DATA{join ',', @frames};
163 #
164 # return;
165 #}
166  
167 2 ▶sub report {
Subroutine report: called CC=7 SCAR=31.5
Pod documented
168 # At most once per second.
169 0 ▶ return if $LAST_TIME_REPORT == time;
Subroutine report: not called CC=7 SCAR=31.5
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if $Devel::ContinuousProfiler::LAST_TIME_REPORT == time 0 0 0
if $LAST_TIME_REPORT == time 1 1 2
170 0 $LAST_TIME_REPORT = time;
171  
172 0 my $report = report_strings();
173  
174 0 ▶ if ($OUTPUT_HANDLE && $OUTPUT_SEEKABLE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($Devel::ContinuousProfiler::OUTPUT_HANDLE and $Devel::ContinuousProfiler::OUTPUT_SEEKABLE) { } 0 0 0
elsif ($Devel::ContinuousProfiler::OUTPUT_HANDLE) { } 0 0 0
if ($OUTPUT_HANDLE and $OUTPUT_SEEKABLE) { } 0 1 1
elsif ($OUTPUT_HANDLE) { } 1 0 1
Condition16%1/6
$Devel::ContinuousProfiler::OUTPUT_HANDLE and $Devel::ContinuousProfiler::OUTPUT_SEEKABLE
!ll&&!rl&&r
000
$OUTPUT_HANDLE and $OUTPUT_SEEKABLE
!ll&&!rl&&r
010
MC/DC0%0/2
$Devel::ContinuousProfiler::OUTPUT_HANDLE and $Devel::ContinuousProfiler::OUTPUT_SEEKABLE
$Devel::ContinuousProfiler::OUTPUT_HANDLE $Devel::ContinuousProfiler::OUTPUT_SEEKABLE
MC/DC0%0/2
$OUTPUT_HANDLE and $OUTPUT_SEEKABLE
$OUTPUT_HANDLE $OUTPUT_SEEKABLE
Truth table0%0/3
A and B
A= $Devel::ContinuousProfiler::OUTPUT_HANDLEB= $Devel::ContinuousProfiler::OUTPUT_SEEKABLE
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $OUTPUT_HANDLEB= $OUTPUT_SEEKABLE
ABresult
0X0
100
111
175 0 $OUTPUT_SEEKABLE = seek $OUTPUT_HANDLE, 0, 0;
176 0 truncate $OUTPUT_HANDLE, 0;
177 0 syswrite $OUTPUT_HANDLE, $_ for @$report;
178 } elsif ( $OUTPUT_HANDLE ) {
179 0 syswrite $OUTPUT_HANDLE, $_ for @$report;
180 }
181  
182 0 return;
183 }
184  
185 1 ▶sub report_strings {
Subroutine report_strings: called CC=4 SCAR=22.7
Pod documented
186 ## no critic (ReverseSortBlock)
187  
188 0 ▶ my $max_length = 0;
Subroutine report_strings: not called CC=4 SCAR=22.7
Pod documented
189 0 for ( values %DATA ) {
190 0 ▶ $max_length = length $_->[0] if length($_->[0]) > $max_length;
Branch: 2 branches
BranchTrueFalseTotal
if length $_->[0] > $max_length 0 0 0
if length $_->[0] > $max_length 1 0 1
191 }
192  
193 0 my $format = "=$PID= %${max_length}d %s\n";
194 return [
195 "=$PID= $PROGRAM_NAME profiling stats.\n",
196 0 map { sprintf $format, $DATA{$_}[0], $_ }
197 0 ▶ sort { $DATA{$b}[0] <=> $DATA{$a}[0] || $DATA{$b}[1] <=> $DATA{$a}[1] }
Condition0%0/6
$Devel::ContinuousProfiler::DATA{$b}[0] <=> $Devel::ContinuousProfiler::DATA{$a}[0] || $Devel::ContinuousProfiler::DATA{$b}[1] <=> $Devel::ContinuousProfiler::DATA{$a}[1]
l!l&&r!l&&!r
000
$DATA{$b}[0] <=> $DATA{$a}[0] || $DATA{$b}[1] <=> $DATA{$a}[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Devel::ContinuousProfiler::DATA{$b}[0] <=> $Devel::ContinuousProfiler::DATA{$a}[0] || $Devel::ContinuousProfiler::DATA{$b}[1] <=> $Devel::ContinuousProfiler::DATA{$a}[1]
$Devel::ContinuousProfiler::DATA{$b}[0] <=> $Devel::ContinuousProfiler::DATA{$a}[0] $Devel::ContinuousProfiler::DATA{$b}[1] <=> $Devel::ContinuousProfiler::DATA{$a}[1]
MC/DC0%0/2
$DATA{$b}[0] <=> $DATA{$a}[0] || $DATA{$b}[1] <=> $DATA{$a}[1]
$DATA{$b}[0] <=> $DATA{$a}[0] $DATA{$b}[1] <=> $DATA{$a}[1]
Truth table0%0/3
A || B
A= $Devel::ContinuousProfiler::DATA{$b}[0] <=> $Devel::ContinuousProfiler::DATA{$a}[0]B= $Devel::ContinuousProfiler::DATA{$b}[1] <=> $Devel::ContinuousProfiler::DATA{$a}[1]
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $DATA{$b}[0] <=> $DATA{$a}[0]B= $DATA{$b}[1] <=> $DATA{$a}[1]
ABresult
1X1
011
000
198 keys %DATA
199 ];
200 }
201  
202 1 'I am an anarchist
203 An antichrist
204 An asterix
205 I am an anorak
206 An acolyte
207 An accidental
208 I am eleven feet
209 Ok, eight...
210 Six foot three...
211 I fought the British and I won
212 I have a rocket ship
213 A jetfighter
214 A paper airplane';
215  
216 __END__
↑ Summary ContinuousProfiler.xs »