blib/lib/DBI/ProfileData.pm

filter: Statementstmt 75.6% 165 / 218 Branchbran 54.5% 48 / 88 Conditioncond 20.8% 5 / 24 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 81.8% 18 / 22 Podpod 91.6% 11 / 12 totaltotal 65.5% 249 / 380 CC 60 SCAR 24.4
CC
60
Cov
66%
format
30.0
report
20.0
_read_files
15.7
CRAP 11.5
« blib/lib/Win32/DBIODBC.pm ↑ Summary blib/lib/DBI/ProfileDumper.pm »
1 package DBI::ProfileData;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 DBI::ProfileData - manipulate DBI::ProfileDumper data dumps
8  
9 =head1 SYNOPSIS
10  
11 The easiest way to use this module is through the dbiprof frontend
12 (see L<dbiprof> for details):
13  
14 dbiprof --number 15 --sort count
15  
16 This module can also be used to roll your own profile analysis:
17  
18 local $DBI::ProfileData::MAX_PATH_DEPTH = 256; # override the maximum path depth of files
19  
20 # load data from dbi.prof
21 $prof = DBI::ProfileData->new(File => "dbi.prof");
22  
23 # get a count of the records (unique paths) in the data set
24 $count = $prof->count();
25  
26 # sort by longest overall time
27 $prof->sort(field => "longest");
28  
29 # sort by longest overall time, least to greatest
30 $prof->sort(field => "longest", reverse => 1);
31  
32 # exclude records with key2 eq 'disconnect'
33 $prof->exclude(key2 => 'disconnect');
34  
35 # exclude records with key1 matching /^UPDATE/i
36 $prof->exclude(key1 => qr/^UPDATE/i);
37  
38 # remove all records except those where key1 matches /^SELECT/i
39 $prof->match(key1 => qr/^SELECT/i);
40  
41 # produce a formatted report with the given number of items
42 $report = $prof->report(number => 10);
43  
44 # clone the profile data set
45 $clone = $prof->clone();
46  
47 # get access to hash of header values
48 $header = $prof->header();
49  
50 # get access to sorted array of nodes
51 $nodes = $prof->nodes();
52  
53 # format a single node in the same style as report()
54 $text = $prof->format($nodes->[0]);
55  
56 # get access to Data hash in DBI::Profile format
57 $Data = $prof->Data();
58  
59 =head1 DESCRIPTION
60  
61 This module offers the ability to read, manipulate and format
62 L<DBI::ProfileDumper> profile data.
63  
64 Conceptually, a profile consists of a series of records, or nodes,
65 each of each has a set of statistics and set of keys. Each record
66 must have a unique set of keys, but there is no requirement that every
67 record have the same number of keys.
68  
69 =head1 METHODS
70  
71 The following methods are supported by DBI::ProfileData objects.
72  
73 =cut
74  
75 2 our $VERSION = "2.010008";
76  
77 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 2 ▶use DBI::Profile qw(dbi_profile_merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 our $MAX_PATH_DEPTH = 256;
84  
85 # some constants for use with node data arrays
86 sub COUNT () { 0 };
87 sub TOTAL () { 1 };
88 sub FIRST () { 2 };
89 sub SHORTEST () { 3 };
90 sub LONGEST () { 4 };
91 sub FIRST_AT () { 5 };
92 sub LAST_AT () { 6 };
93 sub PATH () { 7 };
94  
95  
96 my $HAS_FLOCK = (defined $ENV{DBI_PROFILE_FLOCK})
97 ? $ENV{DBI_PROFILE_FLOCK}
98 2 ▶ : do { local $@; eval { flock STDOUT, 0; 1 } };
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'DBI_PROFILE_FLOCK'} ? : 0 2 2
99  
100  
101 =head2 $prof = DBI::ProfileData->new(File => "dbi.prof")
102  
103 =head2 $prof = DBI::ProfileData->new(File => "dbi.prof", Filter => sub { ... })
104  
105 =head2 $prof = DBI::ProfileData->new(Files => [ "dbi.prof.1", "dbi.prof.2" ])
106  
107 Creates a new DBI::ProfileData object. Takes either a single file
108 through the File option or a list of Files in an array ref. If
109 multiple files are specified then the header data from the first file
110 is used.
111  
112 =head3 Files
113  
114 Reference to an array of file names to read.
115  
116 =head3 File
117  
118 Name of file to read. Takes precedence over C<Files>.
119  
120 =head3 DeleteFiles
121  
122 If true, the files are deleted after being read.
123  
124 Actually the files are renamed with a C<deleteme> suffix before being read,
125 and then, after reading all the files, they're all deleted together.
126  
127 The files are locked while being read which, combined with the rename, makes it
128 safe to 'consume' files that are still being generated by L<DBI::ProfileDumper>.
129  
130 =head3 Filter
131  
132 The C<Filter> parameter can be used to supply a code reference that can
133 manipulate the profile data as it is being read. This is most useful for
134 editing SQL statements so that slightly different statements in the raw data
135 will be merged and aggregated in the loaded data. For example:
136  
137 Filter => sub {
138 my ($path_ref, $data_ref) = @_;
139 s/foo = '.*?'/foo = '...'/ for @$path_ref;
140 }
141  
142 Here's an example that performs some normalization on the SQL. It converts all
143 numbers to C<N> and all quoted strings to C<S>. It can also convert digits to
144 N within names. Finally, it summarizes long "IN (...)" clauses.
145  
146 It's aggressive and simplistic, but it's often sufficient, and serves as an
147 example that you can tailor to suit your own needs:
148  
149 Filter => sub {
150 my ($path_ref, $data_ref) = @_;
151 local $_ = $path_ref->[0]; # whichever element contains the SQL Statement
152 s/\b\d+\b/N/g; # 42 -> N
153 s/\b0x[0-9A-Fa-f]+\b/N/g; # 0xFE -> N
154 s/'.*?'/'S'/g; # single quoted strings (doesn't handle escapes)
155 s/".*?"/"S"/g; # double quoted strings (doesn't handle escapes)
156 # convert names like log_20001231 into log_NNNNNNNN, controlled by $opt{n}
157 s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
158 # abbreviate massive "in (...)" statements and similar
159 s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
160 }
161  
162 It's often better to perform this kinds of normalization in the DBI while the
163 data is being collected, to avoid too much memory being used by storing profile
164 data for many different SQL statement. See L<DBI::Profile>.
165  
166 =cut
167  
168 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
169 4 my $pkg = shift;
170 4 my $self = {
171 Files => [ "dbi.prof" ],
172 Filter => undef,
173 DeleteFiles => 0,
174 LockFile => $HAS_FLOCK,
175 _header => {},
176 _nodes => [],
177 _node_lookup => {},
178 _sort => 'none',
179 @_
180 };
181 4 bless $self, $pkg;
182  
183 # File (singular) overrides Files (plural)
184 4 ▶ $self->{Files} = [ $self->{File} ] if exists $self->{File};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"File"} 4 0 4
185  
186 4 $self->_read_files();
187 4 return $self;
188 }
189  
190 # read files into _header and _nodes
191 4 ▶sub _read_files {
Subroutine _read_files: called CC=14 SCAR=27.5
192 4 my $self = shift;
193 4 my $files = $self->{Files};
194 4 my $read_header = 0;
195 4 my @files_to_delete;
196  
197 4 my $fh = gensym;
198 4 foreach (@$files) {
199 4 my $filename = $_;
200  
201 4 ▶ if ($self->{DeleteFiles}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DeleteFiles"}) 2 2 4
202 2 my $newfilename = $filename . ".deleteme";
203 2 ▶ if ($^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "VMS") 0 2 2
204 # VMS default filesystem can only have one period
205 0 $newfilename = $filename . 'deleteme';
206 }
207 # will clobber an existing $newfilename
208 2 ▶ rename($filename, $newfilename)
Branch: 1 branch
BranchTrueFalseTotal
rename $filename, $newfilename or croak("Can't rename($filename, $newfilename): $!") 2 0 2
209 or croak "Can't rename($filename, $newfilename): $!";
210 # On a versioned filesystem we want old versions to be removed
211 2 ▶ 1 while (unlink $filename);
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename and do { '???' } 0 2 2
212 2 $filename = $newfilename;
213 }
214  
215 4 ▶ open($fh, "<", $filename)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or croak("Unable to read profile file '${filename}': $!") 4 0 4
216 or croak("Unable to read profile file '$filename': $!");
217  
218 # lock the file in case it's still being written to
219 # (we'll be forced to wait till the write is complete)
220 4 ▶ flock($fh, LOCK_SH) if $self->{LockFile};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"LockFile"} 4 0 4
221  
222 4 ▶ if (-s $fh) { # not empty
Branch: 1 branch
BranchTrueFalseTotal
if (-s $fh) 4 0 4
223 4 ▶ $self->_read_header($fh, $filename, $read_header ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$read_header ? : 0 4 4
224 4 $read_header = 1;
225 4 $self->_read_body($fh, $filename);
226 }
227 4 close($fh); # and release lock
228  
229 push @files_to_delete, $filename
230 4 ▶ if $self->{DeleteFiles};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DeleteFiles"} 2 2 4
231 }
232 4 for (@files_to_delete){
233 # for versioned file systems
234 2 ▶ 1 while (unlink $_);
Branch: 1 branch
BranchTrueFalseTotal
unlink $_ and do { '???' } 2 2 4
235 2 ▶ if(-e $_){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $_) 0 2 2
236 0 warn "Can't delete '$_': $!";
237 }
238 }
239  
240 # discard node_lookup now that all files are read
241 4 delete $self->{_node_lookup};
242 }
243  
244 # read the header from the given $fh named $filename. Discards the
245 # data unless $keep.
246 4 ▶sub _read_header {
Subroutine _read_header: called CC=6 SCAR=18.4
247 4 my ($self, $fh, $filename, $keep) = @_;
248  
249 # get profiler module id
250 4 my $first = <$fh>;
251 4 chomp $first;
252 4 ▶ $self->{_profiler} = $first if $keep;
Branch: 1 branch
BranchTrueFalseTotal
if $keep 4 0 4
253  
254 # collect variables from the header
255 4 local $_;
256 4 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 12 0 12
257 12 chomp;
258 12 ▶ last unless length $_;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 4 8 12
259 8 ▶ /^(\S+)\s*=\s*(.*)/
Branch: 1 branch
BranchTrueFalseTotal
/^(\S+)\s*=\s*(.*)/ or croak("Syntax error in header in $filename line $.: $_") 8 0 8
260 or croak("Syntax error in header in $filename line $.: $_");
261 # XXX should compare new with existing (from previous file)
262 # and warn if they differ (different program or path)
263 8 ▶ $self->{_header}{$1} = unescape_key($2) if $keep;
Branch: 1 branch
BranchTrueFalseTotal
if $keep 8 0 8
264 }
265 }
266  
267  
268 374 ▶sub unescape_key { # inverse of escape_key() in DBI::ProfileDumper
Subroutine unescape_key: called CC=1 SCAR=0.0
Pod undocumented
269 374 local $_ = shift;
270 374 s/(?<!\\)\\n/\n/g; # expand \n, unless it's a \\n
271 374 s/(?<!\\)\\r/\r/g; # expand \r, unless it's a \\r
272 374 s/\\\\/\\/g; # \\ to \
273 374 return $_;
274 }
275  
276  
277 # reads the body of the profile data
278 4 ▶sub _read_body {
Subroutine _read_body: called CC=10 SCAR=23.4
279 4 my ($self, $fh, $filename) = @_;
280 4 my $nodes = $self->{_nodes};
281 4 my $lookup = $self->{_node_lookup};
282 4 my $filter = $self->{Filter};
283  
284 # build up node array
285 4 my @path = ("");
286 4 my (@data, $path_key);
287 4 local $_;
288 4 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 640 4 644
289 640 chomp;
290 640 ▶ if (/^\+\s+(\d+)\s?(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\+\s+(\d+)\s?(.*)/) { } 366 274 640
elsif (s/^=\s+//) { } 274 0 274
291 # it's a key
292 366 my ($key, $index) = ($2, $1 - 1);
293  
294 # $index comes straight from the (untrusted) profile file; a
295 # single huge value would expand @path into a giant sparse
296 # array that is then join()ed and copied per data line, giving
297 # a tiny-file -> large-memory amplification. Bound it.
298 366 ▶ croak("Invalid path index at $filename line $.: $1 (You may need to adjust \$MAX_PATH_DEPTH)")
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index > $DBI::ProfileData::MAX_PATH_DEPTH 0 366 366
Condition33%1/3
$index < 0 or $index > $DBI::ProfileData::MAX_PATH_DEPTH
l!l&&r!l&&!r
00366
MC/DC0%0/2
$index < 0 or $index > $DBI::ProfileData::MAX_PATH_DEPTH
$index < 0 $index > $DBI::ProfileData::MAX_PATH_DEPTH
Truth table33%1/3
A or B
A= $index < 0B= $index > $DBI::ProfileData::MAX_PATH_DEPTH
ABresult
1X1
011
000
299 if $index < 0 || $index > $MAX_PATH_DEPTH;
300  
301 366 $#path = $index; # truncate path to new length
302 366 $path[$index] = unescape_key($key); # place new key at end
303  
304 }
305 elsif (s/^=\s+//) {
306 # it's data - file in the node array with the path in index 0
307 # (the optional minus is to make it more robust against systems
308 # with unstable high-res clocks - typically due to poor NTP config
309 # of kernel SMP behaviour, i.e. min time may be -0.000008))
310  
311 274 @data = split / /, $_;
312  
313 # corrupt data?
314 274 ▶ croak("Invalid number of fields in $filename line $.: $_")
Branch: 1 branch
BranchTrueFalseTotal
unless @data == 7 0 274 274
315 unless @data == 7;
316 274 ▶ croak("Invalid leaf node characters $filename line $.: $_")
Branch: 1 branch
BranchTrueFalseTotal
unless /^[-+ 0-9eE\.]+$/ 0 274 274
317 unless m/^[-+ 0-9eE\.]+$/;
318  
319 # hook to enable pre-processing of the data - such as mangling SQL
320 # so that slightly different statements get treated as the same
321 # and so merged in the results
322 274 ▶ $filter->(\@path, \@data) if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 247 27 274
323  
324 # elements of @path can't have NULLs in them, so this
325 # forms a unique string per @path. If there's some way I
326 # can get this without arbitrarily stripping out a
327 # character I'd be happy to hear it!
328 274 $path_key = join("\0",@path);
329  
330 # look for previous entry
331 274 ▶ if (exists $lookup->{$path_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup->{$path_key}) { } 208 66 274
332 # merge in the new data
333 208 dbi_profile_merge($nodes->[$lookup->{$path_key}], \@data);
334 } else {
335 # insert a new node - nodes are arrays with data in 0-6
336 # and path data after that
337 66 push(@$nodes, [ @data, @path ]);
338  
339 # record node in %seen
340 66 $lookup->{$path_key} = $#$nodes;
341 }
342 }
343 else {
344 0 croak("Invalid line type syntax error in $filename line $.: $_");
345 }
346 }
347 }
348  
349  
350  
351 =head2 $copy = $prof->clone();
352  
353 Clone a profile data set creating a new object.
354  
355 =cut
356  
357 4 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
358 4 my $self = shift;
359  
360 # start with a simple copy
361 4 my $clone = bless { %$self }, ref($self);
362  
363 # deep copy nodes
364 4 $clone->{_nodes} = [ map { [ @$_ ] } @{$self->{_nodes}} ];
365  
366 # deep copy header
367 4 $clone->{_header} = { %{$self->{_header}} };
368  
369 4 return $clone;
370 }
371  
372 =head2 $header = $prof->header();
373  
374 Returns a reference to a hash of header values. These are the key
375 value pairs included in the header section of the L<DBI::ProfileDumper>
376 data format. For example:
377  
378 $header = {
379 Path => [ '!Statement', '!MethodName' ],
380 Program => 't/42profile_data.t',
381 };
382  
383 Note that modifying this hash will modify the header data stored
384 inside the profile object.
385  
386 =cut
387  
388 0 ▶sub header { shift->{_header} }
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
389  
390  
391 =head2 $nodes = $prof->nodes()
392  
393 Returns a reference the sorted nodes array. Each element in the array
394 is a single record in the data set. The first seven elements are the
395 same as the elements provided by L<DBI::Profile>. After that each key is
396 in a separate element. For example:
397  
398 $nodes = [
399 [
400 2, # 0, count
401 0.0312958955764771, # 1, total duration
402 0.000490069389343262, # 2, first duration
403 0.000176072120666504, # 3, shortest duration
404 0.00140702724456787, # 4, longest duration
405 1023115819.83019, # 5, time of first event
406 1023115819.86576, # 6, time of last event
407 'SELECT foo FROM bar' # 7, key1
408 'execute' # 8, key2
409 # 6+N, keyN
410 ],
411 # ...
412 ];
413  
414 Note that modifying this array will modify the node data stored inside
415 the profile object.
416  
417 =cut
418  
419 10 ▶sub nodes { shift->{_nodes} }
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
420  
421  
422 =head2 $count = $prof->count()
423  
424 Returns the number of items in the profile data set.
425  
426 =cut
427  
428 4 ▶sub count { scalar @{shift->{_nodes}} }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
429  
430  
431 =head2 $prof->sort(field => "field")
432  
433 =head2 $prof->sort(field => "field", reverse => 1)
434  
435 Sorts data by the given field. Available fields are:
436  
437 longest
438 total
439 count
440 shortest
441  
442 The default sort is greatest to smallest, which is the opposite of the
443 normal Perl meaning. This, however, matches the expected behavior of
444 the dbiprof frontend.
445  
446 =cut
447  
448  
449 # sorts data by one of the available fields
450 {
451 2 my %FIELDS = (
452 longest => LONGEST,
453 total => TOTAL,
454 count => COUNT,
455 shortest => SHORTEST,
456 key1 => PATH+0,
457 key2 => PATH+1,
458 key3 => PATH+2,
459 );
460 10 ▶ sub sort {
Subroutine sort: called CC=4 SCAR=13.9
Pod documented
461 10 my $self = shift;
462 10 my $nodes = $self->{_nodes};
463 10 my %opt = @_;
464  
465 10 ▶ croak("Missing required field option.") unless $opt{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"field"} 0 10 10
466  
467 10 my $index = $FIELDS{$opt{field}};
468  
469 10 ▶ croak("Unrecognized sort field '$opt{field}'.")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 10 10
470 unless defined $index;
471  
472 # sort over index
473 10 ▶ if ($opt{reverse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'reverse'}) { } 4 6 10
474 @$nodes = sort {
475 4 $a->[$index] <=> $b->[$index]
476 } @$nodes;
477 } else {
478 @$nodes = sort {
479 6 $b->[$index] <=> $a->[$index]
480 } @$nodes;
481 }
482  
483 # remember how we're sorted
484 10 $self->{_sort} = $opt{field};
485  
486 10 return $self;
487 }
488 }
489  
490  
491 =head2 $count = $prof->exclude(key2 => "disconnect")
492  
493 =head2 $count = $prof->exclude(key2 => "disconnect", case_sensitive => 1)
494  
495 =head2 $count = $prof->exclude(key1 => qr/^SELECT/i)
496  
497 Removes records from the data set that match the given string or
498 regular expression. This method modifies the data in a permanent
499 fashion - use clone() first to maintain the original data after
500 exclude(). Returns the number of nodes left in the profile data set.
501  
502 =cut
503  
504 2 ▶sub exclude {
Subroutine exclude: called CC=9 SCAR=26.4
Pod documented
505 2 my $self = shift;
506 2 my $nodes = $self->{_nodes};
507 2 my %opt = @_;
508  
509 # find key index number
510 2 my ($index, $val);
511 2 foreach (keys %opt) {
512 2 ▶ if (/^key(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^key(\d+)$/) 2 0 2
513 2 $index = PATH + $1 - 1;
514 2 $val = $opt{$_};
515 2 last;
516 }
517 }
518 2 ▶ croak("Missing required keyN option.") unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 2 2
519  
520 2 ▶ if (UNIVERSAL::isa($val,"Regexp")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'Regexp')) { } 0 2 2
521 # regex match
522 @$nodes = grep {
523 0 ▶ $#$_ < $index or $_->[$index] !~ /$val/
Condition0%0/3
$_->$#* < $index || !($_->[$index] =~ /$val/)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->$#* < $index || !($_->[$index] =~ /$val/)
$_->$#* < $index !($_->[$index] =~ /$val/)
Truth table0%0/3
A || not(B)
A= $_->$#* < $indexB= !($_->[$index] =~ /$val/)
ABresult
1X1
011
000
524 } @$nodes;
525 } else {
526 2 ▶ if ($opt{case_sensitive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'case_sensitive'}) { } 0 2 2
527 @$nodes = grep {
528 0 ▶ $#$_ < $index or $_->[$index] ne $val;
Condition0%0/3
$_->$#* < $index || $_->[$index] ne $val
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->$#* < $index || $_->[$index] ne $val
$_->$#* < $index $_->[$index] ne $val
Truth table0%0/3
A || B
A= $_->$#* < $indexB= $_->[$index] ne $val
ABresult
1X1
011
000
529 } @$nodes;
530 } else {
531 2 $val = lc $val;
532 @$nodes = grep {
533 2 ▶ $#$_ < $index or lc($_->[$index]) ne $val;
Condition66%2/3
$_->$#* < $index || lc $_->[$index] ne $val
l!l&&r!l&&!r
02910
MC/DC50%1/2
$_->$#* < $index || lc $_->[$index] ne $val
$_->$#* < $index lc $_->[$index] ne $val
Truth table66%2/3
A || B
A= $_->$#* < $indexB= lc $_->[$index] ne $val
ABresult
1X1
011
000
534 } @$nodes;
535 }
536 }
537  
538 2 return scalar @$nodes;
539 }
540  
541  
542 =head2 $count = $prof->match(key2 => "disconnect")
543  
544 =head2 $count = $prof->match(key2 => "disconnect", case_sensitive => 1)
545  
546 =head2 $count = $prof->match(key1 => qr/^SELECT/i)
547  
548 Removes records from the data set that do not match the given string
549 or regular expression. This method modifies the data in a permanent
550 fashion - use clone() first to maintain the original data after
551 match(). Returns the number of nodes left in the profile data set.
552  
553 =cut
554  
555 4 ▶sub match {
Subroutine match: called CC=9 SCAR=26.7
Pod documented
556 4 my $self = shift;
557 4 my $nodes = $self->{_nodes};
558 4 my %opt = @_;
559  
560 # find key index number
561 4 my ($index, $val);
562 4 foreach (keys %opt) {
563 4 ▶ if (/^key(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^key(\d+)$/) 4 0 4
564 4 $index = PATH + $1 - 1;
565 4 $val = $opt{$_};
566 4 last;
567 }
568 }
569 4 ▶ croak("Missing required keyN option.") unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 4 4
570  
571 4 ▶ if (UNIVERSAL::isa($val,"Regexp")) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'Regexp')) { } 0 4 4
572 # regex match
573 @$nodes = grep {
574 0 ▶ $#$_ >= $index and $_->[$index] =~ /$val/
Condition0%0/3
$_->$#* >= $index && $_->[$index] =~ /$val/
!ll&&!rl&&r
000
MC/DC0%0/2
$_->$#* >= $index && $_->[$index] =~ /$val/
$_->$#* >= $index $_->[$index] =~ /$val/
Truth table0%0/3
A && B
A= $_->$#* >= $indexB= $_->[$index] =~ /$val/
ABresult
0X0
100
111
575 } @$nodes;
576 } else {
577 4 ▶ if ($opt{case_sensitive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'case_sensitive'}) { } 0 4 4
578 @$nodes = grep {
579 0 ▶ $#$_ >= $index and $_->[$index] eq $val;
Condition0%0/3
$_->$#* >= $index && $_->[$index] eq $val
!ll&&!rl&&r
000
MC/DC0%0/2
$_->$#* >= $index && $_->[$index] eq $val
$_->$#* >= $index $_->[$index] eq $val
Truth table0%0/3
A && B
A= $_->$#* >= $indexB= $_->[$index] eq $val
ABresult
0X0
100
111
580 } @$nodes;
581 } else {
582 4 $val = lc $val;
583 @$nodes = grep {
584 4 ▶ $#$_ >= $index and lc($_->[$index]) eq $val;
Condition66%2/3
$_->$#* >= $index && lc $_->[$index] eq $val
!ll&&!rl&&r
0379
MC/DC50%1/2
$_->$#* >= $index && lc $_->[$index] eq $val
$_->$#* >= $index lc $_->[$index] eq $val
Truth table66%2/3
A && B
A= $_->$#* >= $indexB= lc $_->[$index] eq $val
ABresult
0X0
100
111
585 } @$nodes;
586 }
587 }
588  
589 4 return scalar @$nodes;
590 }
591  
592  
593 =head2 $Data = $prof->Data()
594  
595 Returns the same Data hash structure as seen in L<DBI::Profile>. This
596 structure is not sorted. The nodes() structure probably makes more
597 sense for most analysis.
598  
599 =cut
600  
601 4 ▶sub Data {
Subroutine Data: called CC=4 SCAR=13.9
Pod documented
602 4 my $self = shift;
603 4 my (%Data, @data, $ptr);
604  
605 4 foreach my $node (@{$self->{_nodes}}) {
606 # traverse to key location
607 66 $ptr = \%Data;
608 66 foreach my $key (@{$node}[PATH .. $#$node - 1]) {
609 66 ▶ $ptr->{$key} = {} unless exists $ptr->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ptr->{$key} 32 34 66
610 66 $ptr = $ptr->{$key};
611 }
612  
613 # slice out node data
614 66 $ptr->{$node->[-1]} = [ @{$node}[0 .. 6] ];
615 }
616  
617 4 return \%Data;
618 }
619  
620  
621 =head2 $text = $prof->format($nodes->[0])
622  
623 Formats a single node into a human-readable block of text.
624  
625 =cut
626  
627 0 ▶sub format {
Subroutine format: not called CC=5 SCAR=34.0
Pod documented
628 0 my ($self, $node) = @_;
629 0 my $format;
630  
631 # setup keys
632 0 my $keys = "";
633 0 ▶ for (my $i = PATH; $i <= $#$node; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $node->$#*) 0 0 0
634 0 my $key = $node->[$i];
635  
636 # remove leading and trailing space
637 0 $key =~ s/^\s+//;
638 0 $key =~ s/\s+$//;
639  
640 # if key has newlines or is long take special precautions
641 0 ▶ if (length($key) > 72 or $key =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > 72 or $key =~ /\n/) { } 0 0 0
Condition0%0/3
length $key > 72 or $key =~ /\n/
l!l&&r!l&&!r
000
MC/DC0%0/2
length $key > 72 or $key =~ /\n/
length $key > 72 $key =~ /\n/
Truth table0%0/3
A or B
A= length $key > 72B= $key =~ /\n/
ABresult
1X1
011
000
642 0 $keys .= " Key " . ($i - PATH + 1) . " :\n\n$key\n\n";
643 } else {
644 0 $keys .= " Key " . ($i - PATH + 1) . " : $key\n";
645 }
646 }
647  
648 # nodes with multiple runs get the long entry format, nodes with
649 # just one run get a single count.
650 0 ▶ if ($node->[COUNT] > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->[0] > 1) { } 0 0 0
651 0 $format = <<END;
652 Count : %d
653 Total Time : %3.6f seconds
654 Longest Time : %3.6f seconds
655 Shortest Time : %3.6f seconds
656 Average Time : %3.6f seconds
657 END
658 0 return sprintf($format, @{$node}[COUNT,TOTAL,LONGEST,SHORTEST],
659 $node->[TOTAL] / $node->[COUNT]) . $keys;
660 } else {
661 0 $format = <<END;
662 Count : %d
663 Time : %3.6f seconds
664 END
665  
666 0 return sprintf($format, @{$node}[COUNT,TOTAL]) . $keys;
667  
668 }
669 }
670  
671  
672 =head2 $text = $prof->report(number => 10)
673  
674 Produces a report with the given number of items.
675  
676 =cut
677  
678 0 ▶sub report {
Subroutine report: not called CC=4 SCAR=30.0
Pod documented
679 0 my $self = shift;
680 0 my $nodes = $self->{_nodes};
681 0 my %opt = @_;
682  
683 0 ▶ croak("Missing required number option") unless exists $opt{number};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"number"} 0 0 0
684  
685 0 ▶ $opt{number} = @$nodes if @$nodes < $opt{number};
Branch: 1 branch
BranchTrueFalseTotal
if @$nodes < $opt{"number"} 0 0 0
686  
687 0 my $report = $self->_report_header($opt{number});
688 0 for (0 .. $opt{number} - 1) {
689 0 $report .= sprintf("#" x 5 . "[ %d ]". "#" x 59 . "\n",
690 $_ + 1);
691 0 $report .= $self->format($nodes->[$_]);
692 0 $report .= "\n";
693 }
694 0 return $report;
695 }
696  
697 # format the header for report()
698 0 ▶sub _report_header {
Subroutine _report_header: not called CC=3 SCAR=24.8
699 0 my ($self, $number) = @_;
700 0 my $nodes = $self->{_nodes};
701 0 my $node_count = @$nodes;
702  
703 # find total runtime and method count
704 0 my ($time, $count) = (0,0);
705 0 foreach my $node (@$nodes) {
706 0 $time += $node->[TOTAL];
707 0 $count += $node->[COUNT];
708 }
709  
710 0 my $header = <<END;
711  
712 DBI Profile Data ($self->{_profiler})
713  
714 END
715  
716 # output header fields
717 0 ▶ while (my ($key, $value) = each %{$self->{_header}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_header"};}) 0 0 0
718 0 $header .= sprintf(" %-13s : %s\n", $key, $value);
719 }
720  
721 # output summary data fields
722 0 $header .= sprintf(<<END, $node_count, $number, $self->{_sort}, $count, $time);
723 Total Records : %d (showing %d, sorted by %s)
724 Total Count : %d
725 Total Runtime : %3.6f seconds
726  
727 END
728  
729 0 return $header;
730 }
731  
732  
733 2 1;
734  
735 __END__
« blib/lib/Win32/DBIODBC.pm ↑ Summary blib/lib/DBI/ProfileDumper.pm »