| 1 | package DashProfiler::Core; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | DashProfiler::Core - DashProfiler core object and sampler factory | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | See L<DashProfiler::UserGuide> for a general introduction. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | DashProfiler::Core is currently viewed as an internal class. The interface may change. | ||||||||||||||||||||||||||||
| 12 | The DashProfiler and DashProfiler::Import modules are the usual interfaces. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | A DashProfiler::Core objects are the core of the DashProfiler, naturally. | ||||||||||||||||||||||||||||
| 17 | They sit between the 'samplers' that feed data into a core, and the DBI::Profile | ||||||||||||||||||||||||||||
| 18 | objects that aggregate those samples. A core may have multiple samplers and | ||||||||||||||||||||||||||||
| 19 | multiple profiles. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | our $VERSION = sprintf("1.%06d", q$Revision: 48 $ =~ /(\d+)/o); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use DBI 1.57 qw(dbi_time dbi_profile_merge); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use DBI::Profile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use DBI::ProfileDumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | our $ENDING = 0; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=16.9
| |||||||||||||||||||||||||||||
| 35 | # use env var to control debugging at compile-time | ||||||||||||||||||||||||||||
| 36 | 5 | ▶ | my $debug = $ENV{DASHPROFILER_CORE_DEBUG} || $ENV{DASHPROFILER_DEBUG} || 0; | ||||||||||||||||||||||||||
|
Condition40%2/5
$ENV{'DASHPROFILER_CORE_DEBUG'} || $ENV{'DASHPROFILER_DEBUG'} || 0
$ENV{"DASHPROFILER_CORE_DEBUG"} or $ENV{"DASHPROFILER_DEBUG"}
MC/DC0%0/2
$ENV{'DASHPROFILER_CORE_DEBUG'} || $ENV{'DASHPROFILER_DEBUG'} || 0
$ENV{"DASHPROFILER_CORE_DEBUG"}
$ENV{"DASHPROFILER_DEBUG"}
Truth table33%1/3
A or B
A= $ENV{"DASHPROFILER_CORE_DEBUG"}B= $ENV{"DASHPROFILER_DEBUG"}
| |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | eval "sub DEBUG () { $debug }; 1;" or die; ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 5 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 5 | $ENDING = 1; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=15.2
| |||||||||||||||||||||||||||||
| 45 | # load Hash::Util for lock_keys() | ||||||||||||||||||||||||||||
| 46 | # if Hash::Util isn't available then install a stub for lock_keys() | ||||||||||||||||||||||||||||
| 47 | 5 | eval { | |||||||||||||||||||||||||||
| 48 | 5 | require Hash::Util; | |||||||||||||||||||||||||||
| 49 | 5 | Hash::Util->import('lock_keys'); | |||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | 5 | ▶ | die @$ if $@ && $@ !~ /^Can't locate Hash\/Util/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ and not $@ =~ m[^Can't locate Hash/Util]
MC/DC0%0/2
$@ and not $@ =~ m[^Can't locate Hash/Util]
$@
not $@ =~ m[^Can't locate Hash/Util]
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ m[^Can't locate Hash/Util]
| |||||||||||||||||||||||||||||
| 52 | 5 | ▶ | *lock_keys = sub { } if not defined &lock_keys; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # check for weaken support, used by ChildHandles | ||||||||||||||||||||||||||||
| 57 | 5 | my $HAS_WEAKEN = eval { | |||||||||||||||||||||||||||
| 58 | 5 | require Scalar::Util; | |||||||||||||||||||||||||||
| 59 | # this will croak() if this Scalar::Util doesn't have a working weaken(). | ||||||||||||||||||||||||||||
| 60 | 5 | Scalar::Util::weaken( my $test = [] ); | |||||||||||||||||||||||||||
| 61 | 5 | 1; | |||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | *weaken = sub { croak "Can't weaken without Scalar::Util::weaken" } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 5 | ▶ | unless $HAS_WEAKEN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # On 2GHz OS X 10.5.2 laptop: | ||||||||||||||||||||||||||||
| 68 | # sample_overhead_time = 0.000014s | ||||||||||||||||||||||||||||
| 69 | # sample_inner_time = 0.000003s | ||||||||||||||||||||||||||||
| 70 | 5 | my ($sample_overhead_time, $sample_inner_time) = estimate_sample_overheads(); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 new | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | $obj = DashProfiler::Core->new( 'foo' ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | $obj = DashProfiler::Core->new( 'bar', { ...options... } ); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $obj = DashProfiler::Core->new( extsys => { | ||||||||||||||||||||||||||||
| 82 | granularity => 10, | ||||||||||||||||||||||||||||
| 83 | flush_interval => 300, | ||||||||||||||||||||||||||||
| 84 | } ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Creates and returns a DashProfiler::Core object. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 Options for new() | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head3 disabled | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Set to a true value to prevent samples being added to this core. If true, the | ||||||||||||||||||||||||||||
| 93 | prepare() method and the L<DashProfiler::Sample> new() method will return undef. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Default false. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Currently, any existing samples that were active will still be added when they | ||||||||||||||||||||||||||||
| 98 | terminate. This behaviour may change. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | See also L<DashProfiler::Import>. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head3 dbi_profile_class | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Specifies the class to use for creating DBI::Profile objects. | ||||||||||||||||||||||||||||
| 105 | The default is C<DBI::Profile>. Alternatives include C<DBI::ProfileDumper> | ||||||||||||||||||||||||||||
| 106 | and C<DBI::ProfileDumper::Apache>. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head3 dbi_profile_args | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Specifies extra arguments to pass the new() method of the C<dbi_profile_class> | ||||||||||||||||||||||||||||
| 111 | (e.g., C<DBI::Profile>). The default is C<{ }>. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head3 flush_interval | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | How frequently the DBI:Profiles associated with this core should be written out | ||||||||||||||||||||||||||||
| 116 | and the data reset. Default is 0 - no regular flushing. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head3 flush_hook | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | If set, this code reference is called when flush() is called and can influence | ||||||||||||||||||||||||||||
| 121 | its behaviour. For example, this is the flush_hook used by L<DashProfiler::Auto>: | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | flush_hook => sub { | ||||||||||||||||||||||||||||
| 124 | my ($self, $dbi_profile_name) = @_; | ||||||||||||||||||||||||||||
| 125 | warn $_ for $self->profile_as_text($dbi_profile_name); | ||||||||||||||||||||||||||||
| 126 | return $self->reset_profile_data($dbi_profile_name); | ||||||||||||||||||||||||||||
| 127 | }, | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | See L</flush> for more details. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head3 granularity | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | The default C<Path> for the DBI::Profile objects doesn't include time. | ||||||||||||||||||||||||||||
| 134 | The granularity option adds 'C<!Time~$granularity>' to the front of the Path. | ||||||||||||||||||||||||||||
| 135 | So as time passes the samples are aggregated into new sub-trees. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head3 sample_class | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | The sample_class option specifies which class should be used to take profile samples. | ||||||||||||||||||||||||||||
| 140 | The default is C<DashProfiler::Sample>. | ||||||||||||||||||||||||||||
| 141 | See the L</prepare> method for more information. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head3 period_summary | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Specifies the name of an extra DBI Profile object to attach to the core. | ||||||||||||||||||||||||||||
| 146 | This extra 'period summary' profile is enabled and reset by the start_sample_period() | ||||||||||||||||||||||||||||
| 147 | method and disabled by the end_sample_period() method. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The mechanism enables a single profile to be used to capture both long-running | ||||||||||||||||||||||||||||
| 150 | sampling (often with C<granularity> set) and single-period (e.g., for a 'debug' | ||||||||||||||||||||||||||||
| 151 | footer on a generated web page) | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head3 period_exclusive | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | When using periods, via the start_sample_period() and end_sample_period() methods, | ||||||||||||||||||||||||||||
| 156 | DashProfiler can add an additional sample representing the time between the | ||||||||||||||||||||||||||||
| 157 | start_sample_period() and end_sample_period() method calls that I<wasn't> | ||||||||||||||||||||||||||||
| 158 | accounted for by the samples. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | The period_exclusive option enables this extra sample. The value of the option | ||||||||||||||||||||||||||||
| 161 | is used as the value for key1 and key2 in the Path. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head3 period_strict_start | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | See L</start_sample_period>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head3 period_strict_end | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | See L</end_sample_period>. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head3 profile_as_text_args | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | A reference to a hash containing default formatting arguments for the profile_as_text() method. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head3 extra_info | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Can be used to attach any extra information to the profiler core object. That can be useful sometimes in callbacks. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | 12 | my ($class, $profile_name, $opt_params) = @_; | |||||||||||||||||||||||||||
| 183 | 12 | ▶ | $opt_params ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt_params ||= {}
MC/DC0%0/1
$opt_params ||= {}
$opt_params
Truth table50%1/2
A
A= $opt_params
| |||||||||||||||||||||||||||||
| 184 | 12 | ▶ | croak "No profile_name given" unless $profile_name && not ref $profile_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$profile_name and not ref $profile_name
MC/DC0%0/2
$profile_name and not ref $profile_name
$profile_name
not ref $profile_name
Truth table33%1/3
A and not(B)
A= $profile_nameB= not ref $profile_name
| |||||||||||||||||||||||||||||
| 185 | 12 | ▶ | croak "$class->new($profile_name, $opt_params) options must be a hash reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | if ref $opt_params ne 'HASH'; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 12 | ▶ | our $opt_defaults ||= { | ||||||||||||||||||||||||||
|
Condition100%2/2
our $opt_defaults ||= {"disabled", 0, "sample_class", "DashProfiler::Sample", "dbi_profile_class", "DBI::Profile", "dbi_profile_args", {}, "flush_interval", 0, "flush_hook", undef, "granularity", 0, "period_exclusive", undef, "period_summary", undef, "period_strict_start", 1, "period_strict_end", 0, "profile_as_text_args", undef, "extra_info", undef}
MC/DC100%1/1
our $opt_defaults ||= {"disabled", 0, "sample_class", "DashProfiler::Sample", "dbi_profile_class", "DBI::Profile", "dbi_profile_args", {}, "flush_interval", 0, "flush_hook", undef, "granularity", 0, "period_exclusive", undef, "period_summary", undef, "period_strict_start", 1, "period_strict_end", 0, "profile_as_text_args", undef, "extra_info", undef}
our $opt_defaults
Truth table100%2/2
A
A= our $opt_defaults
| |||||||||||||||||||||||||||||
| 189 | disabled => 0, | ||||||||||||||||||||||||||||
| 190 | sample_class => 'DashProfiler::Sample', | ||||||||||||||||||||||||||||
| 191 | dbi_profile_class => 'DBI::Profile', | ||||||||||||||||||||||||||||
| 192 | dbi_profile_args => {}, | ||||||||||||||||||||||||||||
| 193 | flush_interval => 0, | ||||||||||||||||||||||||||||
| 194 | flush_hook => undef, | ||||||||||||||||||||||||||||
| 195 | granularity => 0, | ||||||||||||||||||||||||||||
| 196 | period_exclusive => undef, | ||||||||||||||||||||||||||||
| 197 | period_summary => undef, | ||||||||||||||||||||||||||||
| 198 | period_strict_start => 0x01, | ||||||||||||||||||||||||||||
| 199 | period_strict_end => 0x00, | ||||||||||||||||||||||||||||
| 200 | profile_as_text_args => undef, | ||||||||||||||||||||||||||||
| 201 | extra_info => undef, # for caller to hook in their own data | ||||||||||||||||||||||||||||
| 202 | }; | ||||||||||||||||||||||||||||
| 203 | 0 | croak "Invalid options: ".join(', ', grep { !$opt_defaults->{$_} } keys %$opt_params) | |||||||||||||||||||||||||||
| 204 | 12 | ▶ | if keys %{ { %$opt_defaults, %$opt_params } } > keys %$opt_defaults; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 12 | my $time = dbi_time(); | |||||||||||||||||||||||||||
| 207 | 12 | my $self = bless { | |||||||||||||||||||||||||||
| 208 | profile_name => $profile_name, | ||||||||||||||||||||||||||||
| 209 | in_use => 0, | ||||||||||||||||||||||||||||
| 210 | in_use_warning_given => 0, | ||||||||||||||||||||||||||||
| 211 | dbi_handles_all => {}, | ||||||||||||||||||||||||||||
| 212 | dbi_handles_active => {}, | ||||||||||||||||||||||||||||
| 213 | flush_due_at_time => undef, | ||||||||||||||||||||||||||||
| 214 | # for start_period | ||||||||||||||||||||||||||||
| 215 | period_count => 0, | ||||||||||||||||||||||||||||
| 216 | period_start_time => 0, | ||||||||||||||||||||||||||||
| 217 | period_accumulated => 0, | ||||||||||||||||||||||||||||
| 218 | exclusive_sampler => undef, | ||||||||||||||||||||||||||||
| 219 | %$opt_defaults, | ||||||||||||||||||||||||||||
| 220 | %$opt_params, | ||||||||||||||||||||||||||||
| 221 | } => $class; | ||||||||||||||||||||||||||||
| 222 | 12 | $self->{flush_due_at_time} = $time + $self->{flush_interval}; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 12 | lock_keys(%$self); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 12 | _load_class($self->{sample_class}); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 12 | ▶ | if (my $exclusive_name = $self->{period_exclusive}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | # create the sampler through which period_exclusive samples are added | ||||||||||||||||||||||||||||
| 230 | # by end_sample_period() | ||||||||||||||||||||||||||||
| 231 | 2 | $self->{exclusive_sampler} = $self->prepare($exclusive_name, $exclusive_name); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 12 | my $dbi_profile = $self->_mk_dbi_profile($self->{dbi_profile_class}, $self->{granularity}); | |||||||||||||||||||||||||||
| 234 | 12 | $self->attach_dbi_profile( $dbi_profile, "main", 0 ); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 12 | ▶ | if (my $period_summary = $self->{period_summary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 1 | my $dbi_profile = $self->_mk_dbi_profile("DashProfiler::DumpNowhere", 0); | |||||||||||||||||||||||||||
| 238 | 1 | my $dbh = $self->attach_dbi_profile( $dbi_profile, "period_summary", 0 ); | |||||||||||||||||||||||||||
| 239 | 1 | $self->{dbi_handles_all}{period_summary} = $dbh; | |||||||||||||||||||||||||||
| 240 | # start_sample_period() will add handle to {dbi_handles_active} | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # mark as in_use if disabled as this allows the sampler to be more efficient | ||||||||||||||||||||||||||||
| 244 | 12 | ▶ | $self->{in_use} = -42 if $self->{disabled}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 12 | return $self; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 estimate_sample_overheads | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | $sample_overhead_time = DashProfiler::Core->estimate_sample_overheads(); | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | ($sample_overhead_time, $sample_inner_time) | ||||||||||||||||||||||||||||
| 255 | = DashProfiler::Core->estimate_sample_overheads(); | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Estimates and returns the approximate minimum time overhead for taking a sample. | ||||||||||||||||||||||||||||
| 258 | Two times are returned. The following timeline diagram explains the difference: | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | previous statement ------------- | ||||||||||||||||||||||||||||
| 261 | | | ||||||||||||||||||||||||||||
| 262 | sampler called | | ||||||||||||||||||||||||||||
| 263 | sampler does work | | ||||||||||||||||||||||||||||
| 264 | sampler reads time ----- | | ||||||||||||||||||||||||||||
| 265 | sampler does work | | | ||||||||||||||||||||||||||||
| 266 | return sample object | | | ||||||||||||||||||||||||||||
| 267 | | | | ||||||||||||||||||||||||||||
| 268 | (measured statements) | | | ||||||||||||||||||||||||||||
| 269 | | | | ||||||||||||||||||||||||||||
| 270 | sample DESTROY'd | | | ||||||||||||||||||||||||||||
| 271 | sample does work v | | ||||||||||||||||||||||||||||
| 272 | sample reads time ----- | = sample_inner_time | ||||||||||||||||||||||||||||
| 273 | sample does work | | ||||||||||||||||||||||||||||
| 274 | v | ||||||||||||||||||||||||||||
| 275 | next statement ------------- = sample_overhead_time | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | For estimate_sample_overheads() there are no I<measured statements> so the | ||||||||||||||||||||||||||||
| 278 | times reflect the pure overheads. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Note that because estimate_sample_overheads() uses a tight loop, the timings | ||||||||||||||||||||||||||||
| 281 | returned are likely to be I<slightly> smaller then the timings you'd get in | ||||||||||||||||||||||||||||
| 282 | practice due to CPU L2 caches and other factors. This is okay. | ||||||||||||||||||||||||||||
| 283 | On my 2GHz laptop running OS X 10.5.2 $sample_overhead_time is 0.000014 and | ||||||||||||||||||||||||||||
| 284 | $sample_inner_time is 0.000003. (When doing occasional sampling the | ||||||||||||||||||||||||||||
| 285 | sample_overhead_time is 0.000002 to 0.000003 higher, in case you care.) | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | DashProfiler automatically calls estimate_sample_overheads() when loading and | ||||||||||||||||||||||||||||
| 288 | records the returned values. It then uses the C<sample_overhead_time> to | ||||||||||||||||||||||||||||
| 289 | adjust the L</period_exclusive> time to more accrately reflect the time not | ||||||||||||||||||||||||||||
| 290 | covered by the accumulated samples. Currently the C<sample_inner_time> is | ||||||||||||||||||||||||||||
| 291 | I<not> subtracted from the individual samples. That may change in future. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =cut | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 6 | ▶ | sub estimate_sample_overheads { | ||||||||||||||||||||||||||
|
Subroutine
estimate_sample_overheads: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 296 | 6 | my ($self, $count) = @_; | |||||||||||||||||||||||||||
| 297 | 6 | ▶ | $count ||= 1000; | ||||||||||||||||||||||||||
|
Condition50%1/2
$count ||= 1000
MC/DC0%0/1
$count ||= 1000
$count
Truth table50%1/2
A
A= $count
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 6 | my $profile = __PACKAGE__->new('overhead',{ dbi_profile_class => 'DashProfiler::DumpNowhere' }); | |||||||||||||||||||||||||||
| 300 | 6 | my $sampler = $profile->prepare('c1'); | |||||||||||||||||||||||||||
| 301 | # It's okay that this is a tight loop so will tend to give lower times | ||||||||||||||||||||||||||||
| 302 | # than would be experienced in practice because, while we want to get as | ||||||||||||||||||||||||||||
| 303 | # close as possible to the true overhead, we don't want to overestimate it. | ||||||||||||||||||||||||||||
| 304 | 6 | my ($i, $sum) = ($count, 0); | |||||||||||||||||||||||||||
| 305 | 6 | ▶ | while ($i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 6000 | my $t0 = dbi_time(); # to compare with t1 below | |||||||||||||||||||||||||||
| 307 | 6000 | my $t1 = dbi_time(); # time before sampling | |||||||||||||||||||||||||||
| 308 | 6000 | my $ps1 = $sampler->("c2"); # begin sample | |||||||||||||||||||||||||||
| 309 | 6000 | undef $ps1; # end sample | |||||||||||||||||||||||||||
| 310 | 6000 | $sum += (dbi_time() - $t1) # time to perform full sample lifecycle | |||||||||||||||||||||||||||
| 311 | - ($t1 - $t0); # subtract cost of calling dbi_time() | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | # overhead is average of time spent calling sampler & DESTROY: | ||||||||||||||||||||||||||||
| 314 | 6 | $sample_overhead_time = $sum / $count; # ~0.000014s on 2GHz OS X 10.5.2 laptop | |||||||||||||||||||||||||||
| 315 | 6 | $sample_inner_time = ($profile->get_dbi_profile->{Data}{c1}{c2}[1] / $count); | |||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # we could also subtract the time accumulated by the samples, like this: | ||||||||||||||||||||||||||||
| 318 | # $sample_overhead_time -= $sample_inner_time | ||||||||||||||||||||||||||||
| 319 | # but we don't because that's also a valid part of the overhead | ||||||||||||||||||||||||||||
| 320 | # because there are no statements between the sample creation and destruction. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 6 | warn sprintf "sample_overhead_time=%.7fs (sample_inner_time=%.7fs)\n", | |||||||||||||||||||||||||||
| 323 | $sample_overhead_time, $sample_inner_time if DEBUG(); | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 6 | $profile->reset_profile_data; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 6 | ▶ | return $sample_overhead_time unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 6 | return ($sample_overhead_time, $sample_inner_time); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =head1 OBJECT METHODS | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head2 attach_dbi_profile | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | $core->attach_dbi_profile( $dbi_profile, $name ); | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Attaches a DBI Profile to a DashProfiler::Core object using the $name given. | ||||||||||||||||||||||||||||
| 340 | Any later samples are also aggregated into this DBI Profile. | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Not normally called directly. The new() method calls attach_dbi_profile() to | ||||||||||||||||||||||||||||
| 343 | attach the "main" profile and the C<period_summary> profile, if enabled. | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | The $dbi_profile argument can be either a DBI::Profile object or a string | ||||||||||||||||||||||||||||
| 346 | containing a DBI::Profile specification. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | The get_dbi_profile($name) method can be used to retrieve the profile. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 13 | ▶ | sub attach_dbi_profile { | ||||||||||||||||||||||||||
|
Subroutine
attach_dbi_profile: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 353 | 13 | my ($self, $dbi_profile, $dbi_profile_name, $weakly) = @_; | |||||||||||||||||||||||||||
| 354 | # wrap DBI::Profile object/spec with a DBI handle | ||||||||||||||||||||||||||||
| 355 | 13 | ▶ | croak "No dbi_profile_name specified" unless defined $dbi_profile_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 13 | local $ENV{DBI_AUTOPROXY}; | |||||||||||||||||||||||||||
| 357 | 13 | my $dbh = DBI->connect("dbi:DashProfiler:", "", "", { | |||||||||||||||||||||||||||
| 358 | Profile => $dbi_profile, | ||||||||||||||||||||||||||||
| 359 | RaiseError => 1, PrintError => 1, TraceLevel => 0, | ||||||||||||||||||||||||||||
| 360 | }); | ||||||||||||||||||||||||||||
| 361 | 13 | $dbh = tied %$dbh; # switch to inner handle | |||||||||||||||||||||||||||
| 362 | 13 | $dbh->{Profile}->empty; # discard FETCH&STOREs etc due to connect() | |||||||||||||||||||||||||||
| 363 | 13 | for my $handles ($self->{dbi_handles_all}, $self->{dbi_handles_active}) { | |||||||||||||||||||||||||||
| 364 | # clean out any dead weakrefs | ||||||||||||||||||||||||||||
| 365 | 26 | ▶ | defined $handles->{$_} or delete $handles->{$_} for keys %$handles; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 26 | $handles->{$dbi_profile_name} = $dbh; | |||||||||||||||||||||||||||
| 367 | # weaken($handles->{$dbi_profile_name}) if $weakly; # not currently documented or used | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | 13 | return $dbh; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub _attach_new_temporary_plain_profile { # not currently documented or used | ||||||||||||||||||||||||||
|
Subroutine
_attach_new_temporary_plain_profile: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 374 | 0 | my ($self, $dbi_profile_name) = @_; | |||||||||||||||||||||||||||
| 375 | # create new DBI profile (with no time key) that doesn't flush anywhere | ||||||||||||||||||||||||||||
| 376 | 0 | my $dbi_profile = $self->_mk_dbi_profile("DashProfiler::DumpNowhere", 0); | |||||||||||||||||||||||||||
| 377 | # attach to the profile, but only weakly | ||||||||||||||||||||||||||||
| 378 | 0 | $self->attach_dbi_profile( $dbi_profile, $dbi_profile_name, 1 ); | |||||||||||||||||||||||||||
| 379 | # return ref so caller can store till ready to discard | ||||||||||||||||||||||||||||
| 380 | 0 | return $dbi_profile; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 13 | ▶ | sub _mk_dbi_profile { | ||||||||||||||||||||||||||
|
Subroutine
_mk_dbi_profile: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 385 | 13 | my ($self, $class, $granularity) = @_; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 13 | _load_class($class); | |||||||||||||||||||||||||||
| 388 | 13 | ▶ | my $Path = $granularity ? [ "!Time~$granularity", "!Statement", "!MethodName" ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | : [ "!Statement", "!MethodName" ]; | ||||||||||||||||||||||||||||
| 390 | my $dbi_profile = $class->new( | ||||||||||||||||||||||||||||
| 391 | Path => $Path, | ||||||||||||||||||||||||||||
| 392 | Quiet => 1, | ||||||||||||||||||||||||||||
| 393 | Trace => 0, | ||||||||||||||||||||||||||||
| 394 | File => "dashprofile.$self->{profile_name}", | ||||||||||||||||||||||||||||
| 395 | 13 | %{ $self->{dbi_profile_args} }, | |||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 13 | return $dbi_profile; | |||||||||||||||||||||||||||
| 399 | }; | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =head2 get_dbi_profile | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | $dbi_profile = $core->get_dbi_profile( $dbi_profile_name ); | ||||||||||||||||||||||||||||
| 405 | @dbi_profiles = $core->get_dbi_profile( '*' ); | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | Returns a reference to the DBI Profile object that attached to the $core with the given name. | ||||||||||||||||||||||||||||
| 408 | If $dbi_profile_name is undef then it defaults to "main". | ||||||||||||||||||||||||||||
| 409 | Returns undef if there's no profile with that name atached. | ||||||||||||||||||||||||||||
| 410 | If $dbi_profile_name is 'C<*>' then it returns all attached profiles. | ||||||||||||||||||||||||||||
| 411 | See L</attach_dbi_profile>. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =cut | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 55 | ▶ | sub get_dbi_profile { | ||||||||||||||||||||||||||
|
Subroutine
get_dbi_profile: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 55 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 417 | my $dbi_handles = $self->{dbi_handles_all} | ||||||||||||||||||||||||||||
| 418 | 55 | ▶ | or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | # we take care to avoid auto-viv here | ||||||||||||||||||||||||||||
| 420 | 55 | ▶ | my $dbh = $dbi_handles->{ $name || 'main' }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$name || 'main'
MC/DC100%1/1
$name || 'main'
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||
| 421 | 55 | ▶ | return $dbh->{Profile} if $dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | 13 | ▶ | return unless $name && $name eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$name and $name eq "*"
MC/DC50%1/2
$name and $name eq "*"
$name
$name eq "*"
Truth table66%2/3
A and B
A= $nameB= $name eq "*"
| |||||||||||||||||||||||||||||
| 423 | 9 | ▶ | croak "get_dbi_profile('*') called in scalar context" unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | return map { | ||||||||||||||||||||||||||||
| 425 | 9 | ▶ | ($_->{Profile}) ? ($_->{Profile}) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | } values %$dbi_handles; | ||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head2 profile_as_text | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | $core->profile_as_text(); | ||||||||||||||||||||||||||||
| 433 | $core->profile_as_text( $dbi_profile_name ); | ||||||||||||||||||||||||||||
| 434 | $core->profile_as_text( $dbi_profile_name, { | ||||||||||||||||||||||||||||
| 435 | path => [ $self->{profile_name} ], | ||||||||||||||||||||||||||||
| 436 | format => '%1$s: dur=%11$f count=%10$d (max=%14$f avg=%2$f)'."\n", | ||||||||||||||||||||||||||||
| 437 | separator => ">", | ||||||||||||||||||||||||||||
| 438 | } ); | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Returns the aggregated data from the specified DBI Profile (default "main") formatted as a string. | ||||||||||||||||||||||||||||
| 441 | Calls L</get_dbi_profile> to get the DBI Profile, then calls the C<as_text> method on the profile. | ||||||||||||||||||||||||||||
| 442 | See L<DBI::Profile> for more details of the parameters. | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | In list context it returns one item per profile leaf node, in scalar context | ||||||||||||||||||||||||||||
| 445 | they're concatenated into a single string. Returns undef if the named DBI | ||||||||||||||||||||||||||||
| 446 | Profile doesn't exist. | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 17 | ▶ | sub profile_as_text { | ||||||||||||||||||||||||||
|
Subroutine
profile_as_text: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 451 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 452 | 17 | my $name = shift; | |||||||||||||||||||||||||||
| 453 | 17 | ▶ | my $default_args = $self->{profile_as_text_args} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'profile_as_text_args'} || {}
MC/DC0%0/1
$self->{'profile_as_text_args'} || {}
$self->{'profile_as_text_args'}
Truth table50%1/2
A
A= $self->{'profile_as_text_args'}
| |||||||||||||||||||||||||||||
| 454 | 17 | ▶ | my %args = (%$default_args, %{ shift || {} }); | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 17 | ▶ | $args{path} ||= [ $self->{profile_name} ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"path"} ||= [$self->{"profile_name"}]
MC/DC0%0/1
$args{"path"} ||= [$self->{"profile_name"}]
$args{"path"}
Truth table50%1/2
A
A= $args{"path"}
| |||||||||||||||||||||||||||||
| 457 | 17 | ▶ | $args{format} ||= '%1$s: dur=%11$f count=%10$d (max=%14$f avg=%2$f)'."\n"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"format"} ||= "%1\$s: dur=%11\$f count=%10\$d (max=%14\$f avg=%2\$f)\n"
MC/DC100%1/1
$args{"format"} ||= "%1\$s: dur=%11\$f count=%10\$d (max=%14\$f avg=%2\$f)\n"
$args{"format"}
Truth table100%2/2
A
A= $args{"format"}
| |||||||||||||||||||||||||||||
| 458 | 17 | ▶ | $args{separator} ||= ">"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"separator"} ||= ">"
MC/DC100%1/1
$args{"separator"} ||= ">"
$args{"separator"}
Truth table100%2/2
A
A= $args{"separator"}
| |||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | 17 | ▶ | my $dbi_profile = $self->get_dbi_profile($name) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 15 | return $dbi_profile->as_text(\%args); | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head2 reset_profile_data | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | $core->reset_profile_data( $dbi_profile_name ); | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | Resets (discards) DBI Profile data and resets the period count to 0. | ||||||||||||||||||||||||||||
| 470 | If $dbi_profile_name is false then it defaults to "main". | ||||||||||||||||||||||||||||
| 471 | If $dbi_profile_name is "*" then all attached profiles are reset. | ||||||||||||||||||||||||||||
| 472 | Returns a list of the affected DBI::Profile objects. | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | =cut | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 16 | ▶ | sub reset_profile_data { | ||||||||||||||||||||||||||
|
Subroutine
reset_profile_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 477 | 16 | my ($self, $dbi_profile_name) = @_; | |||||||||||||||||||||||||||
| 478 | 16 | my @dbi_profiles = $self->get_dbi_profile($dbi_profile_name); | |||||||||||||||||||||||||||
| 479 | 16 | $_->empty for @dbi_profiles; | |||||||||||||||||||||||||||
| 480 | 16 | $self->{period_count} = 0; | |||||||||||||||||||||||||||
| 481 | 16 | return @dbi_profiles; | |||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 6 | ▶ | sub _visit_nodes { # depth first with lexical ordering | ||||||||||||||||||||||||||
|
Subroutine
_visit_nodes: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 486 | 6 | my ($self, $node, $path, $sub) = @_; | |||||||||||||||||||||||||||
| 487 | 6 | ▶ | croak "No sub ref given" unless ref $sub eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | 6 | ▶ | return unless $node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | 6 | ▶ | $path ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$path ||= []
MC/DC100%1/1
$path ||= []
$path
Truth table100%2/2
A
A= $path
| |||||||||||||||||||||||||||||
| 490 | 6 | ▶ | if (ref $node eq 'HASH') { # recurse | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 4 | $path = [ @$path, undef ]; | |||||||||||||||||||||||||||
| 492 | return map { | ||||||||||||||||||||||||||||
| 493 | 4 | $path->[-1] = $_; | |||||||||||||||||||||||||||
| 494 | 5 | ▶ | ($node->{$_}) ? $self->_visit_nodes($node->{$_}, $path, $sub) : () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 495 | } sort keys %$node; | ||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||
| 497 | 2 | return $sub->($node, $path); | |||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | =head2 visit_profile_nodes | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | $core->visit_profile_nodes( $dbi_profile_name, sub { ... } ) | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | Calls the given subroutine for each leaf node in the named DBI Profile. | ||||||||||||||||||||||||||||
| 506 | The name defaults to "main". If $dbi_profile_name is "*" then the leafs nodes | ||||||||||||||||||||||||||||
| 507 | in all the attached profiles are visited. | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =cut | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 1 | ▶ | sub visit_profile_nodes { | ||||||||||||||||||||||||||
|
Subroutine
visit_profile_nodes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 512 | 1 | my ($self, $dbi_profile_name, $sub) = @_; | |||||||||||||||||||||||||||
| 513 | 1 | my @dbi_profiles = $self->get_dbi_profile($dbi_profile_name); | |||||||||||||||||||||||||||
| 514 | 1 | for my $dbi_profile (@dbi_profiles) { | |||||||||||||||||||||||||||
| 515 | my $data = $dbi_profile->{Data} | ||||||||||||||||||||||||||||
| 516 | 1 | ▶ | or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 517 | 1 | $self->_visit_nodes($data, undef, $sub) | |||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||
| 519 | 1 | return; | |||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =head2 propagate_period_count | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | $core->propagate_period_count( $dbi_profile_name ) | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | Sets the count field of all the leaf-nodes in the named DBI Profile to the | ||||||||||||||||||||||||||||
| 528 | number of times start_sample_period() has been called since the last flush() or | ||||||||||||||||||||||||||||
| 529 | reset_profile_data(). | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | If $dbi_profile_name is "*" then counts in all attached profiles are set. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | Resets the period count used. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Does nothing but return 0 if the the period count is zero. | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | This method is especially useful where the number of sample I<periods> are much | ||||||||||||||||||||||||||||
| 538 | more relevant than the number of samples. This is typically the case where | ||||||||||||||||||||||||||||
| 539 | sample periods correspond to major units of work, such as web requests. | ||||||||||||||||||||||||||||
| 540 | Using propagate_period_count() lets you calculate averages based on the count | ||||||||||||||||||||||||||||
| 541 | of I<periods> instead of samples. | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | Imagine, for example, that you're instrumenting a web application and you have | ||||||||||||||||||||||||||||
| 544 | a function that sends a request to some network service and another reads each | ||||||||||||||||||||||||||||
| 545 | line of the response. You'd add DashProfiler sampler calls to each function. | ||||||||||||||||||||||||||||
| 546 | The number of samples recorded in the leaf node will depends on the number of | ||||||||||||||||||||||||||||
| 547 | lines in the response from the network service. You're much more likely to want | ||||||||||||||||||||||||||||
| 548 | to know "average total time spent handling the network service per http request" | ||||||||||||||||||||||||||||
| 549 | than "average time spent in a network service related function". | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | This method is typically called just before a flush(), often via C<flush_hook>. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | =cut | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 3 | ▶ | sub propagate_period_count { | ||||||||||||||||||||||||||
|
Subroutine
propagate_period_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 556 | 3 | my ($self, $dbi_profile_name) = @_; | |||||||||||||||||||||||||||
| 557 | # force count of all nodes to be count of periods instead of samples | ||||||||||||||||||||||||||||
| 558 | my $count = $self->{period_count} | ||||||||||||||||||||||||||||
| 559 | 3 | ▶ | or return 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | 1 | warn "propagate_period_count $self->{profile_name} count $count\n" if DEBUG(); | |||||||||||||||||||||||||||
| 561 | # force count of all nodes to be count of periods | ||||||||||||||||||||||||||||
| 562 | 1 | ▶ | $self->visit_profile_nodes($dbi_profile_name, sub { return unless ref $_[0] eq 'ARRAY'; $_[0]->[0] = $count }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 1 | return $count; | |||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | =head2 flush | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | $core->flush() | ||||||||||||||||||||||||||||
| 570 | $core->flush( $dbi_profile_name ) | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | Calls the C<flush_hook> code reference, if set, passing it $core and the | ||||||||||||||||||||||||||||
| 573 | $dbi_profile_name augument (which is typically undef). If the C<flush_hook> | ||||||||||||||||||||||||||||
| 574 | code returns a non-empty list then flush() does nothing more except return that | ||||||||||||||||||||||||||||
| 575 | list. | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | If C<flush_hook> wasn't set, or it returned an empty list, then the flush_to_disk() | ||||||||||||||||||||||||||||
| 578 | method is called for the named DBI Profile (defaults to "main", use "*" for all). | ||||||||||||||||||||||||||||
| 579 | In this case flush() returns a list of the DBI::Profile objects flushed. | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | =cut | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 2 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 585 | 2 | my ($self, $dbi_profile_name) = @_; | |||||||||||||||||||||||||||
| 586 | 2 | ▶ | if (my $flush_hook = $self->{flush_hook}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 587 | # if flush_hook returns true then don't call flush_to_disk | ||||||||||||||||||||||||||||
| 588 | 1 | my @ret = $flush_hook->($self, $dbi_profile_name); | |||||||||||||||||||||||||||
| 589 | 1 | ▶ | return @ret if @ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 590 | # else fall through | ||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | 1 | my @dbi_profiles = $self->get_dbi_profile($dbi_profile_name); | |||||||||||||||||||||||||||
| 593 | 1 | $_->flush_to_disk for (@dbi_profiles); | |||||||||||||||||||||||||||
| 594 | 1 | return @dbi_profiles; | |||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | =head2 flush_if_due | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | $core->flush_if_due() | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | Returns nothing if C<flush_interval> was not set. | ||||||||||||||||||||||||||||
| 603 | Returns nothing if C<flush_interval> was set but insufficient time has passed since | ||||||||||||||||||||||||||||
| 604 | the last call to flush_if_due(). | ||||||||||||||||||||||||||||
| 605 | Otherwise notes the time the next flush will be due, and calls C<return flush();>. | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =cut | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | 1 | ▶ | sub flush_if_due { | ||||||||||||||||||||||||||
|
Subroutine
flush_if_due: called CC=3 SCAR=13.3
Pod
documented
| |||||||||||||||||||||||||||||
| 610 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 611 | 1 | ▶ | return unless $self->{flush_interval}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 612 | 1 | ▶ | return if time() < $self->{flush_due_at_time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 613 | 0 | $self->{flush_due_at_time} = time() + $self->{flush_interval}; | |||||||||||||||||||||||||||
| 614 | 0 | return $self->flush(); | |||||||||||||||||||||||||||
| 615 | } | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head2 has_profile_data | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | $bool = $core->has_profile_data | ||||||||||||||||||||||||||||
| 621 | $bool = $core->has_profile_data( $dbi_profile_name ) | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | Returns true if the named DBI Profile (default "main") contains any profile data. | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | =cut | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 9 | ▶ | sub has_profile_data { | ||||||||||||||||||||||||||
|
Subroutine
has_profile_data: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 628 | 9 | my ($self, $dbi_profile_name) = @_; | |||||||||||||||||||||||||||
| 629 | 9 | ▶ | my @dbi_profiles = $self->get_dbi_profile($dbi_profile_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 630 | or return undef; ## no critic | ||||||||||||||||||||||||||||
| 631 | 9 | ▶ | keys %{$_->{Data}||{}} && return 1 for (@dbi_profiles); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_->{'Data'} || {}
MC/DC100%1/1
$_->{'Data'} || {}
$_->{'Data'}
Truth table100%2/2
A
A= $_->{'Data'}
| |||||||||||||||||||||||||||||
| 632 | 8 | return 0; | |||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =head2 start_sample_period | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | $core->start_sample_period | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | Marks the start of a series of related samples, e.g, within one http request. | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | If end_sample_period() has not been called for this core since the last | ||||||||||||||||||||||||||||
| 643 | start_sample_period() then the value of the C<period_strict_start> attribute | ||||||||||||||||||||||||||||
| 644 | determines the actions taken: | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | 0 = restart the period, silently | ||||||||||||||||||||||||||||
| 647 | 1 = restart the period and issue a warning (this is the default) | ||||||||||||||||||||||||||||
| 648 | 2 = continue the current period, silently | ||||||||||||||||||||||||||||
| 649 | 3 = continue the current period and issue a warning | ||||||||||||||||||||||||||||
| 650 | 4 = call end_sample_period(), silently | ||||||||||||||||||||||||||||
| 651 | 5 = call end_sample_period() and issue a warning | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | If the value is a CODE ref then it's called (and passed $core) and the return value used. | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | Resets the C<period_accumulated> attribute to zero. | ||||||||||||||||||||||||||||
| 656 | Sets C<period_start_time> to the current dbi_time(). | ||||||||||||||||||||||||||||
| 657 | If C<period_summary> is enabled then the period_summary DBI Profile is enabled and reset. | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | See also L</end_sample_period>, the C<period_summary> option, and L</propagate_period_count>. | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | =cut | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | 7 | ▶ | sub start_sample_period { | ||||||||||||||||||||||||||
|
Subroutine
start_sample_period: called CC=8 SCAR=30.6
Pod
documented
| |||||||||||||||||||||||||||||
| 664 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 665 | # marks the start of a series of related samples, e.g, within one http request | ||||||||||||||||||||||||||||
| 666 | # see end_sample_period() | ||||||||||||||||||||||||||||
| 667 | 7 | ▶ | if ($self->{period_start_time}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 668 | 0 | ▶ | if (my $strictness = $self->{period_strict_start}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 669 | 0 | ▶ | $strictness = $strictness->($self) if ref $strictness eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 670 | 0 | ▶ | carp "start_sample_period() called for $self->{profile_name} without preceeding end_sample_period()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 671 | if $strictness & 0x01; | ||||||||||||||||||||||||||||
| 672 | return | ||||||||||||||||||||||||||||
| 673 | 0 | ▶ | if $strictness & 0x02; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 674 | 0 | ▶ | $self->end_sample_period() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 675 | if $strictness & 0x04; | ||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||
| 678 | 7 | ▶ | if (my $period_summary_h = $self->{dbi_handles_all}{period_summary}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 679 | # ensure period_summary_h dbi profile will receive samples | ||||||||||||||||||||||||||||
| 680 | 2 | $self->{dbi_handles_active}{period_summary} = $period_summary_h; | |||||||||||||||||||||||||||
| 681 | 2 | $period_summary_h->{Profile}->empty; # start period empty | |||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||
| 683 | 7 | $self->{period_accumulated} = 0; | |||||||||||||||||||||||||||
| 684 | 7 | $self->{period_start_time} = dbi_time(); | |||||||||||||||||||||||||||
| 685 | 7 | return; | |||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | =head2 end_sample_period | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | $core->end_sample_period | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | Marks the end of a series of related samples, e.g, within one http request. | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | If start_sample_period() has not been called for this core since the last | ||||||||||||||||||||||||||||
| 696 | end_sample_period() (or the start of the script) then the value of the | ||||||||||||||||||||||||||||
| 697 | C<period_strict_end> attribute determines the actions taken: | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | 0 = do nothing, silently (this is the default) | ||||||||||||||||||||||||||||
| 700 | 1 = do nothing but warn | ||||||||||||||||||||||||||||
| 701 | 2 = call start_sample_period(), silently | ||||||||||||||||||||||||||||
| 702 | 3 = call start_sample_period() and warn | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | If the value is a CODE ref then it's called (and passed $core) and the return value used. | ||||||||||||||||||||||||||||
| 705 | If start_sample_period() isn't called then end_sample_period() just returns. | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | The C<period_count> attribute is incremented. | ||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||
| 709 | If C<period_exclusive> is enabled then a sample is added with a duration | ||||||||||||||||||||||||||||
| 710 | caclulated to be the time since start_sample_period() was called to now, minus | ||||||||||||||||||||||||||||
| 711 | the time accumulated by samples since start_sample_period() was called. | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | Resets the C<period_start_time> attribute to 0. If C<period_summary> is | ||||||||||||||||||||||||||||
| 714 | enabled then the C<period_summary> DBI Profile is disabled and returned, else | ||||||||||||||||||||||||||||
| 715 | undef is returned. | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | See also L</start_sample_period>, C<period_summary> and L</propagate_period_count>. | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | =cut | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | 7 | ▶ | sub end_sample_period { | ||||||||||||||||||||||||||
|
Subroutine
end_sample_period: called CC=9 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||
| 722 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | 7 | ▶ | if (not $self->{period_start_time}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 725 | 0 | ▶ | if (my $strictness = $self->{period_strict_end}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 726 | 0 | ▶ | $strictness = $strictness->($self) if ref $strictness eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 727 | 0 | ▶ | carp "end_sample_period() called for $self->{profile_name} without preceeding start_sample_period()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 728 | if $strictness & 0x01; | ||||||||||||||||||||||||||||
| 729 | 0 | ▶ | $self->start_sample_period() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 730 | if $strictness & 0x02; | ||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||
| 732 | # return if we didn't start a period | ||||||||||||||||||||||||||||
| 733 | 0 | ▶ | return if not $self->{period_start_time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||
| 736 | 7 | $self->{period_count}++; | |||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | # disconnect period_summary dbi profile from receiving any more samples | ||||||||||||||||||||||||||||
| 739 | 7 | my $period_summary_dbh = delete $self->{dbi_handles_active}{period_summary}; | |||||||||||||||||||||||||||
| 740 | 7 | my $period_summary_profile = $period_summary_dbh->{Profile}; | |||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | 7 | ▶ | if (my $exclusive_sampler = $self->{exclusive_sampler}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 743 | # Calculate how much time between $self->{period_start_time} and now | ||||||||||||||||||||||||||||
| 744 | # is not accounted for by $self->{period_accumulated}. | ||||||||||||||||||||||||||||
| 745 | # Add a sample with the start time forced to be period_start_time | ||||||||||||||||||||||||||||
| 746 | # shifted forward by the accumulated sample durations + sampling overheads. | ||||||||||||||||||||||||||||
| 747 | # This accounts for all the time between start_sample_period and | ||||||||||||||||||||||||||||
| 748 | # end_sample_period that hasn't been accounted for by normal samples. | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | # calculate overhead of taking samples | ||||||||||||||||||||||||||||
| 751 | 4 | my $overhead; | |||||||||||||||||||||||||||
| 752 | 4 | ▶ | if ($period_summary_profile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 753 | # if period_summary is enabled then we can use the count of | ||||||||||||||||||||||||||||
| 754 | # samples this period to scale the overhead correctly | ||||||||||||||||||||||||||||
| 755 | 0 | dbi_profile_merge(my $total=[], $period_summary_profile->{Data}); | |||||||||||||||||||||||||||
| 756 | # scale overhead by number of samples in period | ||||||||||||||||||||||||||||
| 757 | 0 | $overhead = $sample_overhead_time * $total->[0]; | |||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||
| 759 | else { | ||||||||||||||||||||||||||||
| 760 | # if period_summary is not enabled then we can't do much | ||||||||||||||||||||||||||||
| 761 | 4 | $overhead = $sample_overhead_time; | |||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | warn sprintf "%s period end: overhead %.6fs (%.0f * %.6fs)\n", | ||||||||||||||||||||||||||||
| 765 | 4 | $self->{profile_name}, $overhead, $overhead/$sample_overhead_time, $sample_overhead_time | |||||||||||||||||||||||||||
| 766 | if DEBUG() && DEBUG() >= 3; | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | 4 | $exclusive_sampler->(undef, $self->{period_start_time} + $self->{period_accumulated} + $overhead); | |||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | # sample gets destroyed, and so counted, immediately. | ||||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | 7 | $self->{period_start_time} = 0; | |||||||||||||||||||||||||||
| 774 | # $self->{period_accumulated} will be reset by start_sample_period() | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | 7 | return $period_summary_profile; | |||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | =head2 period_start_time | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | $time = $core->period_start_time; | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | Returns the time the current sample period was started (typically the time | ||||||||||||||||||||||||||||
| 785 | L</start_sample_period> was called) or 0 if there's no period active. | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | =cut | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | 3 | ▶ | sub period_start_time { | ||||||||||||||||||||||||||
|
Subroutine
period_start_time: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 790 | 3 | return shift->{period_start_time}; | |||||||||||||||||||||||||||
| 791 | } | ||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | =head2 prepare | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | $sampler_code_ref = $core->prepare( $context1 ) | ||||||||||||||||||||||||||||
| 797 | $sampler_code_ref = $core->prepare( $context1, $context2 ) | ||||||||||||||||||||||||||||
| 798 | $sampler_code_ref = $core->prepare( $context1, $context2, %meta ) | ||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||
| 800 | $sampler_code_ref->( $context2 ) | ||||||||||||||||||||||||||||
| 801 | $sampler_code_ref->( $context2, $start_time ) | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | Returns a reference to a subroutine that will create sampler objects. | ||||||||||||||||||||||||||||
| 804 | In effect the prepare() method creates a 'factory'. | ||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||
| 806 | The sampler objects created by the returned code reference are pre-set to use | ||||||||||||||||||||||||||||
| 807 | $context1, and optionally $context2, as their context values. | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | If the appropriate value for C<context2> won't be available until the end of | ||||||||||||||||||||||||||||
| 810 | the sample you can set $context2 to a code reference. The reference will be | ||||||||||||||||||||||||||||
| 811 | executed at the end of the sample. See L<DashProfiler::Sample>. | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | XXX needs more info about %meta - see the code for now, it's not very complex. | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | See L<DashProfiler::Sample> for more information. | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | =cut | ||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | 15 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 820 | 15 | my ($self, $context1, $context2, %meta) = @_; | |||||||||||||||||||||||||||
| 821 | # return undef if profile exists but is disabled | ||||||||||||||||||||||||||||
| 822 | 15 | ▶ | return undef if $self->{disabled}; ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||
| 824 | # return a light wrapper around the profile, containing the context1 | ||||||||||||||||||||||||||||
| 825 | 15 | ▶ | my $sample_class = $meta{sample_class} || $self->{sample_class}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$meta{'sample_class'} || $self->{'sample_class'}
MC/DC0%0/2
$meta{'sample_class'} || $self->{'sample_class'}
$meta{'sample_class'}
$self->{'sample_class'}
Truth table33%1/3
A || B
A= $meta{'sample_class'}B= $self->{'sample_class'}
| |||||||||||||||||||||||||||||
| 826 | # use %meta to carry context info into sample object factory | ||||||||||||||||||||||||||||
| 827 | 15 | $meta{_dash_profile} = $self; | |||||||||||||||||||||||||||
| 828 | 15 | $meta{_context1} = $context1; | |||||||||||||||||||||||||||
| 829 | 15 | $meta{_context2} = $context2; | |||||||||||||||||||||||||||
| 830 | # skip method lookup | ||||||||||||||||||||||||||||
| 831 | 15 | ▶ | my $coderef = $sample_class->can("new") || "new"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$sample_class->can('new') || 'new'
MC/DC0%0/1
$sample_class->can('new') || 'new'
$sample_class->can('new')
Truth table50%1/2
A
A= $sample_class->can('new')
| |||||||||||||||||||||||||||||
| 832 | 9213 | ▶ | return sub { # closure over $sample_class, %meta and $coderef | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 833 | 9213 | $sample_class->$coderef(\%meta, @_) | |||||||||||||||||||||||||||
| 834 | 15 | }; | |||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||
| 838 | 9 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 839 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 840 | # global destruction shouldn't be relied upon because often the | ||||||||||||||||||||||||||||
| 841 | # dbi profile data will have been already destroyed | ||||||||||||||||||||||||||||
| 842 | 9 | ▶ | $self->end_sample_period() if $self->{period_start_time}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 843 | 9 | ▶ | $self->flush if $self->has_profile_data("*"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 844 | } | ||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||
| 847 | 25 | ▶ | sub _load_class { | ||||||||||||||||||||||||||
|
Subroutine
_load_class: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 848 | 25 | my ($class) = @_; | |||||||||||||||||||||||||||
| 849 | ## no critic | ||||||||||||||||||||||||||||
| 850 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 851 | 25 | ▶ | return 1 if keys %{"$class\::"}; # already loaded | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 852 | 5 | (my $file = $class) =~ s/::/\//g; | |||||||||||||||||||||||||||
| 853 | 5 | require "$file.pm"; | |||||||||||||||||||||||||||
| 854 | } | ||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||
| 857 | =head2 DEBUG | ||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||
| 859 | The DEBUG subroutine is a constant that returns whatever the value of | ||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||
| 861 | $ENV{DASHPROFILER_CORE_DEBUG} || $ENV{DASHPROFILER_DEBUG} || 0; | ||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||
| 863 | was when the modle was loaded. | ||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||
| 865 | =cut | ||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||
| 869 | # --- DBI::ProfileDumper subclass that doesn't flush_to_disk | ||||||||||||||||||||||||||||
| 870 | # Used by period_summary | ||||||||||||||||||||||||||||
| 871 | { | ||||||||||||||||||||||||||||
| 872 | package DashProfiler::DumpNowhere; | ||||||||||||||||||||||||||||
| 873 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 874 | 5 | ▶ | use base qw(DBI::ProfileDumper); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 875 | 7 | ▶ | sub flush_to_disk { return } | ||||||||||||||||||||||||||
|
Subroutine
flush_to_disk: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 876 | } | ||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | # --- ultra small 'null' driver for DBI --- | ||||||||||||||||||||||||||||
| 880 | # This is really just for the custom dbh DESTROY method below | ||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||
| 882 | { | ||||||||||||||||||||||||||||
| 883 | 5 | package DBD::DashProfiler; | |||||||||||||||||||||||||||
| 884 | 5 | our $drh; # holds driver handle once initialised | |||||||||||||||||||||||||||
| 885 | 5 | ▶ | sub driver{ | ||||||||||||||||||||||||||
|
Subroutine
driver: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 886 | 5 | ▶ | return $drh if $drh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 887 | 5 | my ($class, $attr) = @_; | |||||||||||||||||||||||||||
| 888 | 5 | $DBD::DashProfiler::db::imp_data_size = 0; | |||||||||||||||||||||||||||
| 889 | 5 | $DBD::DashProfiler::dr::imp_data_size = 0; | |||||||||||||||||||||||||||
| 890 | 5 | return DBI::_new_drh($class."::dr", { | |||||||||||||||||||||||||||
| 891 | Name => 'DashProfiler', Version => $DashProfiler::Core::VERSION, | ||||||||||||||||||||||||||||
| 892 | }); | ||||||||||||||||||||||||||||
| 893 | } | ||||||||||||||||||||||||||||
| 894 | 0 | ▶ | sub CLONE { undef $drh } | ||||||||||||||||||||||||||
|
Subroutine
CLONE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 895 | } | ||||||||||||||||||||||||||||
| 896 | 5 | { package DBD::DashProfiler::dr; | |||||||||||||||||||||||||||
| 897 | 5 | our $imp_data_size = 0; | |||||||||||||||||||||||||||
| 898 | 0 | ▶ | sub DESTROY { undef } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||
| 900 | 5 | { package DBD::DashProfiler::db; | |||||||||||||||||||||||||||
| 901 | 5 | our $imp_data_size = 0; | |||||||||||||||||||||||||||
| 902 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 903 | 91 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 904 | 91 | my ($dbh, $attrib, $value) = @_; | |||||||||||||||||||||||||||
| 905 | 91 | ▶ | $value = ($value) ? -901 : -900 if $attrib eq 'AutoCommit'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 906 | 91 | return $dbh->SUPER::STORE($attrib, $value); | |||||||||||||||||||||||||||
| 907 | } | ||||||||||||||||||||||||||||
| 908 | 10 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 909 | 10 | my $dbh = shift; | |||||||||||||||||||||||||||
| 910 | 10 | $dbh->{Profile} = undef; # don't profile the DESTROY | |||||||||||||||||||||||||||
| 911 | 10 | return $dbh->SUPER::DESTROY; | |||||||||||||||||||||||||||
| 912 | } | ||||||||||||||||||||||||||||
| 913 | } | ||||||||||||||||||||||||||||
| 914 | 5 | { package DBD::DashProfiler::st; | |||||||||||||||||||||||||||
| 915 | 5 | our $imp_data_size = 0; | |||||||||||||||||||||||||||
| 916 | } | ||||||||||||||||||||||||||||
| 917 | # fake the %INC entry so DBI install_driver won't try to load it | ||||||||||||||||||||||||||||
| 918 | 5 | ▶ | BEGIN { $INC{"DBD/DashProfiler.pm"} = __FILE__ } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||
| 922 | 5 | 1; | |||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||
| 924 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||
| 926 | DashProfiler by Tim Bunce, L<http://www.tim.bunce.name> and | ||||||||||||||||||||||||||||
| 927 | L<http://blog.timbunce.org> | ||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||
| 929 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||
| 931 | The DashProfiler distribution is Copyright (c) 2007-2008 Tim Bunce. Ireland. | ||||||||||||||||||||||||||||
| 932 | All rights reserved. | ||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||
| 934 | You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||
| 935 | License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||
| 937 | =cut | ||||||||||||||||||||||||||||
| 938 | |||||||||||||||||||||||||||||