DashProfiler/Sample.pm

filter: Statementstmt 93.3% 42 / 45 Branchbran 56.2% 9 / 16 Conditioncond 54.5% 6 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 76.4% 68 / 89 CC 14 SCAR 16.5
CC
14
Cov
79%
new
8.8
BEGIN
5.4
DESTROY
5.1
CRAP 5.2
« DashProfiler/Core.pm ↑ Summary DashProfiler/Auto.pm »
1 package DashProfiler::Sample;
2  
3 =head1 NAME
4  
5 DashProfiler::Sample - encapsulates the acquisition of a single sample
6  
7 =head1 DESCRIPTION
8  
9 Firstly, read L<DashProfiler::UserGuide> for a general introduction.
10  
11 A DashProfiler::Sample object is returned from the prepare() method of DashProfiler::Core,
12 or from the functions imported by DashProfiler::Import.
13  
14 The object, and this class, are rarely used directly.
15  
16 =head1 METHODS
17  
18 =cut
19  
20 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 our $VERSION = sprintf("1.%06d", q$Revision: 48 $ =~ /(\d+)/o);
23  
24 5 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use DBI::Profile qw(dbi_profile dbi_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use constant DBI_GE_1603 => ($DBI::VERSION >= 2.603);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.9
30 # use env var to control debugging at compile-time
31 # see pod for DEBUG at end
32 5 ▶ my $debug = $ENV{DASHPROFILER_SAMPLE_DEBUG} || $ENV{DASHPROFILER_DEBUG} || 0;
Condition40%2/5
$ENV{'DASHPROFILER_SAMPLE_DEBUG'} || $ENV{'DASHPROFILER_DEBUG'} || 0
l!l
05
$ENV{"DASHPROFILER_SAMPLE_DEBUG"} or $ENV{"DASHPROFILER_DEBUG"}
l!l&&r!l&&!r
005
MC/DC0%0/2
$ENV{'DASHPROFILER_SAMPLE_DEBUG'} || $ENV{'DASHPROFILER_DEBUG'} || 0
$ENV{"DASHPROFILER_SAMPLE_DEBUG"} $ENV{"DASHPROFILER_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"DASHPROFILER_SAMPLE_DEBUG"}B= $ENV{"DASHPROFILER_DEBUG"}
ABresult
1X1
011
000
33 5 ▶ eval "sub DEBUG () { $debug }; 1;" or die; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
eval "sub DEBUG () { $debug }; 1;" or die 5 0 5
34 }
35  
36  
37 =head2 new
38  
39 This method is normally only called by the code reference returned from the
40 DashProfiler::Core prepare() method, and not directly.
41  
42 $sample = DashProfiler::Sample->new($meta, $context2);
43 $sample = DashProfiler::Sample->new($meta, $context2, $start_time, $allow_overlap);
44  
45 The returned object encapsulates the time of its creation and the supplied arguments.
46  
47 The $meta parameter must be a hash reference containing at least a
48 'C<_dash_profile>' element which must be a reference to a DashProfiler::Core
49 object. The new() method marks the profile as 'in use'.
50  
51 If the $context2 is false then $meta->{_context2} is used instead.
52  
53 If $start_time false, which it normally is, then the value returned by dbi_time() is used instead.
54  
55 If $allow_overlap is false, which it normally is, then if the DashProfiler
56 refered to by the 'C<_dash_profile>' element of %$meta is marked as 'in use'
57 then a warning is given (just once) and C<new> returns undef, so no sample is
58 taken.
59  
60 If $allow_overlap is true, then overlaping samples can be taken. However, if
61 samples do overlap then C<period_exclusive> is disabled for that DashProfiler.
62  
63 =cut
64  
65 9213 ▶sub new {
Subroutine new: called CC=7 SCAR=21.8
Pod documented
66 # ($class, $meta, $context2, $start_time, $allow_overlap)
67 9213 my $profile_ref = $_[1]->{_dash_profile}; # $meta->_dash_profile
68 9213 ▶ if ($profile_ref->{in_use}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_ref->{"in_use"}++) 1 9212 9213
69 1 ▶ if ($profile_ref->{disabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_ref->{"disabled"}) 0 1 1
70 0 $profile_ref->{in_use}--; # undo the increment we did above
71 0 return;
72 }
73 1 ▶ if ($_[4]) { # allow_overlaping_use
Branch: 1 branch
BranchTrueFalseTotal
if ($_[4]) { } 0 1 1
74 # can't use exclusive timer with nested samples
75 0 undef $profile_ref->{exclusive_sampler};
76 }
77 else {
78 Carp::cluck("$_[0] $profile_ref->{profile_name} already active")
79 1 ▶ unless $profile_ref->{in_use_warning_given}++; # warn once
Branch: 1 branch
BranchTrueFalseTotal
unless $profile_ref->{"in_use_warning_given"}++ 1 0 1
80 1 return; # don't double count
81 }
82 }
83 # to help debug nested profile samples you can uncomment this
84 # and remove the ++ from the if() above and tweak the cluck message
85 #$profile_ref->{in_use} = Carp::longmess("");
86 return bless [
87 $_[1],
88 $_[2] || $_[1]->{_context2},
89 9212 ▶ $_[3] || dbi_time(), # do this as late as practical
Condition66%4/6
$_[2] || $_[1]{'_context2'}
l!l&&r!l&&!r
920750
$_[3] || dbi_time()
l!l&&r!l&&!r
492080
MC/DC0%0/2
$_[2] || $_[1]{'_context2'}
$_[2] $_[1]{'_context2'}
MC/DC0%0/2
$_[3] || dbi_time()
$_[3] dbi_time()
Truth table66%2/3
A || B
A= $_[2]B= $_[1]{'_context2'}
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $_[3]B= dbi_time()
ABresult
1X1
011
000
90 ] => $_[0];
91 }
92  
93  
94 =head2 current_sample_duration
95  
96 $ps = foo_profiler(...);
97 my $duration = $ps->current_sample_duration();
98  
99 Returns the amount of time since the sample was created.
100  
101 =cut
102  
103 136270 ▶sub current_sample_duration {
Subroutine current_sample_duration: called CC=1 SCAR=0.0
Pod documented
104 136270 return dbi_time() - shift->[2];
105 }
106  
107  
108 =head2 DESTROY
109  
110 When the DashProiler::Sample object is destroyed it:
111  
112 - calls dbi_time() to get the time of the end of the sample
113  
114 - marks the profile as no longer 'in use'
115  
116 - adds the timespan of the sample to the 'period_accumulated' of the DashProiler
117  
118 - extracts context2 from the DashProiler::Sample object. If it's a code reference
119 then it's executed and the return value is used as context2.
120 This is very useful where the value of context2 can't be determined
121 at the time the sample is started.
122  
123 - if the $meta hash reference (passed to new()) contained a 'C<context2edit>'
124 code reference then it's called and passed context2 and $meta.
125 The return value is used as context2.
126  
127 - calls DBI::Profile::dbi_profile(handle, context1, context2, start time, end time)
128 for each DBI profile currently attached to the DashProiler.
129  
130 =cut
131  
132 9212 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.2
133 9212 my $end_time = dbi_time(); # get timestamp as early as practical
134  
135 # Any fatal errors won't be reported because we're in a DESTROY.
136 # This can make debugging hard. If you suspect a problem then uncomment this:
137 #local $SIG{__DIE__} = sub { warn @_ } if DEBUG(); ## no critic
138 # Note that throwing an exception can be used by the context2edit hook
139 # to 'veto' the sample.
140  
141 9212 my ($meta, $context2, $start_time) = @{+shift};
142  
143 9212 my $profile_ref = $meta->{_dash_profile};
144 9212 undef $profile_ref->{in_use};
145 9212 $profile_ref->{period_accumulated} += $end_time - $start_time;
146  
147 9212 ▶ $context2 = $context2->($meta)
Branch: 1 branch
BranchTrueFalseTotal
if ref $context2 eq "CODE" 0 9212 9212
148 if ref $context2 eq 'CODE';
149 $context2 = $meta->{context2edit}->($context2, $meta)
150 9212 ▶ if ref $meta->{context2edit} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta->{"context2edit"} eq "CODE" 0 9212 9212
151  
152 carp(sprintf "%s: %s %s: %f - %f = %f",
153 9212 $profile_ref->{profile_name}, $meta->{_context1}, $context2, $start_time, $end_time, $end_time-$start_time
154 ) if DEBUG() and DEBUG() >= 4;
155  
156 9212 if (DBI_GE_1603()) { # use more functional dbi_profile() if available
157 dbi_profile($profile_ref->{dbi_handles_active}, $meta->{_context1}, $context2, $start_time, $end_time);
158 }
159 else {
160 # if you get an sv_dump ("SV = RV(0x181aa80) at 0x1889a80 ...") to stderr
161 # it probably means %$dbi_handles_active contains a plain hash ref not a dbh
162 9212 for (values %{$profile_ref->{dbi_handles_active}}) {
163 9213 ▶ next unless defined; # skip any dead weakrefs
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 9213 9213
164 9213 dbi_profile($_, $meta->{_context1}, $context2, $start_time, $end_time);
165 }
166 }
167  
168 9212 return;
169 }
170  
171  
172 5 1;
173  
174 =head2 DEBUG
175  
176 The DEBUG subroutine is a constant that returns whatever the value of
177  
178 $ENV{DASHPROFILER_SAMPLE_DEBUG} || $ENV{DASHPROFILER_DEBUG} || 0;
179  
180 was when the modle was loaded.
181  
182 =head1 AUTHOR
183  
184 DashProfiler by Tim Bunce, L<http://www.tim.bunce.name> and
185 L<http://blog.timbunce.org>
186  
187 =head1 COPYRIGHT
188  
189 The DashProfiler distribution is Copyright (c) 2007-2008 Tim Bunce. Ireland.
190 All rights reserved.
191  
192 You may distribute under the terms of either the GNU General Public
193 License or the Artistic License, as specified in the Perl README file.
194  
195 =cut
196  
« DashProfiler/Core.pm ↑ Summary DashProfiler/Auto.pm »