| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | package Plack::Debugger::Panel::DBIProfile; | ||||||||||||||||||||||||||||
| 4 | 1 | $Plack::Debugger::Panel::DBIProfile::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use parent 'Plack::Debugger::Panel'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use DBI::Profile; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw(gettimeofday tv_interval); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | my $DBI_PROFILE_FORMAT = '%1$s XXX %11$fs / %10$d = %2$fs avg (first %12$fs, min %13$fs, max %14$fs)'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 0 | ▶ | my %args = @_ == 1 && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | $args{'title'} ||= 'DBI Profile'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"title"} ||= "DBI Profile"
MC/DC0%0/1
$args{"title"} ||= "DBI Profile"
$args{"title"}
Truth table0%0/2
A
A= $args{"title"}
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my $dbi_profile = delete $args{dbi_profile} || 6; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'dbi_profile'} || 6
MC/DC0%0/1
delete $args{'dbi_profile'} || 6
delete $args{'dbi_profile'}
Truth table0%0/2
A
A= delete $args{'dbi_profile'}
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $dbi_profile_format = delete $args{dbi_profile_format} || $DBI_PROFILE_FORMAT; | ||||||||||||||||||||||||||
|
Condition0%0/3
delete $args{'dbi_profile_format'} || $DBI_PROFILE_FORMAT
MC/DC0%0/2
delete $args{'dbi_profile_format'} || $DBI_PROFILE_FORMAT
delete $args{'dbi_profile_format'}
$DBI_PROFILE_FORMAT
Truth table0%0/3
A || B
A= delete $args{'dbi_profile_format'}B= $DBI_PROFILE_FORMAT
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # This is the JS formatter function that places the data in the panel. | ||||||||||||||||||||||||||||
| 22 | # https://metacpan.org/source/STEVAN/Plack-Debugger-0.03/example/app.psgi has some examples of formatters | ||||||||||||||||||||||||||||
| 23 | # The JS source is at $app/debugger/static/js/plack-debugger.js - search for 'formatters' | ||||||||||||||||||||||||||||
| 24 | # Options available are: pass_through, generic_data_formatter, ordered_key_value_pairs, | ||||||||||||||||||||||||||||
| 25 | # simple_data_table, simple_data_table_w_headers, multiple_data_table, multiple_data_table_w_headers, | ||||||||||||||||||||||||||||
| 26 | # ordered_keys_with_nested_data, nested_data, subrequest_formatter | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $args{'formatter'} ||= 'simple_data_table'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"formatter"} ||= "simple_data_table"
MC/DC0%0/1
$args{"formatter"} ||= "simple_data_table"
$args{"formatter"}
Truth table0%0/2
A
A= $args{"formatter"}
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $args{'before'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | my ($self, $env) = @_; | |||||||||||||||||||||||||||
| 32 | 0 | my $profile_obj = _set_profile_on_all_dbi_handles($dbi_profile); | |||||||||||||||||||||||||||
| 33 | 0 | $self->stash({ start => [ gettimeofday ], profile_obj => $profile_obj}); | |||||||||||||||||||||||||||
| 34 | 0 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $args{'after'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($self, $env, $resp) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my $start = $self->stash->{start}; | |||||||||||||||||||||||||||
| 40 | 0 | my $end = [ gettimeofday ]; | |||||||||||||||||||||||||||
| 41 | 0 | my $elapsed = tv_interval( $start, $end ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | $self->set_subtitle( $elapsed ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (my $profile_obj = $self->stash->{profile_obj}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | #my $duration = gettimeofday() - $start_time; | ||||||||||||||||||||||||||||
| 47 | 0 | my $time_in_dbi = dbi_profile_merge_nodes(my $totals=[], $profile_obj->{Data}); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # 'Profile Path: %1$s XXX Profile Data: %11$fs / %10$d = %2$fs avg (first %12$fs, min %13$fs, max %14$fs)' | ||||||||||||||||||||||||||||
| 50 | my @items = $profile_obj->as_text({ | ||||||||||||||||||||||||||||
| 51 | format => $dbi_profile_format, | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sortsub => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | 0 | my $ary = shift; | |||||||||||||||||||||||||||
| 54 | 0 | @$ary = sort { $b->[0][1] <=> $a->[0][1] } @$ary; | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | 0 | }); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $i = 1; my $n; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | my @rows = map {[$i++ %2 ? do {$n = $i * 0.5; "<b>$n. $_</b>"} : ' ' x 6 . $_]} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | map {split /XXX/} @items; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | $self->set_result( [ @rows ] ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $subtitle = sprintf "%.3f s (%d%%)", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | $time_in_dbi, ($elapsed) ? $time_in_dbi/$elapsed*100 : "-"; | ||||||||||||||||||||||||||||
| 66 | # only show item count if >1 because they'll always be one | ||||||||||||||||||||||||||||
| 67 | # for profile==1, the default, so it's only noise, and for other | ||||||||||||||||||||||||||||
| 68 | # profile levels they'll always be an extra 'empty' item for | ||||||||||||||||||||||||||||
| 69 | # calls that can't be associated with a particular statement etc. | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $subtitle .= " #".@items if @items > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | $self->set_subtitle($subtitle); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # disable profiling and silently discard profile data | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | local $DBI::Profile::ON_DESTROY_DUMP = sub { }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 0 | _set_profile_on_all_dbi_handles(undef); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | }; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | $class->SUPER::new( \%args ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub _set_profile_on_all_dbi_handles { | ||||||||||||||||||||||||||
|
Subroutine
_set_profile_on_all_dbi_handles: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 83 | 0 | my ($profile_spec) = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # for drivers we've not loaded yet | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $DBI::shared_profile = ($profile_spec) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | ? DBI::Profile->_auto_new($profile_spec) # XXX not documented | ||||||||||||||||||||||||||||
| 88 | : undef; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # for any existing handles | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | DBI->visit_handles(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 92 | 0 | shift->{Profile} = $DBI::shared_profile; | |||||||||||||||||||||||||||
| 93 | 0 | return 1; # keep going to visit all | |||||||||||||||||||||||||||
| 94 | 0 | }); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return $DBI::shared_profile; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | 1; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||